Ber*_*ger 7 c# windows-services asp.net-core
当将 Asp.Net Core 应用程序作为 Windows 服务运行(使用 .net Core 兼容包)时,您必须获取与正常运行不同的应用程序数据路径。正如这里所描述的,你必须这样做:
string pathToContentRoot = Directory.GetCurrentDirectory();
if (isService) {
string pathToExe = Process.GetCurrentProcess().MainModule.FileName;
pathToContentRoot = Path.GetDirectoryName(pathToExe);
}
Run Code Online (Sandbox Code Playgroud)
并设置 Webhostbuilder 的基本路径(缩短):
IConfigurationRoot config = new ConfigurationBuilder()
.SetBasePath(pathToContentRoot)
.Build();
IWebHost host = WebHost.CreateDefaultBuilder(args)
.UseConfiguration(config)
.UseStartup<Startup>()
.Build();
Run Code Online (Sandbox Code Playgroud)
然而问题是,当您想在ConfigureServices(...) 方法中访问Startup.cs 中的此路径时,我找不到方法来执行此操作。
我尝试过以下事情:
配置服务仅接受 IServiceCollection,因此这是不允许的:
public void ConfigureServices(IServiceCollection services, IHostingEnvironment env) {...}
Run Code Online (Sandbox Code Playgroud)
此时 HostingEnvironment 尚未设置:
public Startup(IHostingEnvironment env) {
HostingEnvironment = env; // <-- is null
}
Run Code Online (Sandbox Code Playgroud)
这里,Content Root 路径是"C:\Windows\System32",而不是 Program.cs 中提供的路径
string contentRoot = services.BuildServiceProvider()
.GetService<IHostingEnvironment>()
.ContentRootPath;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3361 次 |
最近记录: |