Josh
Josh

Reputation: 13818

Set IncludeExceptionDetailInFaults to true in code for WCF

How do I set IncludeExceptionDetailInFaults in code without using App.Config?

Upvotes: 75

Views: 54970

Answers (3)

Benni
Benni

Reputation: 280

This worked for me to include exception details:

            builder.AddService<CustomerService>((serviceOptions) =>
            {
                serviceOptions.BaseAddresses.Add(new Uri($"http://{HOST_IN_WSDL}/CustomerService.svc"));
                //serviceOptions.BaseAddresses.Add(new Uri($"https://{HOST_IN_WSDL}/CustomerService"));
                serviceOptions.DebugBehavior.IncludeExceptionDetailInFaults = true;
            })

            .AddServiceEndpoint<CustomerService, ICustomerService>(myWSHttpBinding, "");

Upvotes: 2

LievenV
LievenV

Reputation: 811

You can also set it in the [ServiceBehavior] tag above your class declaration that inherits the interface

[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class MyClass:IMyService
{
...
}

Upvotes: 36

marc_s
marc_s

Reputation: 754598

Yes, sure - on the server side, before you open the service host. This would however require that you self-host the WCF service - won't work in IIS hosting scenarios:

ServiceHost host = new ServiceHost(typeof(MyWCFService));

ServiceDebugBehavior debug = host.Description.Behaviors.Find<ServiceDebugBehavior>();

// if not found - add behavior with setting turned on 
if (debug == null)
{
    host.Description.Behaviors.Add(
         new ServiceDebugBehavior() { IncludeExceptionDetailInFaults = true });
}
else
{  
    // make sure setting is turned ON
    if (!debug.IncludeExceptionDetailInFaults)
    {
        debug.IncludeExceptionDetailInFaults = true;
    }
}

host.Open();

If you need to do the same thing in IIS hosting, you'll have to create your own custom MyServiceHost descendant and a suitable MyServiceHostFactory that would instantiate such a custom service host, and reference this custom service host factory in your *.svc file.

Upvotes: 110

Related Questions