如果您想在所有情况下都这样做,请使用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();