Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a single WCF service with multiple clients. This is implemented using Duplex communication.
I have a request for execute a command(request from client) and send back the result to the same client.

My InstanceContextMode is PerSession.

Currently Result is getting updated to all the client. I need to just update the send to the client which triggered execute. How to achieve this?

Sample Service Code:

C#
ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class NotificationService : INotificationService
{
    
static Action<string> sendDataEvent = delegate { };

private INotificationServiceCallBack GetCurrentCallback()
        {
            return OperationContext.Current.GetCallbackChannel<INotificationServiceCallBack>();
        }


 public void Subscribe()
        {
            var subscriber = GetCurrentCallback();
            sendDataEvent += subscriber.SendResult;
        }


        public void Execute()
        {
           
                //logic and then send the result back
                sendDataEvent(measurements);
       }
}



Client Code:

C#
public class TestDataInterface:INotificationServiceCallback,INotifyPropertyChanged
   {
static Dictionary<string, TestDataInterface> instance=null;
 string testBench=string.Empty;

        NotificationService.NotificationServiceClient notificationProxy;


private TestDataInterface(string testBenchName)
       {
           testBench = testBenchName;
           notificationProxy = new NotificationService.NotificationServiceClient(new InstanceContext(this));
           notificationProxy.Subscribe();
       }

public static TestDataInterface Instance(string testBenchName)
        {
            if (instance == null)
                instance = new Dictionary<string, TestDataInterface>();
            //Add object only if key testBenchName is not found
            if (!instance.ContainsKey(testBenchName))
            {
                instance.Add(testBenchName, new TestDataInterface(testBenchName));
            }
            return instance[testBenchName];
        }

#region INotificationServiceCallback Methods
        public void SendResult(string testResult)
        {
            //logic
        }
        #endregion

public void ExecuteCommand(string command)
       {
                      notificationProxy.Execute(command);
       }
Posted
Updated 30-Jun-15 5:47am
v5
Comments
virusstorm 30-Jun-15 12:19pm    
Your issue is your static delegate. Because it is static, it exists once on the heap inside .NET. All of the clients that subscribe to it will be notified. To offer a suggestion on how to fix it, I need a better understanding of what you are trying to do.
Ganga Prabhu 30-Jun-15 14:14pm    
Thank you. I am not sure why I used static. It worked after removing.
what else do I need to take care when I have single service with multiple clients.( do I need to queue my request)

Mine requirement is stated here:
http://www.codeproject.com/Questions/1003461/What-is-the-best-way-to-archictect-the-following-r?
Ganga Prabhu 30-Jun-15 13:45pm    
@ virusstorm : Thank you for the suggestion. I am just trying to learn WCF.

Here is my goal for today:
a. Single Service serving multiple clients.
b. Client will request for "Start Operation", Service will perform the operation and send the result back to the same client.

Let me know if I am not clear still..
virusstorm 30-Jun-15 14:41pm    
Your best bet is to have your clients do their operations async.

https://msdn.microsoft.com/en-us/library/ms730059%28v=vs.110%29.aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900