IPC Mechanisms in C# - Usage and Best Practices

78,271

Solution 1

Most recent Microsoft's stuff in IPC is Windows Communication Foundation. Actually there is nothing new in the lower level (tcp, upd, named pipes etc) But WCF simplifies IPC development greatly.

Useful resource:

and of course MSDN on WCF

Solution 2

Apart from the obvious (WCF), there is a ZeroMQ binding for C#/CLR which is pretty good:

http://www.zeromq.org/bindings:clr

Does message-oriented IPC, pub/sub and various other strategies with much less code and config than WCF.

It's also at least an order of magnitude faster than anything else and has less latency if you require low latency comms.

With respects to semaphores, locks, mutexes etc. If you share by communicating rather than communicate by sharing, you'll have a whole load less hassle than the traditional paradigm.

Solution 3

I tend to use named pipes or Unix sockets (depending on whether I'm targetting MS.NET or Mono -- I have a class that abstracts it away) since it's easy to use, portable, and allows me to easily interoperate with unmanaged code. That said, if you're only dealing with managed code, go with WCF or remoting -- the latter if you need Mono support, since their WCF support simply isn't there yet.

Solution 4

I would recommend using Memory Mapped Files if you need to use on the machine domain not communication through network. See the following link.

http://techmikael.blogspot.com/2010/02/blazing-fast-ipc-in-net-4-wcf-vs.html

Solution 5

There is also .NET Remoting, which I found quite cool, but I guess they are obsoleting it now that they have WCF.

Share:
78,271
prakash
Author by

prakash

Dad, Dreamer, Developer

Updated on July 09, 2022

Comments

  • prakash
    prakash almost 2 years

    I have used IPC in Win32 code a while ago - critical sections, events, and semaphores.

    How is the scene in the .NET environment? Are there any tutorial explaining all available options and when to use and why?

  • Mark
    Mark over 14 years
    Has Microsoft ever actually said that?
  • Lucas
    Lucas almost 14 years
    They have: "This topic is specific to a legacy technology that is retained for backward compatibility with existing applications and is not recommended for new development. Distributed applications should now be developed using the Windows Communication Foundation (WCF)." from msdn.microsoft.com/en-us/library/kwdt6w2k.aspx
  • Brent Matzelle
    Brent Matzelle almost 13 years
    While this may have been the case in Sep 2008 (when this comment was made), it's 2011 and WCF on Mono is quite mature now. See the Mono WCF Development page and judge for yourself.
  • Sandeep Datta
    Sandeep Datta almost 13 years
    Hey the video in the first link is dead!
  • Matt Klein
    Matt Klein over 8 years
    "share by communicating rather than communicate by sharing" This should be recited every day by anyone who is doing concurrent programming.
  • Ricardo Peres
    Ricardo Peres over 8 years
    A list of .NET local Machine APIs: weblogs.asp.net/ricardoperes/…
  • Believe2014
    Believe2014 almost 7 years
    What does this mean?
  • jmathew
    jmathew almost 7 years
    I take it to mean that instead of sharing resources and having each program try to claim it at will, you should have programs talk directly to each other with IPC. In the first scenario, your programs "communicate" by locking a shared resource. In the second, they communicate through some protocol to coordinate usage of the shared resource.
  • Samuel
    Samuel almost 5 years
    zguide.zeromq.org/page:all#Unicast-Transports The inter-process ipc transport is disconnected, like tcp. It has one limitation: it does not yet work on Windows. Soooo actually not really IPC for windows right? Which still might be quite common when the C# tag was added.
  • Behzad
    Behzad almost 5 years
    I have worked with Remoting for years.. For small-scale applications, ( e.g. passing up to 1k small objects ) it is very straight-forward and efficient. Above that limit, No! It really sucks. You have to zip data before sending and this adds extra overhead ( CPU usage ). I had a very frustrating memory with the issue.
  • 0b101010
    0b101010 almost 4 years
    You could use the local loopback adaptor with ZeroMQ. Performance will obviously depend on the adaptor implementation though. However, I don't like the semantics of hitting the network stack (albeit a virtual interface) when your intention is not communication over a network. Personally for IPC on the same machine I think using pipes is the best solution. The .NET Standard implementation is rock solid and totally portable. Mapped memory solutions may offer the best performance, but rolling your own is complex and synchronisation is error prone. "share by communicating" :)