WCF Named Pipe IPC

This week I tried to speed up work on Named Pipes. The problem that I am trying to solve with them is that I have an existing Windows service that acts as a device driver that combines data from an external device into a database. Now I need to change this service and add an additional user interface (on the same computer using the IPC form), which can control data when passing between the device and the database, and also send some commands back to the service.

My initial ideas for IPC were either named pipes or memory mapped files. So far, I have been working on the idea of ​​a named pipe using WCF Tutorial Basic Interprocess Communication . My idea is to install the Windows service using an additional thread that implements the WCF NamedPipe service and use this as a channel for the internal components of my driver.

I have some sample code that works, but I can't figure out two issues that I hope someone here can help me with:

  • In the tutorial, ServiceHost is created by an instance of type (StringReverser), and not by reference to a specific class. Thus, there is no mechanism for the Server to interact with the service itself (between the lines host.Open () and host.Close ()). Is it possible to create a connection between and the transfer of information between the server and the class that actually implements the service? If so, how?

  • If I run one server instance and run multiple client instances, it seems that each client receives a separate instance of the service class. I tried to add some status information to a class that implements this service, and was saved only in the named pipe instance. This may be related to the first question, but is there a way to force named pipes to use the same instance of the class that implements this service?

  • Finally, any thoughts on MMF and Named Pipes?

Edit - About Solution

According to Tomasr, the solution is to use the right constructor to provide a specific singleton class that implements the service ( ServiceHost Constructor (Object, Uri []) ). What I did not appreciate at the time was his reference to providing a class of service thread safe. A naive simple designer change caused a crash on the server, and this eventually led me to understand the InstanceContextMode from this Instancecontextmode And Concurrencymode blog post. Setting the correct context is perfectly completed.

+6
named-pipes wcf
source share
1 answer

For (1) and (2), the answer is simple: you can ask WCF to use one instance of your service to handle all requests. Basically, all you have to do is use an alternative ServiceHost constructor that accepts an instance of an object instead of a type.

Please note that you are responsible for ensuring that your service class is safe.

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

+2
source share

All Articles