简体   繁体   English

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

[英]Custom Behavior won't register in my web.config

I have a working application using Json.NET (newtonsoft) as a custom serializer. 我有一个使用Json.NET(newtonsoft)作为自定义序列化程序的工作应用程序。 Currently I'm adding this derivative of WebHttpBehavior in a custom WebServiceHostFactory. 目前,我在自定义的WebServiceHostFactory中添加了WebHttpBehavior的衍生版本。 See the code snippet at the end of this blog for how I've attached it. 请参阅本博客末尾的代码段,了解我是如何附加它的。

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. 由于我在IIS中托管此服务,我想摆脱我的自定义托管代码,只需将自定义行为添加到我的web.config。 The procedure is shown in this msdn article . 该程序显示在这篇msdn文章中

So I try to do that like so: 所以我试着这样做:

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

Sadly, I'm unable to make that work. 可悲的是,我无法做那项工作。 When I do that, Visual Studio tells me that 当我这样做时,Visual Studio告诉我

The element 'behavior' has invalid child element 'NewtonsoftJsonBehavior' 元素'behavior'具有无效的子元素'NewtonsoftJsonBehavior'

In the afforementioned msdn article , it's said that 在上面提到的msdn文章中 ,据说是这样的

To add configuration abilities to the element, you need to write and register a configuration element. 要向元素添加配置功能,您需要编写和注册配置元素。 For more information on this, see the System.Configuration documentation. 有关此内容的更多信息,请参阅System.Configuration文档。

After the element and its configuration type are defined, the extension can be used, as shown in the following example. 定义元素及其配置类型后,可以使用扩展名,如以下示例所示。

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. 可悲的是,我无法对System.Configuration做出正面或反面,它应该告诉我如何做到这一点。 So that's basically my question: 所以这基本上是我的问题:

How do I write and register the configuration element, and if that's not my problem, what is the problem? 如何编写和注册配置元素,如果这不是我的问题,那么问题是什么?

Many thanks in advance! 提前谢谢了!

The missing piece is the class BehaviorExtensionElement. 缺少的部分是BehaviorExtensionElement类。 In the OP I was attempting to add the WebHttpBehavior-derivative as an element. 在OP中,我试图将WebHttpBehavior-derivative添加为元素。 The BehaviorExtensionElement tells the config-parser which Type to use for a certain element. BehaviorExtensionElement告诉config-parser哪个Type用于某个元素。

Here's the implementation I needed: 这是我需要的实现:

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

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

This wasn't sufficient to get rid of my custom WebServiceHostFactory, of course. 当然,这还不足以摆脱我的自定义WebServiceHostFactory。 For I also had to add a custom ContentTypeMapper: 因为我还必须添加一个自定义的ContentTypeMapper:

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

I could then use them in my Web.config. 然后我可以在我的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>

Then configuring a webHttpBinding with my custom contentTypeMapper: 然后使用我的自定义contentTypeMapper配置webHttpBinding:

<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. 希望这些东西会帮助那里的人。 :) :)

If you open this app.config with svcconfigeditor, it should start asking to select the assembly associated with the NewtonsoftJsonBehavior. 如果使用svcconfigeditor打开此app.config,它应该开始要求选择与NewtonsoftJsonBehavior关联的程序集。 If you select that, and save the app.config again, does that resolve your issue? 如果您选择该项,然后再次保存app.config,是否可以解决您的问题?

BTW, we also use a custom binding extension in our service configurations. 顺便说一句,我们还在服务配置中使用自定义绑定扩展。 The xml in the config always underlines the extension with a blue wave indicating that the element is not know to the schema as you describe it. 配置中的xml始终使用蓝色波浪强调扩展名,表示在您描述时该元素不为架构所知。 However, when we start the service, both the service and the behavior function as expected. 但是,当我们启动服务时,服务和行为都按预期运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WCF:在我的app.config / web.config中使用我的“自定义”ServiceHost来获取我的wcf服务? - WCF: Using my “custom” ServiceHost in my app.config/web.config for my wcf service? 无法阻止web.config继承 - can't stop web.config inheritance WCF:找不到我在web.config中指定的自定义验证器 - customUserNamePasswordValidatorType - - 无法加载文件或程序集...... - 帮助? - WCF: Cannot find my custom validator specified in web.config - customUserNamePasswordValidatorType - - Could not load file or assembly … - help? 如果我没有“如何诊断WCF请求错误” <behaviors> “我的Web.Config中的节点? - How to diagnose WCF Request errors if I don't have a “<behaviors>” node in my Web.Config? 删除子应用程序的web.config中的自定义绑定元素 - Removing custom binding elements in web.config for child application Sharepoint 2013自定义服务与单独的web.config文件? - Sharepoint 2013 custom service with separate web.config file? 如何在服务的 web.config 中添加自定义 EndPointBehavior? - How to add a Custom EndPointBehavior to the web.config of the service? WCFExtras web.config指向(我的)外部服务 - WCFExtras web.config pointing (my) external service 使用wsHttpBinding,WCF忽略了我的web.config - Using wsHttpBinding, WCF is ignoring my web.config WCF-一个OperationContract看到Web.config,另一个没有 - WCF - One OperationContract sees Web.config, another doesn't
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM