C# 在WCF的代码中将IncludeExceptionDetailInFaults设置为true

C# 在WCF的代码中将IncludeExceptionDetailInFaults设置为true,c#,wcf,C#,Wcf,如何在不使用App.Config的情况下在代码中设置IncludeExceptionDetailInFaults?是,当然-在服务器端,在打开服务主机之前。但是,这将要求您自行托管WCF服务-在IIS托管方案中不起作用: ServiceHost host = new ServiceHost(typeof(MyWCFService)); ServiceDebugBehavior debug = host.Description.Behaviors.Find<ServiceDebugBeha

如何在不使用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();
ServiceHost host=newservicehost(typeof(mywcfsservice));
ServiceDebugBehavior调试=host.Description.Behaviors.Find();
//如果未找到-在打开设置的情况下添加行为
if(debug==null)
{
host.Description.Behaviors.Add(
新的ServiceDebugBehavior(){IncludeExceptionDetailInFaults=true});
}
其他的
{  
//确保设置已打开
如果(!debug.IncludeExceptionDetailInFaults)
{
debug.IncludeExceptionDetailInFaults=true;
}
}
host.Open();

如果您需要在IIS主机中执行相同的操作,则必须创建自己的自定义
MyServiceHost
子代和合适的
MyServiceHostFactory
,以实例化此类自定义服务主机,并在*.svc文件中引用此自定义服务主机工厂。

您还可以在[ServiceBehavior]中进行设置在继承接口的类声明上方添加标记

[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class MyClass:IMyService
{
...
}

在本地命名管道WCF应用程序和运行服务上救了我的命。谢谢