加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

wcf – 自定义行为不会在我的web.config中注册

发布时间:2020-12-14 21:49:09 所属栏目:资源 来源:网络整理
导读:我有一个使用Json.NET(newtonsoft)作为自定义序列化程序的工作应用程序。目前,我将WebHttpBehavior的派生类添加到自定义的WebServiceHostFactory中。请参阅 this blog年底的代码段,了解我如何附加它。 当我在IIS中托管这个服务时,我想摆脱我的自定义托管
我有一个使用Json.NET(newtonsoft)作为自定义序列化程序的工作应用程序。目前,我将WebHttpBehavior的派生类添加到自定义的WebServiceHostFactory中。请参阅 this blog年底的代码段,了解我如何附加它。

当我在IIS中托管这个服务时,我想摆脱我的自定义托管代码,只需将自定义行为添加到我的web.config中。程序如msdn article所示。

所以我试着这样做:

<behaviors>
  <endpointBehaviors>
    <behavior name="jsonRest">
      <webHttp defaultOutgoingResponseFormat="Json" />
      <NewtonsoftJsonBehavior/>
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<extensions>
  <behaviorExtensions>
    <add name="NewtonsoftJsonBehavior" type="Newtonsoft.Json.Extensions.NewtonsoftJsonBehavior,NewtonsoftJsonExtensions,Version=0.0.0.0,Culture=neutral,PublicKeyToken=null" />
  </behaviorExtensions>
</extensions>

可悲的是,我无法做这项工作。当我这样做,Visual Studio告诉我

The element ‘behavior’ has invalid child element ‘NewtonsoftJsonBehavior’

在上述的msdn article,据说

To add configuration abilities to the element,you need to write and register a configuration element. For more information on this,see the 07003 documentation.

After the element and its configuration type are defined,the extension can be used,as shown in the following example.

我有这种感觉,我所缺少的就是这样。不知何故注册元素及其配置类型。可悲的是,我不能使System.Configuration的头或尾,这应该告诉我如何做到这一点。这基本上是我的问题:

如何编写和注册配置元素,如果这不是我的问题,有什么问题?

提前谢谢了!

解决方法

缺少的部分是BehaviorExtensionElement类。在OP中,我试图添加WebHttpBehavior-derivative作为元素。 BehaviorExtensionElement告诉config-parser用于某个元素的Type。

这是我需要的实现:

public class NewtonsoftJsonBehaviorExtension : BehaviorExtensionElement
{
    public override Type BehaviorType
    {
        get { return typeof(NewtonsoftJsonBehavior); }
    }

    protected override object CreateBehavior()
    {
        return new NewtonsoftJsonBehavior();
    }
}

这当然不足以摆脱我自定义的WebServiceHostFactory。因为我也不得不添加一个自定义的ContentTypeMapper:

public class NewtonsoftJsonContentTypeMapper : WebContentTypeMapper
{
    public override WebContentFormat GetMessageFormatForContentType(string contentType)
    {
        return WebContentFormat.Raw;
    }
}

我可以在我的Web.config中使用它们。这是工作配置的相关部分。首先设置扩展并配置一个行为:

<extensions>
  <behaviorExtensions>
    <add name="newtonsoftJsonBehavior" type="Newtonsoft.Json.Extensions.NewtonsoftJsonBehaviorExtension,Version=1.0.0.0,PublicKeyToken=null" />
  </behaviorExtensions>
</extensions>
<behaviors>
  <endpointBehaviors>
    <behavior name="jsonRestEndpointBehavior">
      <webHttp/>
      <newtonsoftJsonBehavior/>
    </behavior>
  </endpointBehaviors>
<behaviors>

然后使用自定义contentTypeMapper配置webHttpBinding:

<bindings>
  <webHttpBinding>
    <binding name="newtonsoftJsonBinding" contentTypeMapper="Newtonsoft.Json.Extensions.NewtonsoftJsonContentTypeMapper,PublicKeyToken=null" />
  </webHttpBinding>
</bindings>

最后建立一个使用上述的端点:

<services>
  <service name="My.Namespaced.MyService" behaviorConfiguration="jsonRestServiceBehavior">
    <endpoint address=""                behaviorConfiguration="jsonRestEndpointBehavior"
              binding="webHttpBinding"  bindingConfiguration="newtonsoftJsonBinding" 
              contract="My.Namespaced.IMyService" />
  </service>
</services>

希望这个东西会帮助有人在那里。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读