2011-11-07 26 views
2

最初の少しの背景:WebHttpEndpointを使用するWCF 4にはRESTサービスがあります。すべてのサービスメソッドまたはすべてのサービスクラスで明示的なエラーハンドラを持つのではなく、クライアントに渡すための素晴らしいカスタムメッセージをラップすることができる集中型のエラー処理が必要です。カスタムWebHttpBehaviorを追加できません:同じキーを持つ2つのアイテムを追加できません。

私は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" />  

への全体的なアプローチ場合エラー処理は悪い考えのように思えますが、そのことについてコメントしてください。

しかし、私の質問は、起動するES:

[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の動作のいずれかがそれ自体で存在することができますが、彼らは共存しないだろうと思われます。

答えて

2

はすでにWebHttpBehaviorです(これは<webHttp/>の設定要素に関連する動作です)。 <errorHandler>に関連付けられたビヘイビア拡張を更新して、WebHttpBehaviorに渡すパラメータを理解し、そのパラメータのみを持たせる必要があります。

+0

はい、意味があります。ありがとうございました! :) – kodbuse

+0

あなたが言っていることを取得しないでください – PositiveGuy

関連する問題