Set IncludeExceptionDetailInFaults to true in code for WCF

前端 未结 2 1691
囚心锁ツ
囚心锁ツ 2021-01-30 07:56

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

2条回答
  •  借酒劲吻你
    2021-01-30 08:46

    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();
    
    // 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.

提交回复
热议问题