2011-11-07 4 views
2

처음 약간의 배경 : WebHttpEndpoint를 사용하는 WCF 4에 REST 서비스가 있습니다. 모든 서비스 메소드 나 모든 서비스 클래스에서 명시 적 오류 처리기를 사용하는 대신 로깅을 수행하고 클라이언트에 전달할 멋진 사용자 정의 메시지를 래핑 할 수있는 중앙 집중식 오류 처리를하고 싶습니다. 내가 IErrorHandler를 구현하고 추가하여이 작업을 수행 할 시도하고사용자 지정 WebHttpBehavior를 추가 할 수 없습니다. 동일한 키를 가진 두 항목을 추가 할 수 없습니다.

그 고객 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 동작 자체가 존재할 수 있지만 공존하지 않는 것으로 보입니다.

답변

2

귀하의 <errorHandler>은 이미 WebHttpBehavior (이것은 <webHttp/> 구성 요소와 관련된 동작입니다)입니다. <errorHandler>과 연결된 비헤이비어 확장을 업데이트하여 WebHttpBehavior에 전달할 매개 변수를 이해하고 해당 매개 변수 만 가져야합니다.

+0

예, 맞습니다. 고맙습니다! :) – kodbuse

+0

당신이 말하는 것을 얻지 못합니다. – PositiveGuy

관련 문제