wcf - Debug to enable Server exception in Fault message

3 篇文章 0 订阅

WCF will be able to send back the status of server and client erroneous condition in Fault messages. However, due to the security consideration, it is commonly disabled on the server side so that normally you are not able to view the server internal information direclty (suppose there are some critical messgaes in the server fault message)..

however, in debug mode, you want to expose the message to the client so that they can know what might go wrong...

You can do that either via the Service Configuration behavior or you may need to have to call in code to enable this behavior..


Enable ServiceDebugBehavior in code programmatically

 

            using (ServiceHost host = new ServiceHost(typeof(TabularPushService)))
            {
                host.AddServiceEndpoint(
                    typeof(ITabularPushService),
                    new NetTcpBinding(),
                    string.Format("net.tcp://127.0.0.1:{0}/TabularPushService", arguments.Port));
#if DEBUG

                ((ServiceDebugBehavior)host.Description.Behaviors[typeof(ServiceDebugBehavior)]).IncludeExceptionDetailInFaults = true;

#endif 
                //...
              }

 

Add the ServiceBehavior on ServiceContract parameter.

    [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    public class TabularPushService : ITabularPushService
    {
     // ...
    }

 

As you can see, you can decorate the service in question with the ServiceBehavior attribute, and then you can supply an value to indicate that IncludeExceptionDetailInFault in the message. 


Configure IncludeExceptionDetailInFault via config

In the Sercice endpoint configuraiotn, you can do this: 

<?xml version="1.0"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
  <system.serviceModel>

    <!-- include more detailed debug information
    code snippet from :  http://stackoverflow.com/questions/5076534/implementing-wcf-using-nettcp-protocol
    -->
    <behaviors>
      <serviceBehaviors>
        <behavior name="standard">
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    
       <!-- Port will be programmtically determined -->
      <!-- TabularPushService -->
      <service name="WcfPub.TabularPushService">
        <endpoint address="net.tcp://127.0.0.1:9999/TabularPushService" binding="netTcpBinding" contract="WcfPub.ITabularPushService" behaviorConfiguration="standard"/>
      </service>
    </services>
    
    
  </system.serviceModel>

</configuration>

 as you can see, the most important part to enable the behavior is the "behaviorConfiguration" attribute, which points to the name of the behavior configuration.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值