无法将参数传递给控制器​​构造函数 - Autofac MVC 5

时间:2015-06-12 14:22:24

标签: c# asp.net-mvc asp.net-mvc-4 autofac

我在16个小时内尝试修复我的应用中的一个问题。我已经搜索过互联网,但无法找到任何解决方案。我正在使用Onion Architecture和ASP.NET MVC。我不能将服务注入我的控制器。

控制器:

public class TestController : Controller
{
    private readonly IService<Customer> _service;
    public TestController(IService<Customer> service)
    {
        _service = service;
    }

    public async Task<ActionResult> Index()
    {
        var list = await _service.GetAllAsync();
        return View(list);
    }
}

IOC:

public class IocConfig
    {
        public static void RegisterDependencies()
        {
            var builder = new ContainerBuilder();
            const string nameOrConnectionString = "name=AppContext";
            builder.RegisterControllers(typeof(MvcApplication).Assembly);
            builder.RegisterModule<AutofacWebTypesModule>();
            builder.RegisterGeneric(typeof(EntityRepository<>)).As(typeof(IRepository<>)).InstancePerRequest();
            builder.RegisterGeneric(typeof(Service<>)).As(typeof(IService<>)).InstancePerRequest();
            builder.RegisterType(typeof(UnitOfWork)).As(typeof(IUnitOfWork)).InstancePerRequest();
            builder.Register<IEntitiesContext>(b =>
            {
                var logger = b.Resolve<ILogger>();
                var context = new AspnetIdentityWithOnionContext(nameOrConnectionString, logger);
                return context;
            }).InstancePerRequest();
            builder.Register(b => NLogLogger.Instance).SingleInstance();
            builder.RegisterModule(new IdentityModule());
                var container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }
    }

当我运行动作方法时,它会给出

  

&#34;没有为此对象定义无参数构造函数。&#34;

我使用的是最新版本的Autofac。请指导我。

0 个答案:

没有答案