//依赖注入public void ConfigureContainer(ContainerBuilder builder){Assembly iserAss = Assembly.Load(Interface);Assembly serAss = Assembly.Load(Service);//创建serAss中的所有类的instance以此类的实现...
//依赖注入 public void ConfigureContainer(ContainerBuilder builder) { Assembly iserAss = Assembly.Load("Interface"); Assembly serAss = Assembly.Load("Service"); //创建serAss中的所有类的instance以此类的实现接口存储 builder.RegisterAssemblyTypes(iserAss, serAss).AsImplementedInterfaces().PropertiesAutowired();//支持构造方式注入和属性方式注入,不加PropertiesAutowired则只支持构造方式 //注入到控制器 var controllersTypesInAssembly = typeof(Startup).Assembly.GetExportedTypes() .Where(type => typeof(Controller).IsAssignableFrom(type)).ToArray(); builder.RegisterTypes(controllersTypesInAssembly).PropertiesAutowired(); }
public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureAppConfiguration((hostingContext, configBuilder) => { //configBuilder // .AddJsonFile("Config/Mongo.json") // .AddJsonFile("Config/SqlConn.json");//注入json文件 }) .UseServiceProviderFactory(new AutofacServiceProviderFactory()) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>().UseUrls("http://*:8080"); }); }
沃梦达教程
本文标题为:.net core autofac依赖注入简洁版
基础教程推荐
猜你喜欢
- C# windows语音识别与朗读实例 2023-04-27
- C#控制台实现飞行棋小游戏 2023-04-22
- C# List实现行转列的通用方案 2022-11-02
- C#类和结构详解 2023-05-30
- unity实现动态排行榜 2023-04-27
- winform把Office转成PDF文件 2023-06-14
- C# 调用WebService的方法 2023-03-09
- 一个读写csv文件的C#类 2022-11-06
- linux – 如何在Debian Jessie中安装dotnet core sdk 2023-09-26
- ZooKeeper的安装及部署教程 2023-01-22