Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raise event in C# from external application?

Is there a way to raise an event in C# from an external application? In particular from Ruby? I have need to cause something to happen in C# from a rails application.

like image 911
tybro0103 Avatar asked Sep 27 '10 19:09

tybro0103


2 Answers

I assume that by "external application" you mean something that is outside of the process space of the C# code. Also, I assume that you're not referring to C# events - which aren't a concept that exists outside of the implementation domain of .NET - they aren't something that are accessible from outside of the code.

So the general answer to this question is is "yes it is possible" - but since you're essentially trying to send inter-process notifications you are going to need to use some kind of IPC mechanism.

.NET has a rich API for performing IPC, ranging from named pipes, to DCOM, to low-level networking protocols like TCP/IP. Which approach you use depends on the environment in which these process need to communicate (on a single machine, across machines on an intranet, over the internet, etc). It will also depend on the kind of information you're exchanging and how much effort you're willing to expend. For .NET 3.5 applications you can usually find an supported implementation using WCF as the unifying framework.

Obviously, all of this depends on your ability to modify the implementation (read source code) of the .NET application you're trying to raise an event within. If you can't do that, and there is no existing IPC mechanism in that code, you are likely stuck.

UPDATE: In response to your comment:

What would you recommend with priority of ease and time. Both apps are running on the same machine, and I am writing them both myself

I would consider using Named Pipes. Named pipes are originally a Unix concept - but they are available on Windows as well. They have a vary simple programming interface (they are similar to working with files), and don't require much to use in a single-machine scenario.

On the Ruby side (which I am less familiar with), here are some link that you may find useful:

  • Using Named Pipes for Interprocess Communication In Ruby
  • Communicating Between Ruby and PHP Using Named Pipes

On the .NET side, here are some relevant links to explore for more information:

  • WCF Communication Through Named Pipes With Non .NET Apps
  • .NET 3.5 Adds Named Pipes Support
  • .NET Named Pipe Server Stream Class
like image 135
LBushkin Avatar answered Sep 20 '22 17:09

LBushkin


An obvious solution would be to use a network message of sorts. Either a short-lived TCP/IP connection, or -- particularly if both apps are on the same host -- a simple UDP packet.

The C# application would accept connections (if TCP), read the event(s) from the socket and finally process them.

like image 24
Martin Törnwall Avatar answered Sep 22 '22 17:09

Martin Törnwall