.net core 3.0 MVC學習-autofac

1.引入:Autofac.Extensions.DependencyInjectionweb

2.在Program裏面ide

public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                })
             .UseServiceProviderFactory(new AutofacServiceProviderFactory()); //配置autofac;
    }

3.在Startup裏面新增ui

services.AddControllersWithViews().AddControllersAsServices();
public void ConfigureContainer(ContainerBuilder builder)
        {
            //註冊controller裏面的service
            var controllerBaseType = typeof(Microsoft.AspNetCore.Mvc.ControllerBase);
            builder.RegisterAssemblyTypes(typeof(Program).Assembly)
                .Where(t => controllerBaseType.IsAssignableFrom(t) && t != controllerBaseType)
                .InstancePerLifetimeScope().PropertiesAutowired();

            // 在這裏添加服務註冊

        }

大功告成spa

相關文章
相關標籤/搜索