在当前正在运行的控制台应用程序中调用方法。 [英] Invoke a method in a Console application that is currently running.

查看:165
本文介绍了在当前正在运行的控制台应用程序中调用方法。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用C#编写的控制台应用程序,用于轮询多个设备,收集一些数据并将信息存储在数据库中。该应用程序在我们的Web服务器上运行,我想知道如何从命令控制台调用方法调用(这样我就可以从php应用程序执行命令,该命令将被控制台应用程序读取,shell命令也可以工作)。 / p>

有人有什么想法吗?我一直徘徊在谷歌周围,却找不到满足我当前需求的东西。



此外,我对更改控制台应用程序也没有不利如果在那里需要大修。请,如果您的答案是COM Interop,请提供一个很好的示例,说明如何从PHP / Apache2进行构建和调用。

解决方案

您可以这样创建服务:

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

这篇关于在当前正在运行的控制台应用程序中调用方法。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆