Autofac异常:无法解析构造函数'Void .ctor的参数 [英] Autofac Exception: Cannot resolve parameter of constructor 'Void .ctor
问题描述
我遇到以下错误:
ExceptionMessage =没有发现以下构造函数 类型上的"Autofac.Core.Activators.Reflection.DefaultConstructorFinder" 可以使用``RestAPI.DevelopersController''进行调用 服务和参数:无法解析参数 构造函数'Void'的'Services.DevelopersService userService' .ctor(Services.DevelopersService)".
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();
}
我该如何解决?
推荐答案
当 Autofac 尝试实例化DevelopersController
时,会出现此错误消息.为了创建一个新的DevelopersController
,它必须提供一个实例DevelopersService
,但是它们都没有在 Autofac 中注册.
This error message occurs when Autofac try to instantiate a DevelopersController
. In order to create a new DevelopersController
it have to provide an instance DevelopersService
but none of them are registered in Autofac.
即使是以下代码段
builder.RegisterAssemblyTypes(Assembly.Load("Services"))
.Where(t => t.Name.EndsWith("Service"))
.AsImplementedInterfaces()
.InstancePerLifetimeScope();
在 Autofac 中注册DevelopersService
,它不会将其注册为DevelopersService
,而是注册为已实现的接口(即IService<User>
)
register a DevelopersService
in Autofac, it doesn't register it as a DevelopersService
but as implemented interfaces (ie IService<User>
)
为了解决您的问题,您可以更改注册以将服务本身注册为
In order to fix your issue, you can change your registration to register the service as itself
builder.RegisterAssemblyTypes(Assembly.Load("Services"))
.Where(t => t.Name.EndsWith("Service"))
.AsImplementedInterfaces()
.AsSelf()
.InstancePerLifetimeScope();
或将您的DevelopersController
更改为不依赖DevelopersService
而是依赖IService<USer>
or change your DevelopersController
to not rely on DevelopersService
but on IService<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.
这篇关于Autofac异常:无法解析构造函数'Void .ctor的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!