【问题标题】:Set IncludeExceptionDetailInFaults to true in code for WCF在 WCF 的代码中将 IncludeExceptionDetailInFaults 设置为 true
【发布时间】:2011-01-29 19:16:56
【问题描述】:

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

【问题讨论】:

    标签: c# wcf


    【解决方案1】:

    是的,当然 - 在服务器端,在您打开服务主机之前。但是,这将要求您自行托管 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 文件中。

    【讨论】:

    • 在本地命名管道 WCF 应用程序与正在运行的服务之间挽救了我的生命。谢谢!
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2023-03-02
      • 1970-01-01
      • 1970-01-01
      • 2011-01-11
      • 2019-08-11
      • 1970-01-01
      • 2011-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多