如何在不使用App.Config的情况下在代码中设置IncludeExceptionDetailInFaults?
发布于 2010-03-20 22:31:24
是的,当然-在服务器端,在你打开服务主机之前。然而,这将要求您自托管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文件中引用此自定义服务主机工厂。
发布于 2014-10-10 21:38:01
也可以在继承接口的类声明上方的ServiceBehavior标记中设置它
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class MyClass:IMyService
{
...
}
https://stackoverflow.com/questions/2483178
复制相似问题