标签:ado ocs acs tps contain factor gets ted with
1.NuGet安装Autofac.Extensions.DependencyInjection
2.在Program中添加.UseServiceProviderFactory(new AutofacServiceProviderFactory())这句。
public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .UseServiceProviderFactory(new AutofacServiceProviderFactory()) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); }
在Startup中添加方法ConfigureContainer,在此方法中直接注册组件。
public void ConfigureContainer(ContainerBuilder builder) { var basePath = AppContext.BaseDirectory; // Register your own things directly with Autofac, like: //builder.RegisterModule(new autofac3()); var dllpath = Path.Combine(basePath, "Service.dll"); var assemblysdllpath = Assembly.LoadFrom(dllpath); builder.RegisterAssemblyTypes(assemblysdllpath).AsImplementedInterfaces(); }
[Route("api/[controller]/[action]")] [ApiController] public class StudentController : ControllerBase { protected readonly IGetStu _gg; public StudentController(IGetStu gg) { _gg = gg; } [HttpPost] public ActionResult<string> getname(string name) { return _gg.getname("jinwei"); } }
参考资料:https://autofac.readthedocs.io/en/latest/integration/aspnetcore.html
标签:ado ocs acs tps contain factor gets ted with
原文地址:https://www.cnblogs.com/JinweiChang/p/12597616.html