且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在当前正在运行的控制台应用程序中调用方法。

更新时间:2023-01-28 08:18:39

您可以这样创建服务:

  [ServiceContract] 
公共接口IService
{
[OperationContract]
[WebInvoke(
Method = GET,
UriTemplate = / magic)]
void MagicMethod();

}

还有这样的服务实现:

  public class Service:IService 
{
public void MagicMethod()
{
//魔术在这里
}
}

要启动HTTP服务,它应该看起来像这样:

  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();

这将在端口8080上启动HTTP服务器。
然后可以使HTTP Get请求'http:// localhost:8080 / magic'来调用方法调用。


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 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.