WCF ServiceHost already has 5 behaviors
I´m creating a ServiceF开发者_如何转开发actory to gain control over inicialization of my services exposed through IIS 7.
However I´m surprised by the behavior of ServiceHost. Although I have 0 configuration files for the service, wherever I Initialize a new ServiceHost, like this:
var host = new ServiceHost(typeof(MyService), baseAddresses);
Next I want to add some behaviors only if the build is in Debug mode:
#if DEBUG
host.Description.Behaviors.Add(new ServiceDebugBehavior());
#endif
However this code fails cause the ServiceDebugBehavior is already applied! Despite I have no configuration files, and no attributes applied to the service class, the host already has this behavior and 5 more applied!
Is this the expected behavior? What if I want to disable the ServiceDebugBehavior on release builds?
Thanks in advance,
Not easily - no setting I'm aware of to just turn this off. Question really is: what benefit do you get from that??
From what I see, most of those behaviors are quite essential - authentication and service credentials and so forth. And if they're there by default, even without config, I would believe they're there for a reason.
But if you really really want to, you can always create your own CustomServiceHost
and do whatever you like inside that class - including tossing out all pre-defined behaviors, if you want to.
If you want to e.g. enable the IncludeExceptionDetailsInFaults
setting on the service debug behavior of your service, try this type of code:
ServiceDebugBehavior behavior =
host.Description.Behaviors.Find<ServiceDebugBehavior>();
if(behavior != null)
{
behavior.IncludeExceptionDetailInFaults = true;
}
else
{
host.Description.Behaviors.Add(
new ServiceDebugBehavior() { IncludeExceptionDetailInFaults = true });
}
In this case, if the ServiceDebugBehavior
is present already, you find it and just set the property to true - otherwise you create and add a new ServiceDebugBehavior
. Pretty easy, I think.
You shouldn't create the service debug behaviour inside the #if DEBUG, instead just set the values for the properties you want to change from the default.
精彩评论