lemunk
lemunk

Reputation: 2636

None of the constructors found with Autofac

Heres my error:

Autofac.Core.DependencyResolutionException: None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'ConnectCx.Web.PluginUI.MVC5.Services.DefaultService' can be invoked with the available services and parameters: Cannot resolve parameter 'ConnectCx.Web.PluginUI.MVC5.Repository.IRepository1[ConnectCx.Web.PluginUI.MVC5.Models.ViewModels.DependancyTestViewModel] dependancyRepository' of constructor 'Void .ctor(ConnectCx.Web.PluginUI.MVC5.Repository.IRepository1[ConnectCx.Web.PluginUI.MVC5.Models.ViewModels.DependancyTestViewModel])'.

Here's my Global

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));
    }

heres my Irepository

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);
}
}

heres my Default Service

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?";
    }
}

heres my default service interface

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

is this an issue with my implimentation of Irepository? if not what is the issue here?

Upvotes: 0

Views: 10063

Answers (1)

Cyril Durand
Cyril Durand

Reputation: 16192

This error message tells you that Autofac is not able to build a DefaultService instance because the only available constructor require a IRepository<DependancyTestViewModel> and none is registered.

In order to fix this error you have to register a IRepository<DependancyTestViewModel>.

For example :

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

or if you have a generic repository

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

Upvotes: 2

Related Questions