2

I have a Windows service written in C# that handles processing of digital documents, and a web application written in PHP. I need to be able to notify the C# service that new documents are ready for processing.

Currently, the C# service is reading the MySQL database every 10 seconds to see if there are new tasks to be performed. This creates a 10 second lag in the responsiveness of the web application. I'd like to be able to trigger the C# service immediately when the document is ready.

I'm looking for an answer that will allow PHP to notify the C# service without any delay.

Here are some possible ideas that I've had;

  • Use a shared memory object between PHP and C#, and have a thread in C# wait for that object to be signaled. Is this possible with PHP?
  • PHP connects to C# using a socket on localhost, sends a nak and disconnects. This seems inefficient to me.
  • Configure MySQL to use an in-memory table, and have the C# service query the table every second.
  • Is it difficult to create some kind of web service in C# that uses XML or SOAP, and would there be any lag (+1 second) in calling that service via PHP?

That's all I can think of. Maybe there is some kind of standard way this should be done, and I'm just not aware of it.

Reactgular
  • 52,335
  • 19
  • 158
  • 208
  • 1
    Options 2 and 3 allow your web app to scale to multiple hosts. And something *like* your options 2 and 3 aren't exactly uncommon. Option 1 sounds ... not simple. – svidgen Jan 24 '13 at 17:13
  • @svidgen I agree. Opening it up to scalability is a good idea. Document processing is very time consuming. – Reactgular Jan 24 '13 at 17:17
  • 1
    Your newly added option 4 sounds like the most common solution. M$ uses SOAP services for pretty much all of their Dynamics integration stuff. I should also note, they don't *just* use SOAP services. They also perform some polling ... Not that that's a great way to go if you need low latency. Just that you're not doing anything out of the ordinary with any option but your first one. – svidgen Jan 24 '13 at 17:18
  • 1
    I don't know PHP, but you can use OnCustomCommand in your service. You can then send the command from another app: http://stackoverflow.com/questions/3695245/how-to-send-a-custom-command-to-a-net-windows-service-from-net-code – Pete Jan 24 '13 at 17:20

3 Answers3

1

I'm going to go ahead and try to answer this.

In your service, add an OnCustomCommand handler as described in this question to trigger the service work: How to send a custom command to a .NET windows Service from .NET code?

Create a separate C# application that simply sends the command to your service and call that from PHP via the exec() function.

Community
  • 1
  • 1
Pete
  • 6,585
  • 5
  • 43
  • 69
  • PHP has an issue. Only one exec() process can run at a time. So if the application was busy enough and two people triggered an exec() at the same time, then only one would fire. That might not be an issue since the C# service would process what documents were pending anyway. – Reactgular Jan 24 '13 at 17:27
  • It would also execute very quickly if all it's doing is sending the command. Optionally you could do a WCF or other service, as the other responder mentioned, which sends the command. – Pete Jan 24 '13 at 17:29
  • 1
    A note from the PHP docs: Note: If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends. – Pete Jan 24 '13 at 17:30
1

It'd be pretty trivial to make a REST facade in WCF that triggers your c# service on a POST against /. Security can be layered on depending on the nature of your deployment.

http://msdn.microsoft.com/en-us/library/bb412178.aspx

Krypes
  • 561
  • 2
  • 10
1

You could self-host an ASP.NET WebAPI in your service

Ian Mercer
  • 38,490
  • 8
  • 97
  • 133