在集合中找不到ServiceStack标记Attribute



我有一个自定义属性,我想通过GlobalFilter检查我调用的方法是否有这个标记属性。我找不到一种方法来获取信息,即我的请求所指向的被调用方法具有Attribute。我已经找到了另一篇文章,它建议使用FilterAttributeCache.GetRequestFilterAttributes(request.GetType()),但这个方法和其他方法都没有重新调整任何元素。

你能帮我解决我在这里缺少的东西吗?

以下示例代码:

自定义属性:

using ServiceStack;
using ServiceStack.Web;
namespace MyProject.Web
{
public class CustomAttribute : RequestFilterAttribute
{
public string ServiceName;
public string ServiceClass;
public string ServiceMethod;
public JwtAuthAttribute(string serviceName, string serviceClass, string serviceMethod)
{
ServiceName = serviceName;
ServiceClass = serviceClass;
ServiceMethod = serviceMethod;
}
public override void Execute(IRequest req, IResponse res, object requestDto)
{
//Nothing in here
}
}
}

过滤器:

using System;
using System.Linq;
using ServiceStack.Support.WebHost;
using ServiceStack.Web;
namespace MyProject.Web
{
public class CustomFilter
{
private readonly ICustomManager _customManager;
public CustomFilter(ICustomManager customManager)
{
_customManager= customManager;
}
public void Execute(IRequest request)
{
var customHeader = request.Headers.GetValues("CustomHeader");
if (customHeader == null)
{
return;
}
var customAttribute = FilterAttributeCache.GetRequestFilterAttributes(request.GetType())
.OfType<CustomAttribute>().FirstOrDefault();
if (customAttribute == null)
{
return;
}
// Do other things here
}
}
}

GlobalFilter在Global.asax:中的注册

public override void Configure(Container container)
{
//other configurations

GlobalRequestFilters.Add((req, res, dto) => req.ResponseContentType = MimeTypes.Json);
GlobalRequestFilters.Add((req, res, dto) =>
{
var customFilter = new CustomFilter(request.TryResolve<ICustomManager>());
customFilter.Execute(req);
});
}

带有标记CustomAttribute:的方法

namespace MyProject.Web
{
[Authenticate]
[RequiredRole("CustomUser")]
public class CustomService : Service
{
[Custom("Service", "ServiceClass", "ServiceMethod")]
public object Get(CustomRequest request)
{
//Additional code
}   
}
}

我建议在请求DTO上添加更容易访问的属性,因为请求DTO在整个ServiceStack请求管道中随处可见,例如全局筛选器中的dto.GetType()或其他任何地方的IRequest.Dto.GetType()

要访问ServiceStack操作,您需要访问服务类型,然后访问其操作,例如:

GlobalRequestFilters.Add((req, res, dto) =>
{
var serviceType = HostContext.AppHost.Metadata.GetServiceTypeByRequest(dto.GetType());
var action = serviceType.GetActions().First(x => x.RequestType == dto.GetType());
var attrs = action.AllAttributes<CustomAttribute>();
});

mythz给出答案后,我解决了这个问题。我已经延长了";执行";方法";CustomFilter";通过requestDto,这是由GobalRequestFilter注册提供的。

不幸的是,我无法访问该房产";"请求类型";,可能是因为我使用的是旧版本(ServiceStack 5.9.2,.Net Framework 4.7.2(,所以我不得不复习MethodParameters。

这是最后一个代码,它让我可以访问";CustomAttribute">

var serviceTypeDto = HostContext.AppHost.Metadata.GetServiceTypeByRequest(requestDto.GetType());
var methodInfo = serviceTypeDto.GetDeclaredMethods().FirstOrDefault(x =>
x.GetParameters().FirstOrDefault(y => y.ParameterType == requestDto.GetType()) != null && x.IsPublic);
var customAttribute = methodInfo.AllAttributes<CustomAttribute>().FirstOrDefault();

最新更新