不带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
。根本不需要服务地点。