tags:

views:

110

answers:

2

Hi,

I´m creating a ServiceFactory 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,

+2  A: 

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.

marc_s
Weel I have no complains about the behaviors that you say, like authentication and service credentials.However I think is weird to include DebugBehavior in the bag. I do not want to expose this behavior on my production services. Also is no that the point of having a DebugBehavior at all? It allows you to plug it when makes sense.So is this the predefined behavior of WCF?
iCe
Yes, but the `IncludeExceptionDetailsInFaults` setting on the `ServiceDebugBehavior` is **turned off** by default! On purpose, too - which is a good thing!
marc_s
A: 

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.

Pablote