2

最初に少し背景を説明します。WebHttpEndpoint を使用する WCF 4 の 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 の動作は単独で存在できるようですが、共存することはありません。

4

1 に答える 1

2

すでに<errorHandler>is-a (これはconfig 要素WebHttpBehaviorに関連付けられた動作です)。WebHttpBehavior に渡すパラメーターを理解するために、<webHttp/>関連付けられている動作拡張機能を更新し、そのパラメーターのみを使用する必要があります。<errorHandler>

于 2011-11-07T22:39:57.093 に答える