如何在Web应用程序中发布控制台应用程序.

如何在Web应用程序中发布控制台应用程序.

问题描述:

我已经在服务器上发布了一个Web应用程序,现在可以正常运行了,我想在其中添加另一个要通过调度程序运行的应用程序.我已经在global.asax文件中进行了必要的更改.实际上,当我在Web项目中添加该控制台项目的引用,然后将其复制到Web项目的根目录中时,代码在本地计算机上运行良好.但是当我在服务器上发布它的同时做同样的事情时,它就无法正常工作了.有人可以建议我通过服务器上的调度程序运行某些代码的方法.

I''ve published a web application on the sever and it is working fine now I want to add another application in it which I want to run through the scheduler. I''ve already made the required changes in global.asax file. Actually, the code is running well on local machine when I''m adding reference of that console project in my web project and then copying it in the root directory of web project. But when I''m doing the same while publishing it on the server its not working. Can someone please suggest me some way to run some code through scheduler on the server.

一个主意是:您可以创建一些库将替换类System.Console.这将允许您获取控制台应用程序的代码文件,将其插入ASP.NET应用程序或APS.NET应用程序使用的类库程序集项目,只需替换uses子句即可使用此替换类.此类应将控制台输出的结果放到某个数据容器中,以便稍后在生成HTTP响应时使用.这样,您将能够保留现有控制台应用程序的大部分代码.当然,您将只能使用控制台输入.完成这些操作后,您的控制台应用程序将不再是控制台应用程序.

上面说明的方法可以非常准确地实现,但是需要一些时间和精力.

另一种方法很脏:您可以尝试按原样使用控制台应用程序(但是,请确保其中没有控制台输入),然后使用System.Diagnostics.Process.Start作为单独的进程运行它.您将需要在提供的某些流中重定向控制台输出(实际上,通常情况下,您将需要两个流:用于标准输出和标准错误),读取流并将输出数据放在某个容器中,以备将来用于生成HTTP响应.

有关输出重定向的基本示例,请参见以下MSDN页面: http://msdn .microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx [基于枚举的命令行实用程序 [ ^ ].

—SA
One idea is this: you can create some library which would replace the class System.Console. This will allow you to take your code files of your console application, insert into your ASP.NET application or a class library assembly project used by your APS.NET application and simply replace uses clauses to use this replacement class. This class should put the results of console output to some data container which you can later use when generating HTTP response. This way, you would be able to preserve most of your code of your existing console application. You only won''t be able to use console input, of course. After these manipulations, your console application will not be a console application anymore.

The approach explained above can be implemented very accurately, but it needs some time and attention.

Another approach is pretty dirty: you can try to use your console application as is (however, makes sure there is no console input in it) and run it as a separate process using System.Diagnostics.Process.Start. You will need to re-direct console output in some stream you provide (actually, in general case, you will need two streams: for standard output and standard error), read the stream and put output data in some container for future use for generation of HTTP response.

For a basic sample of output redirection, see this MSDN page: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx[^].

If you have any console input in your console application you should remove it, promote input variables to method parameters and organize input of those parameters using HTML controls, pass them to code-behind using Web forms or AJAX and then pass them to you modified code of console application. In second approach, you should pass those parameters using the command line of your console application. For a command line parsing library (you will really need something more high-level compared to regular .NET raw command line parameters), you can use my work published at CodeProject: Enumeration-based Command Line Utility[^].

—SA