【发布时间】:2011-01-29 19:16:56
【问题描述】:
如何在不使用 App.Config 的情况下在代码中设置 IncludeExceptionDetailInFaults?
【问题讨论】:
如何在不使用 App.Config 的情况下在代码中设置 IncludeExceptionDetailInFaults?
【问题讨论】:
是的,当然 - 在服务器端,在您打开服务主机之前。但是,这将要求您自行托管 WCF 服务 - 在 IIS 托管方案中不起作用:
ServiceHost host = new ServiceHost(typeof(MyWCFService));
ServiceDebugBehavior debug = host.Description.Behaviors.Find<ServiceDebugBehavior>();
// if not found - add behavior with setting turned on
if (debug == null)
{
host.Description.Behaviors.Add(
new ServiceDebugBehavior() { IncludeExceptionDetailInFaults = true });
}
else
{
// make sure setting is turned ON
if (!debug.IncludeExceptionDetailInFaults)
{
debug.IncludeExceptionDetailInFaults = true;
}
}
host.Open();
如果您需要在 IIS 托管中做同样的事情,您必须创建自己的自定义 MyServiceHost 后代和合适的 MyServiceHostFactory 来实例化此类自定义服务主机,并引用此自定义服务主机工厂在您的 *.svc 文件中。
【讨论】:
您也可以在继承接口的类声明上方的 [ServiceBehavior] 标记中进行设置
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class MyClass:IMyService
{
...
}
【讨论】: