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

发布于 2024-12-03 12:25:10 字数 287 浏览 2 评论 0原文

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

有人有什么想法吗?我一直在“谷歌”上徘徊,但没有找到任何可以满足我当前需求的东西。

另外,如果需要进行彻底修改,我并不反对对控制台应用程序进行更改。如果您的答案是 COM Interop,请提供一个很好的示例,说明我如何从 PHP/Apache2 构建和调用它。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

皇甫轩 2024-12-10 12:25:10

您可以创建这样的服务:

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

}

和​​这样的服务实现:

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

要启动 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://localhost:8080/magic”发出 HTTP Get 请求来调用该方法。

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.

-小熊_ 2024-12-10 12:25:10

也许您的控制台应用程序可以轮询目录中的某个文件,并对此做出反应。

然后你的 php 应用程序只需要创建该文件,控制台应用程序应该注意到它并执行你想要的任何操作。我不太确定你想做什么。

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.

蓝眼睛不忧郁 2024-12-10 12:25:10

我会考虑使用 WCF。您的 C# 应用程序将托管一个 WCF 服务,然后您的 PHP 应用程序可以调用它,我相信 PHP5 附带了一个 SOAP 库,这应该使这个过程相对简单。您编写的任何其他应用程序都可以轻松调用,特别是如果它们是用 .NET 编写的。

我认为 COM 可以很好地工作,但我喜欢 WCF 的可扩展性,就好像您最终必须将这些应用程序移动到单独的服务器上,然后除了 URL 之外不需要更改任何内容。

此博客有一个很好的示例。如果您使用 PHP5,这应该是轻而易举的事,如果您必须使用 4,那么它仍然是可能的,但需要更多的跑腿工作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文