简体   繁体   中英

Messages received on WCF callback are out of order

I have a problem with WCF duplex service.

This is my Service interface:

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

Service implementation:

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

Callback interface:

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

Client callback implementation:

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

Lets say that StartSomething() method starts some kind of the device, and inside that method device goes from two states "Starting" and "Ready". When state changes client is notified through NotificationAvailable in MyServiceCallback class.

The problem is that sometimes in NotificationAvailable method messages are not received in correct order even though ordered delivery is set (The correct order would be "Starting"->"Ready" but callback receives "Ready" >"Starting").

This usually happens on first call of StartSomething() method. It seems like some kind of thread race condition. When I set ConcurrencyMode = ConcurrencyMode.Single on MyServiceCallback the problem disappears.

What is the correct way to solve this issue?

I bet you want to change that InstanceContextMode to single threaded.

Sessions, Instancing, and Concurrency details here .

The use of concurrency is related to the instancing mode. In PerCall instancing, concurrency is not relevant, because each message is processed by a new InstanceContext and, therefore, never more than one thread is active in the InstanceContext.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM