使用标准webHttpEndpoint自定义端点行为 [英] Custom Endpoint Behavior using Standard webHttpEndpoint

查看:1536
本文介绍了使用标准webHttpEndpoint自定义端点行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我们有一个工作REST WCF服务。我想实现一个自定义的行为的错误处理,将返回一个自定义的JSON对象。我相信在这里发现文中的 WCF异常处理IErrorHandler 会做的伎俩,然而,这个例子说明如何添加通过code行为。我们主办通过IIS服务,没有任何的ServiceHost。任何人都可以直接我就如何我的自定义错误处理类添加到Web.config?

我已经找遍了谷歌和SO,发现了一些在做接近我想要完成,但我的WCF的理解可能是禁止我从完全融为一体prehending例子。

下面是所有我们已经配置了终点。

 < standardEndpoints>
  < webHttpEndpoint>
    <! - 
        通过的global.asax.cs文件和默认端点配置WCF REST服务的基址
        经由上的&lt属性; standardEndpoint>下面元
     - >
    < standardEndpoint helpEnabled =真automaticFormatSelectionEnabled =FALSE/>
  < / webHttpEndpoint>
< / standardEndpoints>
<行为>
  < serviceBehaviors>
    <行为>
      <! - 为了避免泄露的元数据信息,下面设置为false的价值和部署之前删除上面的元数据终结点 - >
      < serviceMetadata httpGetEnabled =真/>
      < serviceDebug includeExceptionDetailInFaults =真/>
    < /行为>
   < / serviceBehaviors>
< /行为>
 

解决方案

不幸的是,你的将会的必须写一些code。诀窍是把code在一个类库,然后配置web.config中使用code。

复制 ErrorHandlerServiceBehavior 从你使用的示例类,编译到您的WCF项目。

创建行为扩展:

 <扩展>
    < behaviorExtensions>
        <添加名称=myBehaviorTYPE =MyLibrary.ErrorHandlerServiceBehavior,MyLibrary的,
            版本= 1.0.0.0,文化=中性公钥=空/>
   < / behaviorExtensions>
< /扩展>
 

应用行为扩展到行为的配置:

 <行为>
    <行为configurationName =testBehaviorConfiguration>
        < myBehavior />
    < /行为>
< /行为>
 

应用行为的配置到服务:

 <服务>
    <服务名称=MyLibrary.MyService
      behaviorConfiguration =testBehaviorConfiguration>
        <端点绑定=basicHttpBinding的
          合同=MyLibrary.IMyService/>
    < /服务>
< /服务>
 

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 what 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>

这篇关于使用标准webHttpEndpoint自定义端点行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆