使用Topshelf部署.net core windows服務 首先新建一個.net core的模板worker程式 過程 略 打開Program.cs namespace TopshelfDemo { public class Program { public static void Main(s ...
使用Topshelf部署.net core windows服務
首先新建一個.net core的模板worker程式
過程 略
打開Program.cs
namespace TopshelfDemo
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<Worker>();
});
}
}
nuget 安裝Topshelf
略
改寫下Program.cs
public class Program
{
public static void Main(string[] args)
{
HostFactory.Run(x =>
{
x.Service<IHost>(s =>
{
s.ConstructUsing(() => CreateHostBuilder(args).Build());
s.WhenStarted(service =>
{
service.Start();
});
s.WhenStopped(service =>
{
service.StopAsync();
});
});
x.StartAutomatically();
x.RunAsLocalSystem();
x.SetServiceName("TopshelfDemo.ServiceName");
x.SetDisplayName("TopshelfDemo.DisplayName");
x.SetDescription("TopshelfDemo.Description");
});
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<Worker>();
});
}
該寫法的實現原理:通過通用主機方法 IHost 實現管理主機生存期
寫法如下:
public class Program
{
private IHost _host;
public Program()
{
_host = new HostBuilder()
.Build();
}
public async Task StartAsync()
{
_host.StartAsync();
}
public async Task StopAsync()
{
using (_host)
{
await _host.StopAsync(TimeSpan.FromSeconds(5));
}
}
}
打包
安裝 TopshelfDemo
找到相應的文件夾
TopshelfDemo.exe install
啟動 TopshelfDemo
TopshelfDemo.exe start
卸載 TopshelfDemo
TopshelfDemo.exe uninstall
.NET 通用主機通用配置
配置Configuration
Host.CreateDefaultBuilder(args)
.ConfigureHostConfiguration(configHost =>
{
configHost.SetBasePath(Directory.GetCurrentDirectory());
configHost.AddJsonFile("hostsettings.json", optional: true);
configHost.AddEnvironmentVariables(prefix: "PREFIX_");
configHost.AddCommandLine(args);
});
環境名稱
Host.CreateDefaultBuilder(args)
.UseEnvironment("Development")
//...
logging引入
.ConfigureLogging((context, logger) =>
{
logger.AddConsole();
//logger.AddLog4Net();
logger.SetMinimumLevel(LogLevel.Information);
})
依賴註入
public class ToDoWork
{
public string TimeNow()
{
return DateTime.Now.ToString();
}
}
program.cs
.ConfigureServices((hostContext, services) =>
{
services.AddScoped<ToDoWork>();
services.AddHostedService<Worker>();
});
program.cs 最終如下
using System.IO;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Topshelf;
namespace TopshelfDemo
{
public class Program
{
public static void Main(string[] args)
{
HostFactory.Run(x =>
{
x.Service<IHost>(s =>
{
s.ConstructUsing(() => CreateHostBuilder(args).Build());
s.WhenStarted(service =>
{
service.Start();
});
s.WhenStopped(service =>
{
service.StopAsync();
});
});
x.StartAutomatically();
x.RunAsLocalSystem();
x.SetServiceName("TopshelfDemo.ServiceName");
x.SetDisplayName("TopshelfDemo.DisplayName");
x.SetDescription("TopshelfDemo.Description");
});
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder(args)
.UseEnvironment(Environments.Development)
//.UseEnvironment(Environments.Production)
//.UseEnvironment(Environments.Staging)
.ConfigureHostConfiguration(configHost =>
{
configHost.SetBasePath(Directory.GetCurrentDirectory());
configHost.AddJsonFile("hostsettings.json", optional: true);
configHost.AddEnvironmentVariables(prefix: "PREFIX_");
configHost.AddCommandLine(args);
})
.ConfigureLogging((context, logger) =>
{
logger.AddConsole();
//logger.AddLog4Net();
logger.SetMinimumLevel(LogLevel.Information);
})
.ConfigureServices((hostContext, services) =>
{
services.AddScoped<ToDoWork>();
services.AddHostedService<Worker>();
});
}
}