.Net Core 将默认DI改为Autofac

首先引用Autofac相关的包

1.在程序包管理控制台

Install-Package Autofac.Configuration

Install-Package Autofac.Extensions.DependencyInjection

2.添加完成后,修改Program.cs文件(注意命名空间改成自己的)

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Autofac.Extensions.DependencyInjection;


namespace ET.BUA.Web
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var host = new WebHostBuilder()
                .UseKestrel()
                .ConfigureServices(services => services.AddAutofac())
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration()
                .UseStartup<Startup>()
                .UseApplicationInsights()
                .Build();


            host.Run();
        }
    }

}

3.将默认的public void ConfigureServices这个方法替换成如下代码

public IServiceProvider ConfigureServices(IServiceCollection services)

        {

            //注册数据库

            services.AddDbContext<BUAContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("BUAConnection")));

            
            services.AddRouting(options => options.LowercaseUrls = true);

            
            services.AddMvc().AddControllersAsServices();


            var builder = new ContainerBuilder();

            //注册UnitOfWork,此处先注释掉
            //builder.RegisterType<IUnitOfWork>().AsImplementedInterfaces().InstancePerLifetimeScope();

            //注册仓储
            builder.RegisterModule(new RepositoryModule());

            //注册服务
            builder.RegisterModule(new ServiceModule());


            builder.Populate(services);


            return new AutofacServiceProvider(builder.Build());

        }

仓储注入模块

using Autofac;
using ET.BUA.Domain.Repository.Implementation;
using System.Linq;


namespace ET.BUA.Web.Modules
{
    public class RepositoryModule : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            builder.RegisterAssemblyTypes(typeof(ApplicationRepository).Assembly)
                .Where(t => t.Name.EndsWith("Repository"))
                .AsImplementedInterfaces()
                .InstancePerLifetimeScope();
        }
    }
}

服务注入模块

using Autofac;
using ET.BUA.Service.Service.Implementation;
using System.Linq;

namespace ET.BUA.Web.Modules
{
    public class ServiceModule : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            builder.RegisterAssemblyTypes(typeof(ApplicationService).Assembly)
                .Where(t => t.Name.EndsWith("Service"))
                .AsImplementedInterfaces()
                .InstancePerLifetimeScope();
        }
    }

}

4.在Controlle中引入构造方法,作为参数传递

       private readonly IApplicationService _applicationService;

        public AppController(IApplicationService applicationService)
        {
            this._applicationService = applicationService;
        }

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页