`

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(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?

分享到:
评论

相关推荐

    HelloWCF--我的WCF第一个练习

    "HelloWCF--我的WCF第一个练习"是一个针对初学者的教程,旨在帮助首次接触WCF的开发者快速理解并掌握这项技术的基础应用。 在WCF项目创建中,通常会经历以下步骤: 1. **定义服务合同**:首先,我们需要定义服务...

    wpf-wcf-wf

    **WPF-WCF-WF整合开发实例** WPF(Windows Presentation Foundation)、WCF(Windows Communication Foundation)和WF(Windows Workflow Foundation)是微软.NET框架中三个关键的技术组件,它们各自负责不同的领域...

    WCF--DEMO_ GLD

    【标题】"WCF--DEMO_ GLD"是一个关于Windows Communication Foundation(WCF)的示例项目,其中“GLD”可能是项目或团队名称的一部分。这个DEMO着重展示了如何在GLD团队环境下创建和使用一个简单的WCF客户端代理类。...

    WCF-MessageContract-XmlSerializable DEMO

    总结来说,`WCF-MessageContract-XmlSerializable DEMO`是一个关于如何在WCF服务中利用`MessageContract`自定义消息结构,以及如何通过`IXmlSerializable`实现更复杂XML序列化的实例。这两个特性结合使用,可以帮助...

    WCF-Communication-data.rar_wcf

    在这个“WCF-Communication-data.rar_wcf”压缩包中,我们可以期待找到一系列关于WCF学习的资源。 WCF的核心概念包括服务、终结点、绑定和合同。服务是提供功能的实体,终结点是服务与外界交互的接口,绑定定义了...

    WCF--system.serviceModel配置属性说明[借鉴].pdf

    WCF--system.serviceModel配置属性说明 WCF(Windows Communication Foundation)是 Microsoft .NET Framework 中的一种技术,提供了一个统一的编程模型,用于构建分布式应用程序。WCF 配置文件是 WCF 应用程序的...

    WCF-CaterSystem.zip

    **WCF-CaterSystem.zip** 是一个包含使用Windows Communication Foundation(WCF)技术与WinForms集成的示例项目。这个项目旨在演示如何在Windows桌面应用(WinForm)中使用WCF服务来处理登录验证和获取人员信息。让...

    Spring.net整合WCF-精简版

    **Spring.NET整合WCF在Windows Forms应用程序中的应用详解** 在.NET框架中,Spring.NET是一个流行的轻量级依赖注入(DI)容器,它提供了一种强大的方式来管理对象生命周期和实现松耦合。同时,Windows ...

    WCF-学习资料汇总

    Windows Communication Foundation(WCF)是微软.NET Framework的一部分,它提供了一种统一的编程模型来创建分布式应用程序,使得服务和客户端之间的通信变得更加简单。WCF旨在解决.NET Framework早期版本中的多种...

    WCF-Winform项目

    开发工具VS2013 一个简单的Demo解决方案实现以下需求: 多台Host根据XML配置指定某一地址为主服务地址 客户端优先连接主服务Host,当主服务机断连自动检测并连接分服务机,当主服务机恢复服务,自动连回主服务机

    WCF-JSCallService跨域请求windows服务

    总的来说,"WCF-JSCallService跨域请求windows服务"这个主题涵盖了.NET WCF服务的配置、跨域策略的实施以及JavaScript客户端的AJAX请求,这些都是Web开发中实现跨域数据交互的重要技术。通过理解和实践这些知识,...

    WCF - LWCF + LServiceHost

    WCF 项目应用连载[4] - 自定义配置 扩展ServiceHost - LServiceHost WCF 项目应用连载[5] - 自定义配置 扩展ChannelFactory&lt;T&gt; - LDuplex ———————————————————————————————— WCF...

    gRPC-for-WCF-Developers_grpc_asp.netC#_

    **gRPC for WCF Developers: 从Web服务到现代微服务架构的跃迁** 随着技术的不断演进,Web服务的开发方式也在发生变化。Windows Communication Foundation (WCF)曾是.NET框架中的主流服务开发工具,但随着时间推移...

    Silverlight+WCF-图片上传-SLUploadPICDemo.zi

    《Silverlight结合WCF实现图片上传下载技术详解》 在当今的Web开发中,交互性和用户体验成为了关键要素,Microsoft的Silverlight技术凭借其丰富的图形渲染和媒体播放能力,为开发者提供了构建富互联网应用(RIA)的...

    mcr.microsoft.com/dotnet/framework/wcf 4.8-windowsservercore-ltsc2019

    提取码:qhok 下载地址在此,有分的给个分,文件太大没办法上传到这里 dotnet framework 的Docker 镜像文件。用于WCF、WebServer等 framework web项目的容器化构件。 因资源在国外,体积又大,提供给需要的人使用。

    NHibernate+WCF项目实战(二)使用NHibernate实现数据访问并进行单元测试 - WCF - 拼吾爱程序人生.mht

    NHibernate+WCF项目实战

    什么是WCF-WFC详解

    Windows通信基础(Windows Communication Foundation,WCF)是基于Windows平台下开发和部署服务的软件开发包(Software Development Kit,SDK)。

    wcf-dynclient:动态 WCF 客户端

    wcf-dynclient 动态 WCF 客户端。 此示例为 StackOverflow 问题创建: ://stackoverflow.com/questions/28253109/wcf-client-proxy-implementation

    Wcf-cors-behavior:WCF处理Cors的示例行为

    本篇将深入探讨如何在WCF中实现CORS行为,并提供一个示例项目——"Wcf-cors-behavior"的解析。 首先,理解CORS的基本原理是至关重要的。CORS通过在HTTP头部添加`Access-Control-Allow-Origin`字段,允许服务器指定...

Global site tag (gtag.js) - Google Analytics