【问题标题】:How to test for IncludeExceptionDetailInFaults如何测试 IncludeExceptionDetailInFaults
【发布时间】:2015-11-13 19:27:50
【问题描述】:
我可以检查是否设置了行为:
Attribute.GetCustomAttribute(typeof(MyService), typeof(ServiceBehavior))
如何检查是否在 ServiceBehavior 属性中定义和设置了特定属性?例如 IncludeExceptionDetailInFaults:
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
【问题讨论】:
标签:
c#
wcf
attributes
servicebehavior
【解决方案1】:
其实很简单,我需要转换属性,然后检查属性:
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
{ }
希望这对某人有所帮助。