V
V
Vitaly Pukhov2015-06-04 08:11:26
Programming
Vitaly Pukhov, 2015-06-04 08:11:26

Am I understanding WCF correctly?

I found the service code on WCF, but I can’t understand its logic, do I understand correctly that if there are, say, 10 clients and 1 of them calls proxy.ProcessData(), then each of the 10 will have the NotifyClient() procedure called and the console will display "Notification from Server"?
The doubt is caused by the fact that in the implementation of the service, I do not see a word about lists or any iterators or enumerators. It looks like NotifyClient() will be called only for the one who called proxy.ProcessData, then it's not clear why such a crutch is needed at all.

Using WCF you can use duplex named pipes
// Create a contract that can be used as a callback
public interface IMyCallbackService
{
    [OperationContract(IsOneWay = true)]
    void NotifyClient();
}

// Define your service contract and specify the callback contract
[ServiceContract(CallbackContract = typeof(IMyCallbackService))]
public interface ISimpleService
{
    [OperationContract]
    string ProcessData();
}
Implement the Service
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall)]
public class SimpleService : ISimpleService
{
    public string ProcessData()
    {
        // Get a handle to the call back channel
        var callback = OperationContext.Current.GetCallbackChannel<IMyCallbackService>();

        callback.NotifyClient();
        return DateTime.Now.ToString();
    }
}
Host the Service
class Server
{
    static void Main(string[] args)
    {
        // Create a service host with an named pipe endpoint
        using (var host = new ServiceHost(typeof(SimpleService), new Uri("net.pipe://localhost")))
        {
            host.AddServiceEndpoint(typeof(ISimpleService), new NetNamedPipeBinding(), "SimpleService");
            host.Open();

            Console.WriteLine("Simple Service Running...");
            Console.ReadLine();

            host.Close();
        }
    }
}
Create the client application, in this example the Client class implements the call back contract.
class Client : IMyCallbackService
{
    static void Main(string[] args)
    {
        new Client().Run();
    }

    public void Run()
    {
        // Consume the service
        var factory = new DuplexChannelFactory<ISimpleService>(new InstanceContext(this), new NetNamedPipeBinding(), new EndpointAddress("net.pipe://localhost/SimpleService"));
        var proxy = factory.CreateChannel();

        Console.WriteLine(proxy.ProcessData());
    }

    public void NotifyClient()
    {
        Console.WriteLine("Notification from Server");
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artyom, 2015-06-04
@Neuroware

It looks like NotifyClient() will be called only for the one who called proxy.ProcessData, then it's not clear why such a crutch is needed at all.

And there is. And such a crutch is needed because it is such a "Hello World"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question