Autofac Exception: Cannot resolve constructor parameter 'Void.ctor

I have the following error:

ExceptionMessage = None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' of type 'RestAPI.DevelopersController' can be called using available services and parameters: Cannot resolve 'Services.DevelopersService userService' parameter of 'Void.ctor' constructor (Services.DevelopersService) ".

Global.asax.cs

protected void Application_Start()
    {
        GlobalConfiguration.Configure(WebApiConfig.Register);
        AutoMapperConfig.RegisterMappings();
        var builder = new ContainerBuilder();
        builder.RegisterModule(new ServiceModule());
        builder.RegisterModule(new ORMModule());
        builder.RegisterApiControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired();
        var container = builder.Build();
        var resolver = new AutofacWebApiDependencyResolver(container);
        GlobalConfiguration.Configuration.DependencyResolver = resolver;
    }

      

ServiceModule.cs

public class ServiceModule : Autofac.Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.RegisterAssemblyTypes(Assembly.Load("Services"))
                 .Where(t => t.Name.EndsWith("Service"))
                 .AsImplementedInterfaces()
                 .InstancePerLifetimeScope();
    }
}

      

ORMModule.cs

public class ORMModule : Autofac.Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.RegisterType(typeof(DatabaseContext)).As(typeof(DbContext)).InstancePerLifetimeScope();
    }
}

      

DevelopersController

public class DevelopersController : ApiController
{
    private DevelopersService _developersService;

    public DevelopersController(DevelopersService userService)
    {
        _developersService = userService;
        _developersService.SetIdentity(HttpContext.Current.Request.LogonUserIdentity.Name.ToString().Substring(4));
    }

      

DevelopersService.cs

public class DevelopersService : IService<User>
{
    private DatabaseContext _db;

    public DevelopersService(DatabaseContext db)
    {
        _db = db;
    }

    public void SetIdentity(string username)
    {

    }

    public User Create(User entity)
    {
        return new User();
    }
    public User Read(User Id)
    {
        return new User();
    }
    public void Update(User user)
    {

    }
    public void Delete(User Id)
    {

    }
    public IEnumerable<User> GetAll()
    {
        return _db.Users.AsEnumerable();
    }
}

      

IService.cs

public interface IService<T> where T : BaseEntity
{
    void SetIdentity(string identity);
    T Create(T entity);
    T Read(T Id);
    void Update(T entity);
    void Delete(T Id);
    IEnumerable<T> GetAll();
}

      

How can I fix this?

+3


source to share


3 answers


This error message appears when Autofac tries to instantiate DevelopersController

. To create a new one DevelopersController

, it must provide an instance DevelopersService

, but none of them are registered with Autofac.

Eeven if the following piece of code

builder.RegisterAssemblyTypes(Assembly.Load("Services"))
       .Where(t => t.Name.EndsWith("Service"))
       .AsImplementedInterfaces()
       .InstancePerLifetimeScope();

      

register a DevelopersService

with Autofac, it does not register it as DevelopersService

, but as implemented interfaces (i.e. IService<User>

)

To correct your problem, you can change your registration to register the service as such



builder.RegisterAssemblyTypes(Assembly.Load("Services"))
       .Where(t => t.Name.EndsWith("Service"))
       .AsImplementedInterfaces()
       .AsSelf()
       .InstancePerLifetimeScope();

      

or change DevelopersController

to not rely on DevelopersService

, but onIService<User>

public class DevelopersController : ApiController
{
    private IService<USer> _userService;

    public DevelopersController(IService<USer> userService)
    {
        _userService= userService;
        _userService.SetIdentity(HttpContext.Current.Request.LogonUserIdentity.Name.ToString().Substring(4));
    }

      

I would recommend this solution.  

+4


source


OK so I am using nopcommerce and in my dependency registrar I have the type

Typed:

builder.RegisterType<CustomerReturnsService>().As<CustomerReturnsService>();

      



It is assumed that

builder.RegisterType<CustomerReturnsService>().As<ICustomerReturnsService>();

      

Note that I

in the last client the service returns. It binds to the service interface.

+1


source


hope my answer still helps, I think the problem is due to the lack of reference to the DevelopersService class that might be located in another project. Try the code below.

        builder.RegisterAssemblyTypes(typeof(DevelopersService).Assembly)
            .Where(t => t.Name.EndsWith("Service"))
            .AsImplementedInterfaces().InstancePerRequest();

      

0


source







All Articles