21

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

谢谢。

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

1 回答 1

38

如果您想在所有情况下都这样做,请使用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();
于 2010-12-01T00:07:46.943 回答