简体   繁体   English

创建一个ASP.NET MVC构建过程,该过程可以构建,安装和启动Windows服务

[英]Creating a ASP.NET MVC build process that builds, installs and starts a Windows Service

My file structure is like 我的文件结构像

MyMVCProject
  MyWindowsService
    bin
    obj
    .
    .
  MyMVCProject
    App_Data
    App_Start
    bin
    .
    .
  packages
  MyMVCProject.sln

and MyWindowsService depends on MyMVCProject because I want that when my MVC application starts running it has a background process that interfaces my repositories and data contexts defined in the MVP project. MyWindowsService取决于MyMVCProject因为我希望在我的MVC应用程序开始运行时,它具有一个后台进程,该进程连接我在MVP项目中定义的存储库和数据上下文。

I made MyWindowsService install itself when it builds because I put 我让MyWindowsService在构建时自行安装,因为我把

cd C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319 installutil.exe "$(SolutionDir)\\MyWindowsService\\bin\\$(ConfigurationName)\\MyWindowsService.exe" cd C:\\ Windows \\ Microsoft.NET \\ Framework \\ v4.0.30319 installutil.exe“ $(SolutionDir)\\ MyWindowsService \\ bin \\ $(ConfigurationName)\\ MyWindowsService.exe”

in the post-build command line for that project. 在该项目的构建后命令行中。 It seems to work when I build that project by itself because I get output like 当我自己构建该项目时,它似乎可以正常工作,因为我得到的输出像

1>  
1>  Beginning the Install phase of the installation.
1>  See the contents of the log file for the C:\Users\me\Documents\Visual Studio 2013\Projects\MyMVCProject\MyWindowsService\bin\Debug\MyWindowsService.exe assembly's progress.
1>  The file is located at C:\Users\me\Documents\Visual Studio 2013\Projects\MyMVCProject\MyWindowsService\bin\Debug\MyWindowsService.InstallLog.
1>  Installing assembly 'C:\Users\me\Documents\Visual Studio 2013\Projects\MyMVCProject\MyWindowsService\bin\Debug\MyWindowsService.exe'.
1>  Affected parameters are:
1>     logtoconsole = 
1>     logfile = C:\Users\me\Documents\Visual Studio 2013\Projects\MyMVCProject\MyWindowsService\bin\Debug\MyWindowsService.InstallLog
1>     assemblypath = C:\Users\me\Documents\Visual Studio 2013\Projects\MyMVCProject\MyWindowsService\bin\Debug\MyWindowsService.exe
1>  No public installers with the RunInstallerAttribute.Yes attribute could be found in the C:\Users\me\Documents\Visual Studio 2013\Projects\MyMVCProject\MyWindowsService\bin\Debug\MyWindowsService.exe assembly.
1>  
1>  The Install phase completed successfully, and the Commit phase is beginning.
1>  See the contents of the log file for the C:\Users\me\Documents\Visual Studio 2013\Projects\MyMVCProject\MyWindowsService\bin\Debug\MyWindowsService.exe assembly's progress.
1>  The file is located at C:\Users\me\Documents\Visual Studio 2013\Projects\MyMVCProject\MyWindowsService\bin\Debug\MyWindowsService.InstallLog.
1>  Committing assembly 'C:\Users\me\Documents\Visual Studio 2013\Projects\MyMVCProject\MyWindowsService\bin\Debug\MyWindowsService.exe'.
1>  Affected parameters are:
1>     logtoconsole = 
1>     logfile = C:\Users\me\Documents\Visual Studio 2013\Projects\MyMVCProject\MyWindowsService\bin\Debug\MyWindowsService.InstallLog
1>     assemblypath = C:\Users\me\Documents\Visual Studio 2013\Projects\MyMVCProject\MyWindowsService\bin\Debug\MyWindowsService.exe
1>  No public installers with the RunInstallerAttribute.Yes attribute could be found in the C:\Users\me\Documents\Visual Studio 2013\Projects\MyMVCProject\MyWindowsService\bin\Debug\MyWindowsService.exe assembly.
1>  Remove InstallState file because there are no installers.
1>  
1>  The Commit phase completed successfully.
1>  

I'm then trying to start the service in the App_Start of my MVC project like 然后,我试图在MVC项目的App_Start中启动服务,例如

        string serviceExecutablePath = Path.GetFullPath(
            Path.Combine(
                AppDomain.CurrentDomain.BaseDirectory,
                @"..\MyWindowsService",
                "bin",
                isDebugMode ? "Debug" : "Release",
                "MyWindowsService.exe"
            )
        );
        Process.Start(serviceExecutablePath);

so that if I build-and-run or build-and-debug the entire solution then the steps should be like 因此,如果我构建并运行或构建并调试整个解决方案,则步骤应类似于

(1) Compile MVC project (2) Compile Windows Service project (because it depends on (1)) (3) Start MVC app, which in starts starts the service built in (2) (1)编译MVC项目(2)编译Windows Service项目(因为它取决于(1))(3)启动MVC应用,该应用将在启动时启动内置的服务(2)

However, when I do so I'm getting the error 但是,当我这样做时,我得到了错误

在此处输入图片说明

Any idea what I'm doing wrong and how to fix it? 知道我在做什么错以及如何解决吗?

It seems that your service isn't installed on the server. 您的服务似乎未安装在服务器上。 The best way to do this if the service is not installed is to create a command line executable (bat file) and install and start it from there. 如果未安装该服务,最好的方法是创建一个命令行可执行文件(bat文件),然后从那里安装并启动它。

Then executing that file can be done like this: 然后可以像这样执行该文件:

Process serverSideProcess = new Process();
serverSideProcess.StartInfo.FileName = @"C:\pathToTheExe";
serverSideProcess.StartInfo.Arguments = "arg1 arg2 ..."; 
serverSideProcess.EnableRaisingEvents = true;
serverSideProcess.StartInfo.UseShellExecute = true;
serverSideProcess.Start();

Of course you can do the installation and running from your app but remember it is a web app and usually it is not a best practice to run such things as it requires admin privilege. 当然,您可以从您的应用程序进行安装和运行,但是请记住,它是一个Web应用程序,并且通常不是运行这种需要管理员特权的操作的最佳实践。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM