WCF命名管道IPC [英] WCF Named Pipe IPC

查看:154
本文介绍了WCF命名管道IPC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本周,我一直在努力加快命名管道的速度.我要与他们解决的任务是,我有一个现有的Windows服务,该服务充当设备驱动程序,可将数据从外部设备传输到数据库中.现在,我必须修改此服务并添加一个可选的用户前端(在同一台计算机上,使用IPC形式),该前端可以监视数据在设备和DB之间传递时的数据,以及将一些命令发送回该服务

I have been trying to get up to speed on Named Pipes this week. The task I am trying to solve with them is that I have an existing windows service that is acting as a device driver that funnels data from an external device into a database. Now I have to modify this service and add an optional user front end (on the same machine, using a form of IPC) that can monitor the data as it passes between the device and the DB as well as send some commands back to the service.

我对IPC的最初想法是命名管道或内存映射文件.到目前为止,我一直在使用 WCF教程基本交互过程来研究命名管道的想法.交流.我的想法是通过实现WCF NamedPipe服务的附加线程来设置Windows服务,并将其用作驱动程序内部的管道.

My initial ideas for the IPC were either named pipes or memory mapped files. So far I have been working through the named pipe idea using WCF Tutorial Basic Interprocess Communication . My idea is to set the Windows service up with an additional thread that implements the WCF NamedPipe Service and use that as a conduit to the internals of my driver.

我有示例代码,但是我无法解决两个问题,我希望这里的人可以为我提供帮助:

I have the sample code working, however I can not get my head around 2 issues that I am hoping that someone here can help me with:

  1. 在本教程中,ServiceHost是使用typeof(StringReverser)实例化的,而不是通过引用具体的类来实例化的.因此,服务器似乎没有与服务本身进行交互的机制(在host.Open()和host.Close()之间).是否可以在服务器和实际实现服务的类之间创建链接并在服务器之间传递信息?如果是这样,怎么办?

  1. In the tutorial the ServiceHost is instantiated with a typeof(StringReverser) rather than by referencing a concrete class. Thus there seems to be no mechanism for the Server to interact with the service itself (between the host.Open() and host.Close() lines). Is it possible to create a link between and pass information between the server and the class that actually implements the service? If so, how?

如果我运行服务器的单个实例,然后运行客户端的多个实例,则似乎每个客户端都获得了服务类的单独实例.我尝试将一些状态信息添加到实现服务的类中,并且仅保留在命名管道的实例中.这可能与第一个问题有关,但是无论如何,是否要强制命名管道使用实现服务的类的相同实例?

If I run a single instance of the server and then run multiple instance of the clients, it seems that each client gets a separate instance of the service class. I tried adding some state information to the class implementing the service and it was only retained within the instance of the named pipe. This is possibly related to the first question, but is there anyway to force the named pipes to use the same instance of the class that is implementing the service?

最后,对MMF与命名管道有何想法?

Finally, any thoughts on MMF vs Named Pipes?

编辑-关于解决方案

按照Tomasr的回答,解决方案在于使用正确的构造函数,以提供实现服务的具体单例类(

As per Tomasr's answer the solution lies in using the correct constructor in order to supply a concrete singleton class that implements the service (ServiceHost Constructor (Object, Uri[])). What I did not appreciate at the time was his reference to ensuring the service class was thread safe. Naively just changing the constructor caused a crash in the server, and that ultimately lead me down the path of understanding InstanceContextMode from this blog entry Instancecontextmode And Concurrencymode. Setting the correct context nicely finished off the solution.

推荐答案

对于(1)和(2),答案很简单:您可以要求WCF使用服务的单例实例来处理所有请求.通常,您需要做的就是使用备用ServiceHost 构造函数对象实例而不是类型.

For (1) and (2) the answer is simple: You can ask WCF to use a singleton instance of your service to handle all requests. Mostly all you need to do is use the alternate ServiceHost constructor that takes an Object instance instead of a type.

但是,请注意,您将负责使服务类线程安全.

Notice, however, that you'll be responsible for making your service class thread safe.

对于3,这实际上很大程度上取决于您需要做的事情,您的性能需求,您同时期望有多少客户,您要移动的数据量以及需要存储多长时间.可用,等等.

As for 3, it really depends a lot on what you need to do, your performance needs, how many clients you expect at the same time, the amount of data you'll be moving and for how long it needs to be available, etc.

这篇关于WCF命名管道IPC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆