WCF - Notify server when client connects

It is sometimes very important to let the server knows when the client has made a proxy which may talks to the server.

However due to the complication and the design that WCF server has employeed to separate the low-level connection based management from the actual proxy itself. It is really hard to tell when new client has been attempted on the server. 

 

You may try something Get the Dispatcher associated with a ServiceHost and then try to get the Listener (IChannelListener which is attached to the Dispatcher)... Or even, you can create/Build the IChannelListener and then you can simulate the server in the Berkely style like socket api. You can check on the reference "How do I create a simple Web Server using WCF without the ServiceHost class?How do I create a simple Web Server using WCF without the ServiceHost class?" Doing this is OK if you are interactiing with system which interact with each other via soap/xml messages. However, you lost the power of encapsulate of the ServiceHost, which has the ability to dispatch the call to the right instance calll.

However, the WCF system is quite flexible, you can even simuate your own connecion based management interace.

So I will make the following interface.

    [ServiceContract]
    public interface IConnectionService
    {
        [OperationContract(IsOneWay = true)]
        void Connect();

        [OperationContract(IsOneWay = true)]
        void Disconnect();
    }

 and we shall make our service inerface inherited interface of this Connection Serivce. 

    [ServiceContract(CallbackContract = typeof(ITabularPushCallback))]
    public interface ITabularPushService : IHeartbeatService, IConnectionService
    {
        [OperationContract(IsOneWay = true)]
        void Subscribe(string tableId);

        [OperationContract(IsOneWay = true)]
        void UnSubscribe(string tableId);

        [OperationContract(IsOneWay = false)]
        bool IsSubscribed(string tableId);

        [OperationContract(IsOneWay = false)]
        string[] GetTableIds();
    }

 
Then the client should do the following for an valid connection.

 

            InstanceContext instanceContext = new InstanceContext(new TabularPushCallback());
            ServiceEndpoint  endpoint = new ServiceEndpoint(
                ContractDescription.GetContract(typeof(ITabularPushService)),
                new NetTcpBinding(),
                new EndpointAddress("net.tcp://127.0.0.1:9999/TabularPushService"));

            //using (DuplexChannelFactory<ITabularPushService> channelFactory = new DuplexChannelFactory<ITabularPushService>(instanceContext, "TabularPushService"))
            using (DuplexChannelFactory<ITabularPushService> channelFactory = new DuplexChannelFactory<ITabularPushService>(instanceContext, endpoint))
            {
                ITabularPushService proxy = channelFactory.CreateChannel();

                using (proxy as IDisposable)
                {

                    proxy.Connect();
                    string[] tableNames = proxy.GetTableIds();

                    Console.WriteLine("Available Table Ids: \n{0}", string.Join("\n", tableNames));
                    
                    proxy.Subscribe(tableNames[0]);
                    proxy.Subscribe(tableNames[1]);
                    proxy.Subscribe(tableNames[2]);
                    proxy.Subscribe(tableNames[3]);
                    proxy.Subscribe(tableNames[4]);
                    //proxy.Subscribe(tableNames[5]); // Union has problem
                    //proxy.Subscribe(tableNames[6]); // Source3 has problem

                    Console.WriteLine("the table {0} {1} registered", tableNames[0], proxy.IsSubscribed(tableNames[0]) ? "is" : "is not");
                    Console.WriteLine("the table {0} {1} registered", "NonExistingTableId", proxy.IsSubscribed("NonExistingTableId") ? "is" : "is not");

                    proxy.UnSubscribe(tableNames[0]);
                    Console.ReadLine();
                    proxy.Disconnect();
                    Console.WriteLine("Press any key to exit!");
                    Console.Read();
                }
            }

 


And in the implemetatin of the Service. 

And as the implementor, you will need to maintain the inner state of the server, and you should check that the client is connected first. 

 

e.g.

        public string[] GetTableIds()
        {
            TabularChannelManager.Instance.EnsureConnected(OperationContext.Current.GetCallbackChannel<ITabularPushCallback>());
            return TableIdDictWrapper.Instance.TableIdDict.Keys.ToArray<string>();
        }

 and the EnsureConnected call is something like this:

        internal void EnsureConnected(ITabularPushCallback callbackChannel)
        {
            if (callbackChannel == null) throw new ArgumentNullException("callbackchannel");
            lock (SyncObj)
            {
                if (!clients.Contains(callbackChannel))
                    throw new InvalidOperationException("Client is not connected");
            }
        }

 

 

 

References

 

:Notify Server if Client connecs.

How do I create a simple Web Server using WCF without the ServiceHost class?

你可能感兴趣的:(C#,WCF)