Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF ServiceHost already has 5 behaviors

Tags:

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,

like image 464
Iñaki Elcoro Avatar asked Feb 16 '10 13:02

Iñaki Elcoro


1 Answers

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.

like image 119
marc_s Avatar answered Oct 11 '22 13:10

marc_s