2016-10-26 1 views
3

WCF 이중 서비스에 문제가 있습니다.WCF 콜백에서받은 메시지가 잘못되었습니다.

[DeliveryRequirements(RequireOrderedDelivery = true)] 
[(CallbackContract = typeof(IMyNotification), SessionMode = SessionMode.Required)] 
public interface IMyService 
{ 
    [OperationContract] 
    void StartSomething();  
    ... 
} 

서비스 구현 :

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple)] 
public class MyService : IMyService 
{ 
    ... 
} 

콜백 인터페이스 :

[DeliveryRequirements(RequireOrderedDelivery = true)] 
public interface IMyNotification 
{ 
    [OperationContract (IsOneWay=true)] 
    void NotificationAvailable(Notification notification); 
} 

클라이언트 콜백 구현 :

[CallbackBehavior (ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext = false)] 
class MyServiceCallback : IMyNotification 
{ 
    public void NotificationAvailable(Notification notification) 
    { 
      lock (_NotificationLock) 
      { 
       // process notification... 
      } 
    } 
} 

내 서비스 인터페이스

StartSomething() 메서드가 어떤 종류의 장치를 시작하고 그 메서드 내부에 장치가 "Starting"과 "Ready"두 가지 상태에서 시작한다고 가정 해 보겠습니다. 상태가 변경되면 클라이언트는 MyServiceCallback 클래스에서 NotificationAvailable을 통해 알림을받습니다.

주문 배달이 설정되어 있어도 NotificationAvailable 메서드 메시지 이 올바른 순서로 수신되지 않는 경우가 있습니다 (올바른 순서는 "시작"-> "준비 중"이지만 콜백은 "준비 중"> "시작 중").

일반적으로 StartSomething() 메서드의 첫 번째 호출에서 발생합니다. 어떤 종류의 스레드 경쟁 조건처럼 보입니다. MyServiceCallback에 ConcurrencyMode = ConcurrencyMode.Single을 설정하면 문제가 사라집니다.

이 문제를 해결하기위한 올바른 방법은 무엇입니까?

답변

0

InstanceContextMode을 싱글 스레드로 변경하고 싶습니다.

세션, 인스턴스 및 동시성 세부 정보 here.

동시성 사용은 인스턴스 모드와 관련이 있습니다. PerCall 인스 턴싱에서 동시성은 관련이 없습니다. 각 메시지가 새로운 InstanceContext에 의해 처리되고 따라서 스레드가 InstanceContext에서 활성화되어 있기 때문에 입니다.

관련 문제