在ServiceStack中动态添加属性 [英] Dynamically adding attributes in ServiceStack

查看:39
本文介绍了在ServiceStack中动态添加属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 AppHost 中添加限制"属性.这是我的代码:

I'm attempting to add a "Restrict" attribute in my AppHost. Here is my code:

var restrictAttribute = new RestrictAttribute { ExternalOnly = true };
foreach (var dto in dtos)
{
    dto .AddAttributes(restrictAttribute);
}

我将它们添加到的 DTO 是专门用于 POST 请求的 DTO.

The DTOs I'm adding them to are ones specifically for POST requests.

我面临的问题是动态添加属性后,限制的 ServiceStack 功能不起作用.它确实添加了属性,但实际上并没有限制任何东西.

The problem I'm facing is that after adding the attributes dynamically, the ServiceStack functionality for the restrict doesn't work. It DOES add the attribute, but doesn't actually restrict anything.

我可以完成这项工作的唯一方法是在请求 DTO 中手动添加限制属性.我在这里做错了吗?

The only way I can make this work is by adding the Restrict Attribute in the Request DTO manually. Am I doing something wrong here?

推荐答案

对于动态添加服务属性,您需要在 AppHost.Configure() 之前添加它们,因为它们在时间之前已经初始化Configure() 已运行,因此需要在 AppHost 构造函数中或在调用 AppHost.Init() 之前添加它们.

For dynamically adding Service Attributes you need to add them before AppHost.Configure() since they're already initialized by the time Configure() is run, so they need to be either added in AppHost constructor or before AppHost.Init() is called.

这篇关于在ServiceStack中动态添加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆