WCF - Notify server when client connects

3 篇文章 0 订阅

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(IPushCallback))]
    public interface IPushService : 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 PushCallback());
            ServiceEndpoint  endpoint = new ServiceEndpoint(
                ContractDescription.GetContract(typeof(IService)),
                new NetTcpBinding(),
                new EndpointAddress("net.tcp://127.0.0.1:9999/PushService"));

            //using (DuplexChannelFactory<IPushService> channelFactory = new DuplexChannelFactory<IPushService>(instanceContext, "PushService"))
            using (DuplexChannelFactory<IPushService> channelFactory = new DuplexChannelFactory<IPushService>(instanceContext, endpoint))
            {
                IPushService 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(IPushCallback 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?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值