2010-11-30 51 views
17

我在配置文件中有以下內容,我試圖在C#中找到相應的位,因爲我有一個完全以編程方式配置的服務。我應該尋找什麼類/財產/方法?WCF IncludeExceptionDetailInFaults編程?

謝謝。

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

回答

31

如果你想這樣做,在所有情況下,使用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();