开发者

Custom Endpoint Behavior using Standard webHttpEndpoint

开发者 https://www.devze.com 2023-02-22 21:16 出处:网络
We currently have a working REST WCF service.I\'m trying to implement a custom behavior for error handling that will return a custom JSON object.I believe the article found here WCF Exception Handling

We currently have a working REST WCF service. I'm trying to implement a custom behavior for error handling that will return a custom JSON object. I believe the article found here WCF Exception Handling with IErrorHandler will do the trick, however, the example shows how to add the behavior via code. We're hosting the service via IIS and have no ServiceHost. Can anyone direct me as to how to add my custom error handling class to the web.config?

I've searched all over Google and SO and have found a number of examples that are doing close to 开发者_StackOverflowwhat I'm trying to accomplish but my understanding of WCF is probably prohibiting me from fully comprehending.

Here is all we have configured for the endpoints.

<standardEndpoints>
  <webHttpEndpoint>
    <!-- 
        Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
        via the attributes on the <standardEndpoint> element below
    -->
    <standardEndpoint helpEnabled="true" automaticFormatSelectionEnabled="false"/>
  </webHttpEndpoint>
</standardEndpoints>
<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"/>
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>        
   </serviceBehaviors>
</behaviors>


Unfortunately, you will have to write some code. The trick is to put the code in a class library and then configure web.config to use that code.

Copy the ErrorHandlerServiceBehavior class from the example you're using, compile it into your WCF project.

Create a behavior extension:

<extensions>
    <behaviorExtensions>
        <add name="myBehavior" type="MyLibrary.ErrorHandlerServiceBehavior, MyLibrary,
            Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
   </behaviorExtensions>
</extensions>

Apply the behavior extension to a behavior configuration:

<behaviors>
    <behavior configurationName="testBehaviorConfiguration">
        <myBehavior />
    </behavior>
</behaviors>

Apply the behavior configuration to the service:

<services>
    <service name="MyLibrary.MyService"   
      behaviorConfiguration="testBehaviorConfiguration">
        <endpoint binding="basicHttpBinding" 
          contract="MyLibrary.IMyService"/>
    </service>
</services>
0

精彩评论

暂无评论...
验证码 换一张
取 消