问题描述
如何在代码中设置IncludeExceptionDetailInFaults不使用App.Config中
How do I set IncludeExceptionDetailInFaults in code without using App.Config?
推荐答案
是的,当然 - 在服务器端,您打开服务主机之前。然而这将需要您自托管的WCF服务 - 不会在IIS托管方案工作:
Yes, sure - on the server side, before you open the service host. This would however require that you self-host the WCF service - won't work in IIS hosting scenarios:
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文件。
If you need to do the same thing in IIS hosting, you'll have to create your own custom MyServiceHost
descendant and a suitable MyServiceHostFactory
that would instantiate such a custom service host, and reference this custom service host factory in your *.svc file.
这篇关于设置IncludeExceptionDetailInFaults为true在WCF代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!