如何测试IncludeExceptionDetailInFaults

时间:2015-11-13 19:27:50

标签: c# wcf attributes servicebehavior

我可以使用以下方法检查是否设置了行为:

Attribute.GetCustomAttribute(typeof(MyService), typeof(ServiceBehavior))

如何检查是否在ServiceBehavior属性中定义和设置了特定属性?例如IncludeExceptionDetailInFaults:

[ServiceBehavior(IncludeExceptionDetailInFaults = true)]

1 个答案:

答案 0 :(得分:0)

实际上它很简单,我需要转换属性,然后检查属性:

Attribute behavior = Attribute.GetCustomAttribute(myService.GetType(), typeof(ServiceBehaviorAttribute));
if (behavior != null)
{
    if (!((ServiceBehaviorAttribute)behavior).IncludeExceptionDetailInFaults)
    {
        throw new Exception(); // or whatever
    }
}

服务类似于:

[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class MyService
{ }

希望这有助于某人。