WCF IncludeExceptionDetailInFaults以编程方式?

时间:2010-11-30 23:22:56

标签: wcf binding fault

我在配置文件中有以下内容,我正在尝试在C#中找到等效位,因为我有一个完全以编程方式配置的服务。我应该寻找什么类/属性/方法?

感谢。

<behaviors>
    <serviceBehaviors>
        <behavior name="ServiceGatewayBehavior">
            <serviceMetadata httpGetEnabled="true"/>
            <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
    </serviceBehaviors>
</behaviors>

1 个答案:

答案 0 :(得分:35)

如果您想在所有情况下执行此操作,请使用ServiceBehaviorAttribute

   [ServiceBehavior(IncludeExceptionDetailInFaults=true)]
   class MyServiceImplementation : IMyService
   {
      /// ...
   }

如果您只想在某些情况下执行此操作,请在运行时确定....

////////////////////////////////////
// Must include these at the top of file
using System.ServiceModel;
using System.ServiceModel.Description;
// ...

/////////////////////////////////////////////////////////////
// Inside whichever function initializes the service host
//
_serviceHost = new ServiceHost(_service);
if (IWantToIncludeExceptionDetails())
{
    var behavior = _serviceHost.Description.Behaviors.Find<ServiceDebugBehavior>();
    behavior.IncludeExceptionDetailInFaults = true;
}
_serviceHost.Open();