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

Jos*_*osh 69 c# wcf

如何在不使用App.Config的情况下在代码中设置IncludeExceptionDetailInFaults?

mar*_*c_s 104

是的,确定 - 在服务器端,在打开服务主机之前.但是,这需要您自行托管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();
Run Code Online (Sandbox Code Playgroud)

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

  • 在本地命名管道WCF应用程序与运行服务上保存了我的生命.谢谢 ! (3认同)

小智 30

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

[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class MyClass:IMyService
{
...
}
Run Code Online (Sandbox Code Playgroud)