在服务器上运行C#程序并通过WCF/ASP页面控制它
我使用 Visual Studio 2010 用 C# 编写了一个程序。目前它是一个控制台应用程序,我理想的情况是让这个程序在服务器上运行。它将消息记录在 SQL 数据库中,我可以通过网站查看,没有问题,但我想要的是能够控制 C# 程序并通过浏览器中的网站远程调用方法。我定义了这些方法,其中包含对程序中其他方法的调用,例如“ArticleOrganizer.reprocessArticle(a, 0)”:
[ServiceContract]
public interface IRemotePrimeService
{
[OperationContract]
void ProgramRestart();
[OperationContract]
void ProcessArticleAgain(Article a);
}
public class RemotePrimeService : IRemotePrimeService {
public void ProgramRestart() {
Console.WriteLine("Restart requested remotely. Program restarting.");
Application.Restart();
}
public void ProcessArticleAgain(Article a) {
// Log the activity
a.MessageLog = "Reprocessing requested remotely. Article sent to Stage 0 of pipeline.";
// start the article through the processing again
ArticleOrganizer.reprocessArticle(a, 0);
}
}
这些是我希望能够远程调用的方法。 WCF 中的服务似乎就是这样做的方式,但是我可以将这个 C# 控制台应用程序按原样放在服务器上,而不必将其重写为 Web 应用程序吗?我已经阅读了很多文档,特别是 MSDN 上的文档,但希望能详细说明我的最佳操作过程。我已在计算机上安装并设置 IIS 以作为本地主机进行测试。
非常感谢您的建议
I have written a program in C# using Visual Studio 2010. At current it is a Console Application and what I ideally want is to have this program running on a server. It logs messages in a SQL database that I can view via a website no problem, but what I want is to be able to control the C# program and call methods remotely via the website in the browser too. I have defined these methods that contain calls to other methods in the program such as 'ArticleOrganizer.reprocessArticle(a, 0)':
[ServiceContract]
public interface IRemotePrimeService
{
[OperationContract]
void ProgramRestart();
[OperationContract]
void ProcessArticleAgain(Article a);
}
public class RemotePrimeService : IRemotePrimeService {
public void ProgramRestart() {
Console.WriteLine("Restart requested remotely. Program restarting.");
Application.Restart();
}
public void ProcessArticleAgain(Article a) {
// Log the activity
a.MessageLog = "Reprocessing requested remotely. Article sent to Stage 0 of pipeline.";
// start the article through the processing again
ArticleOrganizer.reprocessArticle(a, 0);
}
}
These are what I want to be able to call remotely. It seems Services in WCF is how one would do this, but can I put this C# Console Application on the server as it is somehow without having to rewrite it as a Web Application? I have read A LOT of the documentation, particularly on MSDN but would appreciate a simple step-by-step of my best course of action. I have installed and set up IIS on my computer to test as a localhost.
Many thanks in advance for your advice
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
保持简单,并使用简单的异步事件驱动系统。
例如:引入一个可以存储发送给应用程序的消息的队列。应用程序(无论是控制台还是服务)可以在有时间时读取队列并执行请求。队列可以是 MSMQ 或 SQL 表。只要您不需要即时反馈(此类系统中大多数时间都是这种情况),就可以很好地工作。
Keep it simple, and use a simple async event-driven system.
For example: Introduce a queue where messages to the application can be stored. The application (whether it's console or service) can read the queue when it has time and execute the requests. The queue can be f.e. MSMQ or a SQL table. Works great as long as you won't need instant feedback, which is the case most of time in these kind of systems.