[英]Can I inject a dependency into a ServiceStack Request Filter?
I can successfully inject dependencies into my ServiceStack services but now I have a need to inject a dependency into a Request Filter. 我可以成功地将依赖项注入到我的ServiceStack服务中,但现在我需要将依赖项注入请求过滤器。 However this does not appear to work the same way. 然而,这看起来似乎没有相同的方式。
Here's my filter (it simply checks whether the source IP is in an approved list; it is this list I'm trying to inject): 这是我的过滤器(它只是检查源IP是否在批准列表中;这是我试图注入的列表):
public class CheckIPFilter : RequestFilterAttribute
{
private readonly IList<string> _IPAddresses = new List<string>();
public CheckIPFilter() { }
public CheckIPFilter(IList<string> IPAddresses)
{
_IPAddresses = IPAddresses;
}
public override void Execute(ServiceStack.ServiceHost.IHttpRequest req, ServiceStack.ServiceHost.IHttpResponse res, object requestDto)
{
if (!_IPAddresses.Contains(req.UserHostAddress))
{
var errResponse = DtoUtils.CreateErrorResponse("401", "Unauthorised", null);
var responseDto = DtoUtils.CreateResponseDto(requestDto, new ResponseStatus("401", "Unauthorised"));
var contentType = req.ResponseContentType;
var serializer = EndpointHost.AppHost.ContentTypeFilters.GetResponseSerializer(contentType);
res.ContentType = contentType;
var serializationContext = new HttpRequestContext(req, res, responseDto);
serializer(serializationContext, responseDto, res);
res.EndRequest(); //stops further execution of this request
return;
}
}
}
This is what's in my global.asax: 这就是我的global.asax中的内容:
var IPAddresses = new List<string>()
{ "99.99.99.99", "99.99.99.99", "99.99.99.99", "99.99.99.99" };
container.Register<IList<string>>(IPAddresses);
_IPAddresses is always null. _IPAddresses始终为null。
I guess I must be missing something basic here. 我想我必须遗漏一些基本的东西。 Is there a better way of approaching this? 有没有更好的方法来解决这个问题?
使用属性注入而不是构造函数注入用于过滤器属性,因为它们被克隆并且公共属性自动连接,而不是像从IOC实例化并自动连接的其他所有内容一样创建。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.