Bryan
Bryan

Reputation: 3541

None of the constructors found with - autofac

I have the following autofac-config:

public static void RegisterDI()
{
    var builder = GetBuilder();
    var container = builder.Build();

    DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
public static ContainerBuilder GetBuilder()
{
    var builder = new ContainerBuilder();
    builder.RegisterControllers(Assembly.GetCallingAssembly());
    builder.RegisterFilterProvider();

    var assemblies = BuildManager.GetReferencedAssemblies().Cast<Assembly>().Where(x => x.FullName.Contains("Soundyladder")).ToArray();

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

    builder.RegisterAssemblyModules(assemblies);

    return builder;
}

My application consist of three layers: UI, Service, and DataAcces. Here is my UserRepository from the DataAccess-layer:

public class UserRepository : IUserRepository
{
}

Here is my service from the the service layer:

public UserService(IUserRepository userRepository)
{
    this._userRepository = userRepository;
}

And here is my controller:

public UserController(IUserService userService)
{
    this._userService = userService;
}

Everytime I start the application, I get the following error:

None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'Soundyladder.Service.Services.UserService' can be invoked with the available services and parameters: Cannot resolve parameter 'Soundyladder.DataAccess.Repositories.IUserRepository userRepository' of constructor 'Void .ctor(Soundyladder.DataAccess.Repositories.IUserRepository)'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: Autofac.Core.DependencyResolutionException: None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'Soundyladder.Service.Services.UserService' can be invoked with the available services and parameters: Cannot resolve parameter 'Soundyladder.DataAccess.Repositories.IUserRepository userRepository' of constructor 'Void .ctor(Soundyladder.DataAccess.Repositories.IUserRepository)'.

I have no idea why this happens. I have the same setup In this project that I have In my other projects. When I compare my other projects with this, I can't see any difference.

Upvotes: 2

Views: 9826

Answers (2)

halfer
halfer

Reputation: 20420

(Posted solution on behalf of the question author).

I changed the autofac config. Now it's working:

    var builder = new ContainerBuilder();
    builder.RegisterControllers(Assembly.GetExecutingAssembly());
    builder.RegisterFilterProvider();

    var assemblies = BuildManager.GetReferencedAssemblies().Cast<Assembly>().Where(x => x.FullName.Contains("Soundyladder")).ToArray();
    builder.RegisterAssemblyTypes(assemblies)
        .AsImplementedInterfaces()
        .InstancePerRequest();

    return builder;

Upvotes: 1

Orel Eraki
Orel Eraki

Reputation: 12196

It looks like you only registering "Service" suffix types, and forgot about "Repository" suffix types.

.Where(t => t.Name.EndsWith("Service"))

Upvotes: 1

Related Questions