【发布时间】:2020-07-23 21:51:48
【问题描述】:
将我的应用程序从 .Net Core 2.2 更新到 3.1 尝试解决 ActionFilter 的依赖项时出现错误。
我有这个过滤器
public class DoSomethingAttribute : ActionFilterAttribute
{
private readonly IWorkContext workContext;
public OneEnum OneParameter { get; set; }
public AuthorizeAdminAttribute(OneEnum oneParameter, IWorkContext workContext)
{
OneParameter = oneParameter;
workContext = workContext;
}
/**more stuff**/
}
我注册了
services.AddScoped<DoSomethingAttribute>();
我叫它
[HttpDelete]
[Authorize]
[Route("{id:int}")]
[TypeFilter(typeof(DoSomethingAttribute), Arguments = new object[] { OneEnum.OneValue } )]
public async Task<IActionResult> Delete(int id)
{}
使用 .Net core 2.2 版本可以正常工作,但使用 3.1 时出现此错误:
InvalidOperationException: Error while validating the service descriptor 'ServiceType: DoSomethingAttribute Lifetime: Scoped ImplementationType: DoSomethingAttribute': Unable to resolve service for type 'POneEnum' while attempting to activate 'DoSomethingAttribute'.
InvalidOperationException: Unable to resolve service for type 'OneEnum' while attempting to activate 'DoSomethingAttribute'.
有什么想法吗?
【问题讨论】:
-
只需删除 services.AddScoped
() 这一行,你会看到你的错误消失了,IWorkContext 将由 asp.net core 自动注入。 -
谢谢@Changemyminds,这就是解决方案
标签: c# .net asp.net-core .net-core