开发者

Invoke a method in a Console application that is currently running.

开发者 https://www.devze.com 2023-04-03 11:35 出处:网络
I have a console application I wrote in C# that polls multiple devices, collects some data, and stores the information on a database. The application runs on our web server, and I was wondering how to

I have a console application I wrote in C# that polls multiple devices, collects some data, and stores the information on a database. The application runs on our web server, and I was wondering how to invoke a method call开发者_JAVA百科 from the command console (so I can exec a command from php that will be read by the console application, a shell command would work as well).

Anyone got any ideas? I've been floating around 'the google' and have found nothing that will supply my current needs.

Also, i'm not adverse to making changes to the console application if an overhaul is needed there. Please, if your answer is COM Interop, provide a GOOD example of how I would build and call this from PHP / Apache2.


You could create a Service like this:

[ServiceContract]
public interface IService
{
    [OperationContract]
    [WebInvoke(
      Method = "GET",
      UriTemplate = "/magic")]
    void MagicMethod();

}

And a service implementation like this:

public class Service : IService
{
    public void MagicMethod()
    {
        //magic here
    }
}

to start a HTTP Service it should look like this:

WebServiceHost host = new WebServiceHost(typeof(Service), new Uri("http://127.0.0.1:8080"))
ServiceEndpoint ep = host.AddServiceEndpoint(typeof(IService), new WebHttpBinding(), "");
ServiceDebugBehavior stp = host.Description.Behaviors.Find<ServiceDebugBehavior>();

stp.HttpHelpPageEnabled = false;
host.Open();  

This will start a HTTP server on port 8080. Then you can make a HTTP Get request to 'http://localhost:8080/magic' to invoke the method call.


Perhaps your console app can poll a directory for a certain file, and react to that.

Then your php app will only need to create that file and the console app should notice it and do whatever you want. I'm not really sure what you want to do.


I would look at using WCF. Your C# application would host a WCF service and then your PHP application could call into it, I believe PHP5 comes with a SOAP library which should make this relatively simple. Any other application you write will be able to easily call in to, especially if they're written in .NET.

I imagine COM would work fine, but I like the scalability of WCF, as if you have to end up moving these applications onto separate servers then you wouldn't need to change anything besides a URL.

There's a good example on this blog. If you're using PHP5 it should be a doddle, if you're having to use 4 then it will still be possible but will require just a bit more legwork.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号