【发布时间】:2017-05-10 21:51:10
【问题描述】:
我有一个从 Visual Studio 中的项目创建的可执行文件,我想用它创建一个服务(这样我就可以在不需要控制台窗口的情况下运行它)。我发布项目,并使用以下方法创建 Windows 服务:
sc create MY.SERVICE binpath= "C:\Program Files\Project\serviceProj\myService.exe
服务按预期显示在 Windows 服务管理器中。但是,每当我尝试启动该服务时,它会在大约 2 秒后失败并给我以下错误:
Windows could not start the MY.SERVICE on Local Computer.
Error 1053: The service did not respond to the start or control request in a timely fashion.
我做过的事情:
在 Visual Studio 中从调试更改为发布
以管理员身份运行一切(创建服务、发布项目、启动服务等)。
我还在某处读到过,增加服务管理器等待服务启动的时间可能会起作用。我添加了 Windows 注册表值来做到这一点,但不幸的是它不起作用。
从命令提示符启动服务通常只需 2-3 秒即可启动并开始侦听请求,因此我不确定发生了什么。
感谢任何帮助。
这是我的 Startup.cs 类:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Serialization;
using Microsoft.AspNetCore.Hosting.WindowsServices;
using System.Diagnostics;
using System.IO;
using Serilog;
using System.Linq;
namespace My.Service
{
public class Startup
{
public static void Main(string[] args)
{
var exePath = Process.GetCurrentProcess().MainModule.FileName;
var directoryPath = Path.GetDirectoryName(exePath);
if (Debugger.IsAttached || args.Contains("--debug"))
{
var host = new WebHostBuilder()
.CaptureStartupErrors(true)
.UseKestrel()
.UseUrls("http://localhost:5002")
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
else
{
var host = new WebHostBuilder()
.UseKestrel()
.UseUrls("http://localhost:5002")
.UseContentRoot(directoryPath)
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.RunAsService();
}
}
public Startup(IHostingEnvironment env)
{
//Setup Logger
Log.Logger = new LoggerConfiguration()
.WriteTo.Trace()
.MinimumLevel.Debug()
.CreateLogger();
// Set up configuration sources.
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json");
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; set; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver =
new CamelCasePropertyNamesContractResolver();
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime lifetime)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}");
});
}
}
}
【问题讨论】:
-
请告诉我们更多关于您的服务的信息。您的 OnStart 事件处理程序应该尽快完成它的工作并退出,而且它肯定应该在 30 秒内完成。您是否在 OnStart 处理程序中执行长时间运行的任务?也许发布一些代码?
-
@STLDeveloper 我没有 OnStart 事件处理程序 - 应该吗?我有一个已添加到原始帖子中的 Startup.cs 类。我以前从未创建过 Windows 服务 - 根据我在网上看到的情况,听起来可以从可执行文件创建 Windows 服务。
-
是的,很有可能,但它必须遵循一套关于应用程序结构的非常具体的指导方针。
-
您在网上看到的很可能是在谈论服务包装器,例如 srvany 或 nssm,它们的唯一工作就是运行其他可执行文件。这通常被总结为(有点误导)“将可执行文件作为服务运行”。
标签: c# visual-studio visual-studio-2015 service windows-services