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

查看:94
本文介绍了在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天全站免登陆