2
votes

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.

2
It looks like it wants you to make a constructor with a single parameter, ie: public UserRepository(IUserRepository repo) { ... } - Ron Beyer
@RonBeyer: In my Repository-class? - Bryan
The answer to "can anyone help me" is "yes", but it's probably not actually the question you want to ask. As an editor I tend to trim it out - the best sorts of questions are specific, e.g. "why does <strange behaviour X> occur in <conditions>". That's much answerable than asking for "help", which is a bit broad and undefined. - halfer

2 Answers

1
votes

(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;
1
votes

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

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