【问题标题】:Creating a ASP.NET MVC build process that builds, installs and starts a Windows Service创建用于构建、安装和启动 Windows 服务的 ASP.NET MVC 构建过程
【发布时间】:2016-11-27 21:39:43
【问题描述】:

我的文件结构是这样的

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

MyWindowsService 依赖于MyMVCProject,因为我希望当我的 MVC 应用程序开始运行时,它有一个后台进程来连接我在 MVP 项目中定义的存储库和数据上下文。

我让MyWindowsService 在构建时自行安装,因为我放了

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

在该项目的构建后命令行中。当我自己构建该项目时,它似乎可以工作,因为我得到了类似的输出

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>  

然后我尝试在我的 MVC 项目的 App_Start 中启动服务,例如

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

因此,如果我构建并运行或构建并调试整个解决方案,那么步骤应该类似于

(1) 编译MVC项目 (2)编译Windows Service项目(因为它依赖于(1)) (3) 启动MVC app,其中starts启动了(2)中内置的服务

但是,当我这样做时,我得到了错误

知道我做错了什么以及如何解决吗?

【问题讨论】:

  • 我不知道你最终想用这个做什么,但是从 MVC 应用程序启动 Windows 服务听起来不像是我想要采取的方向。
  • 运行 exe 无法启动 Windows 服务。它们已安装(正如您所做的那样),然后通过服务管理工具或通过“net start/stop”命令行启动/停止。即使重新启动它们仍然是服务,并且通常在机器启动时重新启动,所以我不知道为什么您的 MVC 应用程序甚至会触及服务的安装或启动/停止。这听起来像是一件危险的事情。每个操作通常应该在其中运行的安全上下文是不同的。
  • 我想你在找Hangfire.io
  • 或者如果你坚持服务,你可能想看看Topshelf
  • 如果您输入“NET START myServiceName”(其中 myServiceName 是您的服务的名称),它会启动吗?

标签: c# asp.net .net asp.net-mvc visual-studio


【解决方案1】:

您的服务似乎没有安装在服务器上。如果未安装该服务,最好的方法是创建一个命令行可执行文件(bat 文件)并从那里安装和启动它。

然后执行该文件可以这样完成:

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

当然,您可以从您的应用程序中进行安装和运行,但请记住,它是一个 Web 应用程序,并且通常运行这样的东西不是最佳做法,因为它需要管理员权限。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-27
    • 1970-01-01
    • 2015-03-21
    • 1970-01-01
    • 2011-04-06
    相关资源
    最近更新 更多