我有以下错误:
ExceptionMessage =找不到任何构造函数 ' Autofac.Core.Activators.Reflection.DefaultConstructorFinder'在类型上 ' RestAPI.DevelopersController'可以使用可用的方式调用 服务和参数:无法解析参数 ' Services.DevelopersService userService'构造函数' 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();
}
我该如何解决?
答案 0 :(得分:6)
当 Autofac 尝试实例化DevelopersController
时,会出现此错误消息。为了创建新的DevelopersController
,它必须提供实例DevelopersService
,但它们都没有在 Autofac 中注册。
如果下面的代码是
builder.RegisterAssemblyTypes(Assembly.Load("Services"))
.Where(t => t.Name.EndsWith("Service"))
.AsImplementedInterfaces()
.InstancePerLifetimeScope();
在 Autofac 中注册DevelopersService
,它不会将其注册为DevelopersService
,而是将其注册为实现的接口(即IService<User>
)
为了解决您的问题,您可以更改注册以将服务注册为
builder.RegisterAssemblyTypes(Assembly.Load("Services"))
.Where(t => t.Name.EndsWith("Service"))
.AsImplementedInterfaces()
.AsSelf()
.InstancePerLifetimeScope();
或将您的DevelopersController
更改为不依赖于DevelopersService
,而是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));
}
我会推荐这个解决方案。
答案 1 :(得分:3)
好的,我正在使用nopcommerce,在我的依赖注册商中我有一个类型
已输入:
builder.RegisterType<CustomerReturnsService>().As<CustomerReturnsService>();
原来是
builder.RegisterType<CustomerReturnsService>().As<ICustomerReturnsService>();
请注意上次客户的I
返回服务。它链接到服务的接口。
答案 2 :(得分:0)
希望我的回答仍有帮助,我认为问题是缺少对可能位于其他项目的DevelopersService类的引用。请尝试下面的代码。
builder.RegisterAssemblyTypes(typeof(DevelopersService).Assembly)
.Where(t => t.Name.EndsWith("Service"))
.AsImplementedInterfaces().InstancePerRequest();