Autofac都没有找到构造函数

时间:2015-07-23 08:51:46

标签: dependency-injection asp.net-mvc-5 autofac

继承我的错误:

  

Autofac.Core.DependencyResolutionException:可以使用可用的服务和参数调用“ConnectCx.Web.PluginUI.MVC5.Services.DefaultService”类型中的'Autofac.Core.Activators.Reflection.DefaultConstructorFinder'找到的构造函数:   无法解析参数'ConnectCx.Web.PluginUI.MVC5.Repository.IRepository 1[ConnectCx.Web.PluginUI.MVC5.Models.ViewModels.DependancyTestViewModel] dependancyRepository' of constructor 'Void .ctor(ConnectCx.Web.PluginUI.MVC5.Repository.IRepository 1 [ConnectCx.Web.PluginUI.MVC5.Models.ViewModels.DependancyTestViewModel])'。

这是我的全球

protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

        var builder = new ContainerBuilder();

        // Register our MVC controllers.
        builder.RegisterControllers(typeof(MvcApplication).Assembly);

        //Register our Services
        //builder.RegisterType(typeof(DefaultService)).AsImplementedInterfaces();
        //builder.RegisterType<IRepository>();
        //builder.RegisterType(typeof(IRepository)).AsImplementedInterfaces();
        builder.RegisterType<DefaultService>().As<IDefaultService>().SingleInstance().PreserveExistingDefaults();

        // OPTIONAL: Register model binders that require DI.
        builder.RegisterModelBinders(Assembly.GetExecutingAssembly());
        builder.RegisterModelBinderProvider();

        // OPTIONAL: Register web abstractions like HttpContextBase.
        builder.RegisterModule<AutofacWebTypesModule>();

        // OPTIONAL: Enable property injection in view pages.
        builder.RegisterSource(new ViewRegistrationSource());

        // OPTIONAL: Enable property injection into action filters.
        builder.RegisterFilterProvider();

        // Set the dependency resolver to be Autofac.
        var container = builder.Build();

        DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
    }

继承我的存储库

namespace ConnectCx.Web.PluginUI.MVC5.Repository
{
public interface IRepository<T> where T : class
{
    T Get(object id);
    void Attach(T entity);
    IQueryable<T> GetAll();
    void Insert(T entity);
    void Delete(T entity);
    void Update(T entity);
    void SubmitChanges(); //need?
    T Find(Expression<Func<T, bool>> predicate);
    IQueryable<T> SearchFor(Expression<Func<T, bool>> predicate);
    T GetById(int id);
}
}

继承我的默认服务

public class DefaultService : IDefaultService
{
    private readonly IRepository<DependancyTestViewModel> _dependancyRepository;

    public DefaultService(IRepository<DependancyTestViewModel> dependancyRepository)
    {
        this._dependancyRepository = dependancyRepository;
    }

    public virtual string DependancyStringTest()
    {
        var test = _dependancyRepository.GetAll();//expect to blowup for test

        return "hello world, wasup?";
    }
}

继承我的默认服务界面

namespace ConnectCx.Web.PluginUI.MVC5.Services.ServiceInterface
{
    public interface IDefaultService
    {
        string DependancyStringTest();
    }
}

这是我对Irepository的暗示问题吗?如果不是这里有什么问题?

1 个答案:

答案 0 :(得分:2)

此错误消息告诉您 Autofac 无法构建DefaultService实例,因为唯一可用的构造函数需要IRepository<DependancyTestViewModel>且未注册任何实例。

要修复此错误,您必须注册IRepository<DependancyTestViewModel>

例如:

builder.RegisterType<DependancyTestViewModelRepository>()
       .As<IRepository<DependancyTestViewModel>>(); 

或者如果您有通用存储库

builder.RegisterGeneric(typeof(GenericRepository<>))
       .As(typeof(IRepository<>));