Asp.net core 使用.NET内核和命令行创建Windows服务安装程序
我使用以下示例创建了一个ASP.NET核心Windows服务应用程序: 现在,我想使用命令行安装我的服务,比如“MyService.exe-install”。我知道如果我使用“sc create”会起作用,但是我想使用我自己的应用程序来安装和配置服务 我发现了一个使用ToSelf包的示例,但我使用的是WebHostBuilder,我尝试使用自定义服务配置。该服务已安装,但启动时无法运行 我正在使用.NETCore2.2 我的配置:Asp.net core 使用.NET内核和命令行创建Windows服务安装程序,asp.net-core,windows-services,topshelf,Asp.net Core,Windows Services,Topshelf,我使用以下示例创建了一个ASP.NET核心Windows服务应用程序: 现在,我想使用命令行安装我的服务,比如“MyService.exe-install”。我知道如果我使用“sc create”会起作用,但是我想使用我自己的应用程序来安装和配置服务 我发现了一个使用ToSelf包的示例,但我使用的是WebHostBuilder,我尝试使用自定义服务配置。该服务已安装,但启动时无法运行 我正在使用.NETCore2.2 我的配置: HostFactory.Run(x =>
HostFactory.Run(x =>
{
x.Service<CustomWebHostService>(sc =>
{
sc.ConstructUsing(() => new CustomWebHostService(host));
// the start and stop methods for the service
sc.WhenStarted(ServiceBase.Run);
sc.WhenStopped(s => s.Stop());
});
x.RunAsLocalSystem();
x.StartAutomatically();
x.SetServiceName("Teste Core");
x.SetDisplayName("Teste ASP.NET Core Service");
x.SetDescription("Teste ASP.NET Core as Windows Service.");
});
HostFactory.Run(x=>
{
x、 服务(sc=>
{
sc.ConstructUsing(()=>新的CustomWebHostService(主机));
//服务的启动和停止方法
sc.WhenStarted(ServiceBase.Run);
sc.停止时(s=>s.停止());
});
x、 RunAsLocalSystem();
x、 StartAutomatically();
x、 SetServiceName(“测试核心”);
x、 SetDisplayName(“Teste ASP.NET核心服务”);
x、 SetDescription(“Teste ASP.NET Core作为Windows服务”);
});
我的完整源代码:
我像其他示例一样更改了代码,我的服务现在作为控制台应用程序和windows服务运行。可以使用命令行“MyService.exe install”安装它 My program.cs文件:
class Program
{
public static void Main(string[] args)
{
HostFactory.Run(x =>
{
x.Service<ApplicationHost>(sc =>
{
sc.ConstructUsing(() => new ApplicationHost());
// the start and stop methods for the service
sc.WhenStarted((svc, control) =>
{
svc.Start(control is ConsoleRunHost, args);
return true;
});
sc.WhenStopped(s => s.Stop());
sc.WhenShutdown(s => s.Stop());
});
x.UseNLog();
x.RunAsLocalSystem();
x.StartAutomatically();
x.SetServiceName("Test Core");
x.SetDisplayName("Test ASP.NET Core Service");
x.SetDescription("Test ASP.NET Core as Windows Service.");
});
}
}
类程序
{
公共静态void Main(字符串[]args)
{
HostFactory.Run(x=>
{
x、 服务你知道如何在.NET Core 3.1上实现同样的功能吗?
public class ApplicationHost
{
private IWebHost _webHost;
public void Start(bool launchedFromConsole, string[] args)
{
if (!launchedFromConsole)
{
var pathToExe = Process.GetCurrentProcess().MainModule.FileName;
var pathToContentRoot = Path.GetDirectoryName(pathToExe);
Directory.SetCurrentDirectory(pathToContentRoot);
}
IWebHostBuilder webHostBuilder = CreateWebHostBuilder(args);
_webHost = webHostBuilder.Build();
_webHost.Start();
// print information to console
if (launchedFromConsole)
{
var serverAddresses = _webHost.ServerFeatures.Get<IServerAddressesFeature>()?.Addresses;
foreach (var address in serverAddresses ?? Array.Empty<string>())
{
Console.WriteLine($"Listening on: {address}");
Console.WriteLine("Press Ctrl+C to end the application.");
}
}
}
public void Stop()
{
_webHost?.Dispose();
}
private IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddEventLog();
})
.ConfigureAppConfiguration((context, config) =>
{
// Configure the app here.
})
.UseUrls("http://+:8000")
.UseStartup<Startup>();
}