.NET 6 以上的 Create Host 的写法和以前版本不一样了,直接都是写在了Program.cs 里面。
当我们用Windows Service 的方式去 Hosting 我们的 service时,会遇见 Error:1053, 或因为配置文件路径不正确而引起的 ERROR:1053等问题。只需要在 Program里面,create host builder 的时候加入一些参数即可。
var options = new WebApplicationOptions
{
Args = args,
ContentRootPath = WindowsServiceHelpers.IsWindowsService()
? AppContext.BaseDirectory
: default
};
var builder = WebApplication.CreateBuilder(options);
builder.Host.UseWindowsService();