自定义行为不会在我的web.config中注册 [英] Custom Behavior won't register in my web.config

查看:210
本文介绍了自定义行为不会在我的web.config中注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Json.NET(newtonsoft)作为自定义序列化程序的工作应用程序。目前我在自定义的WebServiceHostFactory中添加了WebHttpBehavior的衍生物。请参阅这个博客关于我如何附加它。

I have a working application using Json.NET (newtonsoft) as a custom serializer. Currently I'm adding this derivative of WebHttpBehavior in a custom WebServiceHostFactory. See the code snippet at the end of this blog for how I've attached it.

因为我在IIS中托管这项服务,我想摆脱我的自定义托管代码,只需将自定义行为添加到我的web.config。此过程显示在此 msdn文章中。

As I'm hosting this service in IIS, I would like to get rid of my custom hosting code and simply add the custom behavior to my web.config. The procedure is shown in this 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告诉我

Sadly, I'm unable to make that work. When I do that, Visual Studio tells me that


元素'behavior'具有无效的子元素'NewtonsoftJsonBehavior'

The element 'behavior' has invalid child element 'NewtonsoftJsonBehavior'

在上面提到的 msdn文章,据说


要为元素添加配置功能,您需要编写并注册配置元素。有关此内容的详细信息,请参阅 System.Configuration 文档。

在定义元素及其配置类型后,可以使用扩展名,如下例所示。

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

我有这种感觉,我所缺少的就是那个。以某种方式注册元素及其配置类型。可悲的是,我无法做出 System.Configuration 应该告诉我该怎么做。所以这基本上是我的问题:

I've got this feeling that what I'm missing is exactly that. Somehow registering the element and its configuration type. Sadly I can't make heads or tails of the System.Configuration which is supposed to tell me how to do this. So that's basically my question:

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

非常感谢提前!

推荐答案

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

The missing piece is the class BehaviorExtensionElement. In the OP I was attempting to add the WebHttpBehavior-derivative as an element. The BehaviorExtensionElement tells the config-parser which Type to use for a certain element.

这是我需要的实现:

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

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

这还不足以摆脱我的习惯当然是WebServiceHostFactory。因为我还必须添加一个自定义的ContentTypeMapper:

This wasn't sufficient to get rid of my custom WebServiceHostFactory, of course. For I also had to add a custom ContentTypeMapper:

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

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

I could then use them in my Web.config. Here are the relevant parts of the working config. Firstly setting up the extension and configuring a behavior with it:

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

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

Then configuring a webHttpBinding with my custom contentTypeMapper:

<bindings>
  <webHttpBinding>
    <binding name="newtonsoftJsonBinding" contentTypeMapper="Newtonsoft.Json.Extensions.NewtonsoftJsonContentTypeMapper, NewtonsoftJsonExtensions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
  </webHttpBinding>
</bindings>

最后使用以上方式设置端点:

Finally setting up an endpoint utilizing the above:

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

希望这些东西会帮助那里的人。 :)

Hope this stuff will help somebody out there. :)

这篇关于自定义行为不会在我的web.config中注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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