How to setup up web.config for WCF IErrorhandler

别说谁变了你拦得住时间么 提交于 2019-12-03 05:52:44
<system.serviceModel>
  <behaviors>
    <serviceBehaviors>
      <behavior>
        <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
        <serviceMetadata httpGetEnabled="true" />
        <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
        <serviceDebug includeExceptionDetailInFaults="true" />
        <errorHandler/>
      </behavior>
    </serviceBehaviors>
  </behaviors>
  <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  <extensions>
    <behaviorExtensions>
      <add name="errorHandler" type="CustomerWcfService.WcfErrorHandlerBehaviour, CustomerWcfService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
    </behaviorExtensions>
  </extensions>
</system.serviceModel>

And then apply your behavior to the service you want it to be applied to.

Edit :

sorry for the miss, but you actually need to remove any line-break and any additional white space in the type name in the extension definition (old WCF bug which force you to use the fully qualified name string in extension type declaration).

It's a bit late, but for other users here is a better approach (in my opinion) and the code above only has to be changed a little bit

You can change:

    public class WcfErrorServiceBehaviour : IServiceBehavior

to:

    public class WcfErrorServiceBehaviourAttribute : Attribute, IServiceBehavior

Now you can use this as an attribute for your service-class like this:

    [WcfErrorServiceBehaviour]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall,
                     ConcurrencyMode = ConcurrencyMode.Multiple)]
    public class MXServiceCommands : IMXServiceCommands
    {
    }

Hope this helps some others.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!