简体   繁体   中英

Bidirectional IPC between C# applications

I am aware of IPC and have used it in several projects. My new requirements include two simple C# applications that need to exchange short messages like "do this" "done that". This should be bidirectional.

This is going to run on old systems (Win Xp .Net 3.5) without being able to configure them. The end user will just run the application.

  • WCF is too complicated for this task
  • Sockets over TCP require firewall configuration and might be blocked from antiviruses
  • Microsoft Message Queuing fits perfectly but requires MSMQ to be installed on all clients
  • Named pipes can be used, but I would like something in higher level (I feel like using assembly with them)

Is there a simple way to exchange data between C# applications?

You've got a couple options:

  • wcf, which you say you've considered and rejected; may I inquire as to what made you reject it?

  • named pipes, as you mentioned; it's actually reasonably trivial to wrap two named pipes into a bidirectional messaging system (pro tip: make it disposable and clean up the pipes on dispose lest you lock them up)

  • queuing systems, but usually you see these in many-to-many or one-to-many channels

  • serialize a message queue (simple queue structure) and put it in a memory mapped file, then read/write from either side

  • esoteric: use windows messaging, ie pinvoke send/post message

Although you said TCP is out...

i've been very keen to use ZeroMQ but just haven't had an opportunity. It's multi platform with ac# wrapper and is designed exactly for this sort of application messaging. Check out ZeroMQ at http://www.zeromq.org/ .

Some sample code from one of their sites:

// Set up a publisher.

var publisher = new ZmqPublishSocket {
    Identity = Guid.NewGuid().ToByteArray(),
    RecoverySeconds = 10
};

publisher.Bind( address: "tcp://127.0.0.1:9292" );

// Set up a subscriber.

var subscriber = new ZmqSubscribeSocket();

subscriber.Connect( address: "tcp://127.0.0.1:9292" );

subscriber.Subscribe( prefix: "" ); // subscribe to all messages

// Add a handler to the subscriber's OnReceive event

subscriber.OnReceive += () => {

    String message;
    subscriber.Receive( out message, nonblocking: true );

    Console.WriteLine( message );
};

// Publish a message to all subscribers.

publisher.Send( "Hello world!" );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM