无法添加自定义 WebHttpBehavior:无法添加具有相同键的两个项目

发布于 2024-12-14 04:58:35 字数 2846 浏览 3 评论 0原文

首先介绍一点背景知识:我在 WCF 4 中有一个使用 WebHttpEndpoint 的 REST 服务。我不想在每个服务方法甚至每个服务类中都有显式的错误处理程序,而是希望有一个集中的错误处理程序来进行日志记录并能够包装一个很好的自定义消息以传递给客户端。

我试图通过实现 IErrorHandler 并添加客户 WebHttpBehavior 来做到这一点:

public class ErrorHandlerBehavior : WebHttpBehavior
{
    protected override void AddServerErrorHandlers(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
    {
        base.AddServerErrorHandlers(endpoint, endpointDispatcher);
    }
}

然后我使用 ExtensionElement 添加它:

<behaviors>
    <endpointBehaviors>
        <behavior>
            <authenticationInspector />
            <authorizationInspector />    
            <errorHandler />   
            <webHttp
                defaultBodyStyle="Bare"
                defaultOutgoingResponseFormat="Json"
                helpEnabled="true"  />      

如果错误处理的整个方法看起来不是一个好主意,请随意对此发表评论...

但是,我的问题是为什么当服务尝试启动时出现此异常:

[ArgumentException: Cannot add two items with the same key to SynchronizedKeyedCollection.]
   System.Collections.Generic.SynchronizedKeyedCollection`2.AddKey(K key, T item) +12277986
   System.Collections.Generic.SynchronizedKeyedCollection`2.InsertItem(Int32 index, T item) +38
   System.ServiceModel.Dispatcher.OperationCollection.InsertItem(Int32 index, DispatchOperation item) +53
   System.Collections.Generic.SynchronizedCollection`1.Add(T item) +78
   System.ServiceModel.Description.WebHttpBehavior.ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) +2498
   System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription description, ServiceHostBase serviceHost) +4275
   System.ServiceModel.ServiceHostBase.InitializeRuntime() +60
   System.ServiceModel.ServiceHostBase.OnBeginOpen() +27
   System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout) +50
   System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +318
   System.ServiceModel.HostingManager.ActivateService(String normalizedVirtualPath) +206
   System.ServiceModel.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath) +651

[ServiceActivationException: The service '/api/Errors' cannot be activated due to an exception during compilation.  The exception message is: Cannot add two items with the same key to SynchronizedKeyedCollection..]
   System.Runtime.AsyncResult.End(IAsyncResult result) +688590
   System.ServiceModel.Activation.HostedHttpRequestAsyncResult.End(IAsyncResult result) +190

System.ServiceModel.Activation.AspNetRouteServiceHttpHandler.EndProcessRequest(IAsyncResult result) +6
System.Web.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult ar) +96

看来 webHttp 或 errorHandler 行为可以单独存在,但它们不会共存。

First a little bit of background: I have a REST service in WCF 4 that uses a WebHttpEndpoint. Rather than having explicit error handler in every service method, or even every service class, I'd like to have a centralized error handling that does logging and is able to wrap a nice custom message to pass up to the client.

I am attempting to do this by implementing IErrorHandler and adding that with a customer WebHttpBehavior:

public class ErrorHandlerBehavior : WebHttpBehavior
{
    protected override void AddServerErrorHandlers(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
    {
        base.AddServerErrorHandlers(endpoint, endpointDispatcher);
    }
}

Then I'm adding that using an ExtensionElement:

<behaviors>
    <endpointBehaviors>
        <behavior>
            <authenticationInspector />
            <authorizationInspector />    
            <errorHandler />   
            <webHttp
                defaultBodyStyle="Bare"
                defaultOutgoingResponseFormat="Json"
                helpEnabled="true"  />      

If the whole approach to error handling seems like a bad idea, feel free to comment on that...

However, my question is why I'm getting this exception when the service tries to start:

[ArgumentException: Cannot add two items with the same key to SynchronizedKeyedCollection.]
   System.Collections.Generic.SynchronizedKeyedCollection`2.AddKey(K key, T item) +12277986
   System.Collections.Generic.SynchronizedKeyedCollection`2.InsertItem(Int32 index, T item) +38
   System.ServiceModel.Dispatcher.OperationCollection.InsertItem(Int32 index, DispatchOperation item) +53
   System.Collections.Generic.SynchronizedCollection`1.Add(T item) +78
   System.ServiceModel.Description.WebHttpBehavior.ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) +2498
   System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription description, ServiceHostBase serviceHost) +4275
   System.ServiceModel.ServiceHostBase.InitializeRuntime() +60
   System.ServiceModel.ServiceHostBase.OnBeginOpen() +27
   System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout) +50
   System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +318
   System.ServiceModel.HostingManager.ActivateService(String normalizedVirtualPath) +206
   System.ServiceModel.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath) +651

[ServiceActivationException: The service '/api/Errors' cannot be activated due to an exception during compilation.  The exception message is: Cannot add two items with the same key to SynchronizedKeyedCollection..]
   System.Runtime.AsyncResult.End(IAsyncResult result) +688590
   System.ServiceModel.Activation.HostedHttpRequestAsyncResult.End(IAsyncResult result) +190

System.ServiceModel.Activation.AspNetRouteServiceHttpHandler.EndProcessRequest(IAsyncResult result) +6
System.Web.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult ar) +96

It appears that either the webHttp or errorHandler behavior can exist by itself, but they won't coexist.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

好菇凉咱不稀罕他 2024-12-21 04:58:35

您的 已经是一个 WebHttpBehavior (这是与 配置元素关联的行为)。您应该更新与 关联的行为扩展,以了解要传递给 WebHttpBehavior 的参数,并且只包含该参数。

Your <errorHandler> already is-a WebHttpBehavior (which is the behavior associated with the <webHttp/> config element). You should update your behavior extension associated with <errorHandler> to understand the parameters you want to pass to the WebHttpBehavior, and have only that one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文