2016-11-27 79 views
0

我的文件结构是这样创建可建造,安装并启动Windows服务

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

MyWindowsService取决于MyMVCProject,因为我想一个ASP.NET MVC构建过程,当我的MVC应用程序开始运行它有一个后台进程,用于连接MVP项目中定义的存储库和数据上下文。

我做MyWindowsService自行安装时建立,因为我把

CD C:\ WINDOWS \ Microsoft.NET \框架\ 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); 

因此,如果(2)编译Windows Service项目(因为它取决于(1)) (1)编译MVC项目 3)启动MVC应用程序,其中在开始开始建(2)

但是该服务,当我这样做,我发现了错误

enter image description here

任何想法,我做错了什么,以及如何解决它?

+0

我不知道你最终想做什么,但从MVC应用程序启动Windows服务听起来不像我想要的方向。 –

+0

Windows服务不是通过运行exe来启动的。它们已经安装完毕(如你所做的那样),然后通过服务管理工具或通过“net start/stop”命令行启动/停止。即使重新启动,它们仍然是一项服务,并且通常在机器启动时自行重启,所以我不知道为什么您的MVC应用程序甚至会触摸安装或启动/停止服务。这听起来像是一件危险的事情。每个操作通常应运行的安全上下文是不同的。 –

+0

我认为你正在寻找[Hangfire.io](https://github.com/HangfireIO/Hangfire) – trailmax

回答

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应用程序,通常它不是运行最佳实践例如它需要管理员权限。

相关问题