gpt4 book ai didi

asp.net-core-2.0 - 作为 Windows 服务托管的 .net 核心 - 如何使用配置文件?

转载 作者:行者123 更新时间:2023-12-04 19:27:37 26 4
gpt4 key购买 nike

我有一个 .Net Core 2 WebAPI,我希望它作为 Windows 服务运行。
默认情况下,该服务在 http://localhost:5000 上运行。当我在本地运行它时,我使用 hosting.json 文件在那里设置 IP 和端口,但由于某种原因,当我尝试使用配置文件时,该服务不会启动。

这是我的代码:

public static void Main(string[] args)
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile("hosting.json", optional: false, reloadOnChange: true)
.AddCommandLine(args)
.Build();

if (Debugger.IsAttached || args.Contains("--console"))
BuildWebHost(config, args).Run();
else
BuildServiceWebHost(config, args).RunAsService();
}

public static IWebHost BuildWebHost(IConfigurationRoot config, string[] args)
{
return WebHost.CreateDefaultBuilder(args)
.UseConfiguration(config)
.UseStartup<Startup>()
.Build();
}

public static IWebHost BuildServiceWebHost(IConfigurationRoot config, string[] args)
{
var pathToExe = Process.GetCurrentProcess().MainModule.FileName;
var pathToContentRoot = Path.GetDirectoryName(pathToExe);

var webHostArgs = args.Where(arg => arg != "--console").ToArray();

return WebHost.CreateDefaultBuilder(webHostArgs)
.UseConfiguration(config)
.UseContentRoot(pathToContentRoot)
.UseStartup<Startup>()
.Build();
}

它在调试上运行(例如,不作为服务),但是当我将它设置为 Windows 服务 sc create MyService binPath="..." 并尝试运行它时,我收到此错误:
Windows could not start the MyService service on Local Computer.

Error 1053:
The service did not respond to the start or control request in a timely fashion.

我检查了 Event Viewer 并发现了这个错误: Exception Info: System.IO.FileNotFoundException: The configuration file 'appsettings.json' was not found and is not optional. The physical path is 'C:\WINDOWS\system32\appsettings.json'.
作为服务运行时,如何将配置文件的路径设置为与 .exe 相同?

最佳答案

我设法通过以下方式解决了这个问题:

using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;

namespace WebAPI
{
public class Program
{
public static void Main(string[] args)
{
if (Debugger.IsAttached || args.Contains("--console"))
BuildWebHost(args).Run();
else
{
var webHost = BuildServiceWebHost(args);

var webHostService = new CustomWebHostService(webHost);
ServiceBase.Run(webHostService);
}
}

public static IWebHost BuildWebHost(string[] args)
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile("hosting.json", optional: false, reloadOnChange: true)
.AddCommandLine(args)
.Build();

return WebHost.CreateDefaultBuilder(args)
.UseConfiguration(config)
.UseStartup<Startup>()
.Build();
}

public static IWebHost BuildServiceWebHost(string[] args)
{
var pathToExe = Process.GetCurrentProcess().MainModule.FileName;
var pathToContentRoot = Path.GetDirectoryName(pathToExe);

var webHostArgs = args.Where(arg => arg != "--console").ToArray();

JObject hosting = JsonConvert.DeserializeObject<JObject>(File.ReadAllText($"{pathToContentRoot}/hosting.json"));
return WebHost.CreateDefaultBuilder(webHostArgs)
.UseUrls(hosting.Value<string>("server.urls"))
.UseContentRoot(pathToContentRoot)
.UseStartup<Startup>()
.Build();
}
}
}

希望这可以帮助其他人遇到同样的问题。

关于asp.net-core-2.0 - 作为 Windows 服务托管的 .net 核心 - 如何使用配置文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51304106/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com