.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();

 

以上是 .net控制台依赖注入 的全部内容, 来源链接: utcz.com/z/512745.html

回到顶部