Visual Studioのプロジェクトから実行可能ファイルを作成してサービスを作成したいのですが(コンソールウィンドウを必要とせずに実行できます)。Windowsサービス(Visual Studioで作成したexe)を起動するとエラー1053が発生する
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
@STLDeveloper OnStartイベントハンドラがありません。元の投稿に追加したStartup.csクラスがあります。以前はWindowsサービスを作成したことはありません。オンラインで見たものから、実行可能ファイルからWindowsサービスを作成することは可能です。 – Roka545
可能ですが、アプリケーションの構造に関して非常に特定のガイドラインに従わなければなりません。 – STLDeveloper