gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:07:18 25 4
gpt4 key购买 nike

我有一个使用 Json.NET (newtonsoft) 作为自定义序列化器的工作应用程序。目前,我正在自定义 WebServiceHostFactory 中添加 WebHttpBehavior 的衍生产品。请参阅 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 System.Configuration 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添加为元素。 BehaviourExtensionElement 告诉配置解析器对某个元素使用哪种类型。

这是我需要的实现:

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, NewtonsoftJsonExtensions, Version=1.0.0.0, Culture=neutral, 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, NewtonsoftJsonExtensions, Version=1.0.0.0, Culture=neutral, 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>

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

关于.net - 自定义行为不会在我的 web.config 中注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8082231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com