2011-04-07 5 views
1

WCF 이중 설정에서 클라이언트의 콜백 메서드에서 throw 된 예외를 처리하려면 어떻게해야합니까?WCF Duplex : 양방향 콜백에서 throw 된 예외를 처리하는 방법

현재 클라이언트가 잘못 모니터링하지 않는 한 오류가있는 이벤트가 발생하지 않지만 클라이언트를 사용하는 Ping() 호출 후 CommunicationException으로 인해 실패합니다. "통신 개체, System.ServiceModel .Channels.ServiceChannel은 중단되었으므로 통신에 사용할 수 없습니다. "

어떻게 처리하고 클라이언트 등을 다시 만들까요? 나의 첫 번째 질문은 그것이 일어나는 때를 발견하는 방법이다. 둘째, 어떻게 처리하는 것이 가장 좋습니까?

내 서비스 및 콜백 계약 :

[ServiceContract(CallbackContract = typeof(ICallback), SessionMode = SessionMode.Required)] 
public interface IService 
{ 
    [OperationContract] 
    bool Ping(); 
} 

public interface ICallback 
{ 
    [OperationContract(IsOneWay = true)] 
    void Pong(); 
} 

내 서버 구현 :

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Single)] 
public class Service : IService 
{ 
    public bool Ping() 
    { 
     var remoteMachine = OperationContext.Current.GetCallbackChannel<ICallback>(); 

     remoteMachine.Pong(); 
    } 
} 

내 클라이언트 구현 : 그것이 나오는 것에 따라

[CallbackBehavior(UseSynchronizationContext = false, ConcurrencyMode = ConcurrencyMode.Single)] 
public class Client : ICallback 
{ 
    public Client() 
    { 
     var context = new InstanceContext(this); 
     var proxy = new WcfDuplexProxy<IApplicationService>(context); 

     (proxy as ICommunicationObject).Faulted += new EventHandler(proxy_Faulted); 

     //First Ping will call the Pong callback. The exception is thrown 
     proxy.ServiceChannel.Ping(); 
     //Second Ping call fails as the client is in Aborted state 
     try 
     { 
      proxy.ServiceChannel.Ping(); 
     } 
     catch (Exception) 
     { 
      //CommunicationException here 
      throw; 
     } 
    } 
    void Pong() 
    { 
     throw new Exception(); 
    } 

    //These event handlers never get called 
    void proxy_Faulted(object sender, EventArgs e) 
    { 
     Console.WriteLine("client faulted proxy_Faulted"); 
    } 
} 

답변

1

, 당신은 오류가 발생한 경우 기대할 수 없다 제기 될 내 예에서,

public class Client : ICallback 
{ 
    public Client() 
    { 
     var context = new InstanceContext(this); 
     var proxy = new WcfDuplexProxy<IApplicationService>(context); 

     (proxy.ServiceChannel as ICommunicationObject).Faulted +=new EventHandler(ServiceChannel_Faulted); 

     //First Ping will call the Pong callback. The exception is thrown 
     proxy.ServiceChannel.Ping(); 
     //Second Ping call fails as the client is in Aborted state 
     try 
     { 
      proxy.ServiceChannel.Ping(); 
     } 
     catch (Exception) 
     { 
      //Re-establish the connection and try again 
      proxy.Abort(); 
      proxy = new WcfDuplexProxy<IApplicationService>(context); 
      proxy.ServiceChannel.Ping(); 
     } 
    } 
    /* 
    [...The rest of the code is the same...] 
    //*/ 
} 

분명히 :

내가 여기에 간단한 코드를하겠습니다 : 그래서, 연결을 다시 설정하는 가장 좋은 방법은 핑()에 대한 후속 호출이 실패 할 때 그것을 할 것입니다 코드에서 예외가 다시 발생하지만이 방법을 사용하면 연결을 다시 설정하는 방법에 대한 아이디어를 얻을 수 있기를 바랍니다.

관련 문제