I have the following error:
ExceptionMessage=None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'RestAPI.DevelopersController' can be invoked with the available services and parameters: Cannot resolve parameter 'Services.DevelopersService userService' of constructor 'Void .ctor(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 it?
This error message occurs when Autofac try to instantiate a
DevelopersController
. In order to create a newDevelopersController
it have to provide an instanceDevelopersService
but none of them are registered in Autofac.Eeven if the following piece of code
register a
DevelopersService
in Autofac, it doesn't register it as aDevelopersService
but as implemented interfaces (ieIService<User>
)In order to fix your issue, you can change your registration to register the service as itself
or change your
DevelopersController
to not rely onDevelopersService
but onIService<USer>
I would recommend this solution.
hope my answer still helps, I think the problem is the lack of reference the DevelopersService class which maybe located at other project. Try the code below.
OK so I'm using nopcommerce, and in my dependancy registrar I have a type
Had typed:
Supposed to be
Notice the
I
in last customer returns service. It links to the interface of the service.