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.
public UserRepository(IUserRepository repo) { ... }
- Ron Beyer