Ric*_*rdo 9 c# asp.net autofac asp.net-web-api
我有以下错误:
ExceptionMessage =可以使用可用的服务和参数调用在'RestAPI.DevelopersController'类型上使用'Autofac.Core.Activators.Reflection.DefaultConstructorFinder'找到的构造函数:无法解析构造函数'Void'的参数'Services.DevelopersService userService'.构造函数(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;
}
Run Code Online (Sandbox Code Playgroud)
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();
}
}
Run Code Online (Sandbox Code Playgroud)
ORMModule.cs
public class ORMModule : Autofac.Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType(typeof(DatabaseContext)).As(typeof(DbContext)).InstancePerLifetimeScope();
}
}
Run Code Online (Sandbox Code Playgroud)
DevelopersController
public class DevelopersController : ApiController
{
private DevelopersService _developersService;
public DevelopersController(DevelopersService userService)
{
_developersService = userService;
_developersService.SetIdentity(HttpContext.Current.Request.LogonUserIdentity.Name.ToString().Substring(4));
}
Run Code Online (Sandbox Code Playgroud)
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();
}
}
Run Code Online (Sandbox Code Playgroud)
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();
}
Run Code Online (Sandbox Code Playgroud)
我该如何解决?
当Autofac尝试实例化a 时,会出现此错误消息DevelopersController.为了创建新的,DevelopersController它必须提供一个实例,DevelopersService但它们都没有在Autofac中注册.
Eeven如果下面这段代码
builder.RegisterAssemblyTypes(Assembly.Load("Services"))
.Where(t => t.Name.EndsWith("Service"))
.AsImplementedInterfaces()
.InstancePerLifetimeScope();
Run Code Online (Sandbox Code Playgroud)
DevelopersService在Autofac中注册一个,它不会将其注册DevelopersService为实现的接口(即IService<User>)
为了解决您的问题,您可以更改注册以将服务注册为自身
builder.RegisterAssemblyTypes(Assembly.Load("Services"))
.Where(t => t.Name.EndsWith("Service"))
.AsImplementedInterfaces()
.AsSelf()
.InstancePerLifetimeScope();
Run Code Online (Sandbox Code Playgroud)
或改变你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));
}
Run Code Online (Sandbox Code Playgroud)
我会推荐这个解决方案.
好的,我正在使用 nopcommerce,在我的依赖注册商中我有一个类型
已输入:
builder.RegisterType<CustomerReturnsService>().As<CustomerReturnsService>();
Run Code Online (Sandbox Code Playgroud)
应该是
builder.RegisterType<CustomerReturnsService>().As<ICustomerReturnsService>();
Run Code Online (Sandbox Code Playgroud)
注意I最后的客户退货服务。它链接到服务的接口。
| 归档时间: |
|
| 查看次数: |
20004 次 |
| 最近记录: |