Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ActionFilterAttribute中的aspnet核心mvc访问依赖项指示服务_C#_Asp.net Core_Asp.net Core Mvc - Fatal编程技术网

C# ActionFilterAttribute中的aspnet核心mvc访问依赖项指示服务

C# ActionFilterAttribute中的aspnet核心mvc访问依赖项指示服务,c#,asp.net-core,asp.net-core-mvc,C#,Asp.net Core,Asp.net Core Mvc,我正在尝试访问ActionFilterAttribute中的以下服务 iFoosService服务 public interface IFooService<T> { List<T> GetFoos { get; set; } } 公共接口IFooService { List GetFoos{get;set;} } 实施*FooService** public class FooService : IFooService<int> {

我正在尝试访问ActionFilterAttribute中的以下服务

iFoosService服务

 public interface IFooService<T>
 {
     List<T> GetFoos { get; set; }
 }
公共接口IFooService
{
List GetFoos{get;set;}
}
实施*FooService**

public class FooService : IFooService<int>
{
    public List<int> GetFoos()
    {
       //do something interesting here.
           return new List<int>();
    }
}
公共类FooService:IFooService
{
公共列表GetFoos()
{
//在这里做些有趣的事。
返回新列表();
}
}
上述服务在aspnetcore依赖项容器中注册为:

services.AddScoped<IFooService<int>, FooService>();
services.addScope();
我想使用属性中的IFooService。但是,在属性级别,我不知道类型参数

[AttributeUsage(AttributeTargets.Method)]
public class FooActionFilterAttribute : ActionFilterAttribute
{
    public FooActionFilterAttribute(Type serviceType)
    {
        ServiceType = serviceType;
    }

    public Type ServiceType { get; }

    public override void OnActionExecuted(ActionExecutedContext context)
    {
        var service =  context.HttpContext.RequestServices.GetService(ServiceType) as FooService;                   
    }
}

// usage
[FooActionFilter(typeof(IFilterService<int>))]
public IActionResult ActionMethod()
{
}
在不知道类型参数的情况下,是否可以在ActionFilterAttribute中找到上述服务?我希望能够仅在接口上调用GetFoos方法

//这是我的尝试

 public class FooActionFilter : ActionFilterAttribute
 {
      public override void OnActionExecuted(ActionExecutedContext context)
      {
            //error here generic service require 1 argument. But i don't know how to pass this argument..
            var foo = (IFooService) context.HttpContext.RequestServices.GetService(typeof(IFooService<>));                                    
      }
 }
公共类FooActionFilter:ActionFilterAttribute
{
公共覆盖无效OnActionExecuted(ActionExecutedContext上下文)
{
//此处出错泛型服务需要1个参数。但我不知道如何传递此参数。。
var foo=(IFooService)context.HttpContext.RequestServices.GetService(typeof(IFooService));
}
}

您可以将类型作为参数存储在操作筛选器中

[AttributeUsage(AttributeTargets.Method)]
public class FooActionFilterAttribute : ActionFilterAttribute
{
    public FooActionFilterAttribute(Type serviceType)
    {
        ServiceType = serviceType;
    }

    public Type ServiceType { get; }

    public override void OnActionExecuted(ActionExecutedContext context)
    {
        var service =  context.HttpContext.RequestServices.GetService(ServiceType) as FooService;                   
    }
}

// usage
[FooActionFilter(typeof(IFilterService<int>))]
public IActionResult ActionMethod()
{
}
[AttributeUsage(AttributeTargets.Method)]
公共类FooActionFilterAttribute:ActionFilterAttribute
{
公共FooActionFilterAttribute(类型serviceType)
{
服务类型=服务类型;
}
公共类型ServiceType{get;}
公共覆盖无效OnActionExecuted(ActionExecutedContext上下文)
{
var service=context.HttpContext.RequestServices.GetService(ServiceType)作为FooService;
}
}
//用法
[FooActionFilter(类型(IFilterService))]
公共IActionResult操作方法()
{
}

您创建并在
iFoosService
中注册了它,这就是您应该要求的。您好,Nkosi,我想将此属性打包到一个Nuget包中。在这种情况下,我不会总是知道类型。@John你的代码甚至不会编译。您希望通过泛型实现什么?没有
IFooService
但是
IFooService
所以您的
就是IFooService甚至不会编译。并且将给出与hist post=>//error中OP所说的相同的错误,此处通用服务要求1参数。但我不知道如何传递此参数。@codenotfind已修复为使用
FooService
。谢谢你纠正我的错误。我只是想给OP一些大致的指导。我不知道他到底想达到什么目的。。它将被编译,但是你打破了Liskov替换原则。您的代码不允许我用
IFooService
的另一个实现替代。即使对于单元测试,我也不能嘲笑它。@CodeNotFound正如我所说的,我甚至不知道OP想要实现什么。我向利斯科夫先生致以最谦卑的歉意。我甚至不知道OP想要达到什么目的。在OP帖子上询问更多的细节也可能有助于获得准确的答案:)