7°

.net 控制台依赖注入

//配置  
var builder = new ConfigurationBuilder()
           .SetBasePath(Directory.GetCurrentDirectory())
           .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
           .AddEnvironmentVariables();
        IConfigurationRoot configuration = builder.Build();

        IServiceCollection services = new ServiceCollection();
        //注入模式访问数据库
        services.AddDbContextPool<ExDBContext>(options =>
         {
             options.UseSqlServer(configuration.GetConnectionString("sqlserver"), //读取配置文件中的链接字符串
             b => b.UseRowNumberForPaging());
         })
            .AddTransient<dbcontext>()
            .AddOptions();

        //注入服务
        services.AddScoped<接口, 实现>();

//配置项 services.Configure<ApiUrlConfig>(configuration.GetSection("ApiUrlConfig")); //构建容器 IServiceProvider serviceProvider = services.BuildServiceProvider(); //解析 var memcachedClient = serviceProvider.GetService<ISynDataService>(); var log = serviceProvider.GetService<接口>(); //调取方法 memcachedClient.SynsArchives();

 

本文由【L】发布于开源中国,原文链接:https://my.oschina.net/u/3911753/blog/3158613

全部评论: 0

    我有话说: