C# 在.net Core 3.1中将ActionFilterAttribute解析为TypeFilter时出错
将我的应用程序从.Net Core 2.2更新到3.1时,我在尝试解决ActionFilter的依赖关系时出错 我有这个过滤器C# 在.net Core 3.1中将ActionFilterAttribute解析为TypeFilter时出错,c#,.net,asp.net-core,.net-core,C#,.net,Asp.net Core,.net Core,将我的应用程序从.Net Core 2.2更新到3.1时,我在尝试解决ActionFilter的依赖关系时出错 我有这个过滤器 public class DoSomethingAttribute : ActionFilterAttribute { private readonly IWorkContext workContext; public OneEnum OneParameter { get; set; } public Auth
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>();
有什么想法吗?下面是一个工作演示,如下所示:
public class MyFilterAttribute : TypeFilterAttribute
{
public MyFilterAttribute(params object[] arguments) : base(typeof(DoSomethingAttribute))
{
Arguments = new object[] { arguments };
}
public class DoSomethingAttribute : ActionFilterAttribute
{
private readonly IWorkContext WorkContext;
private readonly OneEnum OneParameter;
public DoSomethingAttribute(object[] arguments, IWorkContext workContext)
{
OneParameter = (OneEnum)arguments[0];
WorkContext = workContext;
}
public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
//do your stuff....
await base.OnActionExecutionAsync(context, next);
}
}
}
services.AddScoped<DoSomethingAttribute>();
将属性添加到控制器:
[MyFilter(OneEnum.OneValue)]
public async Task<IActionResult> Delete(int id)
{}
[MyFilter(OneEnum.OneValue)]
公共异步任务删除(int-id)
{}
无需注册如下属性:
public class MyFilterAttribute : TypeFilterAttribute
{
public MyFilterAttribute(params object[] arguments) : base(typeof(DoSomethingAttribute))
{
Arguments = new object[] { arguments };
}
public class DoSomethingAttribute : ActionFilterAttribute
{
private readonly IWorkContext WorkContext;
private readonly OneEnum OneParameter;
public DoSomethingAttribute(object[] arguments, IWorkContext workContext)
{
OneParameter = (OneEnum)arguments[0];
WorkContext = workContext;
}
public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
//do your stuff....
await base.OnActionExecutionAsync(context, next);
}
}
}
services.AddScoped<DoSomethingAttribute>();
services.addScope();
只要删除服务。AddScoped()这一行,您就会看到错误消失,IWorkContext将由asp.net core自动注入。谢谢@Changemyminds,这就是解决方案谢谢。我删除了注册并且成功了。如果我的解决方案对您有所帮助,您能接受我的回答吗?