views:

116

answers:

4

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.

+1  A: 

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.

Martin Törnwall
+5  A: 

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:

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

LBushkin
Your assumptions are correct. 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.
tybro0103
For what you describe, I would probably look to use Named Pipes. IIRC, Ruby has built-in libraries that make interacting with named pipes easy, and in .NET there are a number of built-in APIs for performing asynchronous reads/writes to named pipes. Also, WCF has support for them as well. I will update my answer to provide links to relevant resources.
LBushkin
A: 

To expand on Martin's answer, you might consider a messaging protocol with libraries available in both target languages. Something like Stomp comes to mind.

Scott Anderson
A: 

You could probably do something with message queues, particularly one that supports multiple platforms and langauges, like ActiveMQ. I would assume that it would have the capability to add a handler for a message received event so that you could get asynchronous delivery.

tvanfosson