[FromServiceContext]在服务里使用正常,在Controller里使用不正常,请问是什么原因
yus1977 opened this issue · 4 comments
yus1977 commented
[FromServiceContext]在服务里使用正常,在Controller里使用不正常,请问是什么原因
huoshan12345 commented
可否提供一些能复现的代码
IMwinglee commented
同问:webapi项目
api控制器中,添加[UnitOfWork]标签,无效果
Startup中注册
builder.RegisterType<Repository>().As<IUnitOfWork>();
builder.RegisterDynamicProxy();
工作单元
public class UnitOfWorkAttribute : AbstractInterceptorAttribute
{
private IUnitOfWork _unitOfWork;
public override async Task Invoke(AspectContext context, AspectDelegate next)
{
try
{
_unitOfWork = context.ServiceProvider.GetService(typeof(IUnitOfWork)) as IUnitOfWork;
_unitOfWork?.BeginTransaction();
await next(context);
_unitOfWork?.CommitTransaction();
}
catch (Exception ex)
{
_unitOfWork?.RollbackTransaction();
}
}
}
chaoyebugao commented
一样的问题,在逻辑层Service正常,Controller里面用FromServiceContext注入不了,为null
huoshan12345 commented
@yus1977 @IMwinglee @chaoyebugao
在Startup
的ConfigureServices
方法中需要调用AddControllersAsServices
方法,例如
services.AddControllers()
.AddControllersAsServices(); // 关键是这行
这样就可以在Controller正常使用属性注入了。
本issue先关闭了,如果有问题再重新开