如何在Visual Studio中使用我的网站的控制台应用程序?

时间:2021-07-02 19:31:03

Please read the following in the Visual Studio 2012 context:

请阅读Visual Studio 2012上下文中的以下内容:

I have two projects--one is a website (File --> New Website) and another is a console application (File --> New Project --> Windows --> Console Application). I am the author of the former.

我有两个项目 - 一个是网站(文件 - >新网站),另一个是控制台应用程序(文件 - >新项目 - > Windows - >控制台应用程序)。我是前者的作者。

The standalone app fakes the input by hardcoding it, runs through some code, and creates an output. It uses dlls from a local installation of software that I have installed on my machine to generate this output.

独立应用程序通过硬编码来伪造输入,运行一些代码并创建输出。它使用我在我的机器上安装的本地软件安装的dll来生成此输出。

I read on MSDN that I cannot add a console app to a website solution in a useful manner. So, if I compile the console app to output a dll instead of an exe, can I reference that dll in my website? How can I do this exactly? I would need to pass the input value from the website to the dll, and return meaningful results from the dll. Is this possible?

我在MSDN上读到,我无法以有用的方式将控制台应用程序添加到网站解决方案中。所以,如果我编译控制台应用程序输出一个DLL而不是一个exe,我可以在我的网站中引用该DLL吗?我怎么能这样做呢?我需要将输入值从网站传递给dll,并从dll返回有意义的结果。这可能吗?

1 个解决方案

#1


1  

Yes, you describe a feasible way to solve this. You need to create a class library project, add source code from console application to it, except the the class that has static Main method and modify (add to) that source code such that there is a class that you will be able instantiate from the code in your web application after you add the class library assembly to the web application as a reference. This class will have a method with appropriate parameters, that you will call. All this assuming that the task that console application code performs is fast and will not create noticeable delay in the web application response. If the task takes a long time, you will either have to run it in a background thread or move it outside the web application - the latter is significantly more involved.

是的,您描述了解决此问题的可行方法。您需要创建一个类库项目,从控制台应用程序添加源代码,除了具有静态Main方法的类,并修改(添加到)该源代码,以便有一个类,您可以从该实例化实例化将类库程序集作为参考添加到Web应用程序后,Web应用程序中的代码。这个类将有一个具有适当参数的方法,您将调用它。所有这些都假设控制台应用程序代码执行的任务很快,并且不会在Web应用程序响应中产生明显的延迟。如果任务需要很长时间,您将需要在后台线程中运行它或将其移到Web应用程序之外 - 后者更加复杂。

#1


1  

Yes, you describe a feasible way to solve this. You need to create a class library project, add source code from console application to it, except the the class that has static Main method and modify (add to) that source code such that there is a class that you will be able instantiate from the code in your web application after you add the class library assembly to the web application as a reference. This class will have a method with appropriate parameters, that you will call. All this assuming that the task that console application code performs is fast and will not create noticeable delay in the web application response. If the task takes a long time, you will either have to run it in a background thread or move it outside the web application - the latter is significantly more involved.

是的,您描述了解决此问题的可行方法。您需要创建一个类库项目,从控制台应用程序添加源代码,除了具有静态Main方法的类,并修改(添加到)该源代码,以便有一个类,您可以从该实例化实例化将类库程序集作为参考添加到Web应用程序后,Web应用程序中的代码。这个类将有一个具有适当参数的方法,您将调用它。所有这些都假设控制台应用程序代码执行的任务很快,并且不会在Web应用程序响应中产生明显的延迟。如果任务需要很长时间,您将需要在后台线程中运行它或将其移到Web应用程序之外 - 后者更加复杂。