Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.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
不带Autofac的C#AuthorizeAttribute InstancePerRequest_C#_.net_Attributes_Autofac_Action Filter - Fatal编程技术网

不带Autofac的C#AuthorizeAttribute InstancePerRequest

不带Autofac的C#AuthorizeAttribute InstancePerRequest,c#,.net,attributes,autofac,action-filter,C#,.net,Attributes,Autofac,Action Filter,我尝试为每个请求创建新的AuthenticationAttribute。我找到了一种使用iautofacrizationfilter的方法,我可以在WebApiConfig中注册,然后我可以为每个请求使用新实例。但是,如果没有autofac,就没有办法做到这一点 public class AuthenticationAttribute : IAutofacAuthorizationFilter { private readonly IAuthService authService;

我尝试为每个请求创建新的
AuthenticationAttribute
。我找到了一种使用
iautofacrizationfilter
的方法,我可以在
WebApiConfig
中注册,然后我可以为每个请求使用新实例。但是,如果没有autofac,就没有办法做到这一点

public class AuthenticationAttribute : IAutofacAuthorizationFilter
{
    private readonly IAuthService authService;

    public AuthenticationAttribute(IAuthService authService)
    {
        this.authService = authService;
    }

    public  async Task OnAuthorizationAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
    {
        //some actionContext control with authService
    }
}

我需要这样做,因为
IAuthService
是每个请求的实例。我正在为DI使用Autofac,但我不想像这样直接在代码中使用Autofac。

我肯定你已经知道了,我读过了。我们可以说AuthorizeAttribute仍然是单例的,但是IAuthService为每个请求创建的类您必须使用
RequestServices
上的服务位置才能在属性中获得每个请求的内容,如
IAuthService
。属性上没有DI。文档中给出了一个例子。您需要使用来解析每个请求的
IAuthorizationFilter
。根本不需要服务地点。我相信你已经知道了。是的,我看过了。我们可以说AuthorizeAttribute仍然是单例的,但是IAuthService为每个请求创建的类您必须使用
RequestServices
上的服务位置才能在属性中获得每个请求的内容,如
IAuthService
。属性上没有DI。文档中给出了一个例子。您需要使用来解析每个请求的
IAuthorizationFilter
。根本不需要服务地点。