Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# communication between processes

Tags:

c#

servicebus

I'm working with an application, and I am able to make C# scripts to run in this environment. I can import DLLs of any kind into this environment. My problem is that I'd like to enable communication between these scripts. As the environment is controlled and I have no access to the source code of the application, I'm at a loss as to how to do this.

Things I've tried:

  • File I/O: Just writing the messages that I would like each to read in .txt files and having the other read it. Problem is that I need this scripts to run quite quickly and that took up too much time.

  • nServiceBus: I tried this, but I just couldn't get it to work in the environment that I'm dealing with. I'm not saying it can't be done, just that I can't get it done.

Does anyone know of a simple way to do this, that is also pretty fast?

like image 442
zkwentz Avatar asked Mar 14 '10 18:03

zkwentz


5 Answers

Your method of interprocess communication should depend on how important it is that each message get processed.

For instance, if process A tells process B to, say, send an email to your IT staff saying that a server is down, it's pretty important.

If however you're streaming audio, individual messages (packets) aren't critical to the performance of the app, and can be dropped.

If the former, you should consider using persistent storage such as a database to store messages, and let each process poll the database to retrieve its own messages. In this way, if a process is terminated or loses communication with the other processes temporarily, it will be able to retrieve whatever messages it has missed when it starts up again.

like image 126
3Dave Avatar answered Nov 13 '22 13:11

3Dave


The answer is simple;

Since you can import any DLL into the script you may create a custom DLL that will implement communication between the processes in any way you desire: shared memory, named pipe, TCP/UDP.

like image 21
Poni Avatar answered Nov 13 '22 12:11

Poni


You could use a form of Interprocess Communication, even within the same process. Treat your scripts as separate processes, and communicate that way.

Named pipes could be a good option in this situation. They are very fast, and fairly easy to use in .NET 3.5.

Alternatively, if the scripts are loaded into a single AppDomain, you could use a static class or singleton as a communication service. However, if the scripts get loaded in isolation, this may not be possible.

like image 43
Reed Copsey Avatar answered Nov 13 '22 14:11

Reed Copsey


Well, not knowing the details of your environment, there is not much I can really offer. You are using the term "C# scripts"...I am not exactly sure what that means, as C# is generally a compiled language.

If you are using normal C#, have you looked into WCF with Named Pipes? If your assemblies are running on the same physical machine, you should be able to easily and quickly create some WCF services hosted with the Named Pipe binding. Named pipes provide a simple, efficient, and quick message transfer mechanism in a local context. WCF itself is pretty easy to use, and is a native component of the .NET framework.

like image 2
jrista Avatar answered Nov 13 '22 12:11

jrista


Since you already have the File I/O in place you might get enough speed by placing it on a RAM disk. If you are polling for changes today a FileSystemWatcher could help to get your communication more responsive.

like image 2
Jonas Elfström Avatar answered Nov 13 '22 12:11

Jonas Elfström