24

I know I can add service behaviors with some XML configuration, but I'd like to do it with a piece of C#, similar how you can add endpoint behaviors. I'm not sure how to do that, though.

In other words, how can I add the debug behavior I've instantiated below?

var host = new ServiceHost(typeof(MyService));
var endpoint = host.AddServiceEndpoint(typeof (MysService), 
    new WebHttpBinding(), new Uri(myURL));
endpoint.Behaviors.Add(new WebHttpBehavior());
var debug = new ServiceDebugBehavior
{
    IncludeExceptionDetailInFaults = true
};
//WHAT DO I PUT HERE?
host.Open();

2 Answers 2

35
host.Description.Behaviors.Add(debug);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! It turns out I couldn't do .Add() because that behavior is already there. But this worked great: host.Description.Behaviors.Find<ServiceDebugBehavior>().IncludeExceptionDetailInFaults = true
What if this behavior is defined in confituration, but service in the code? How to add it to WCF host with code?
4

Also you can write an atrribute for your service e.g.

[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class Service1 : IServiceChild

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.