我有一个问题。我在.net core 2.2中有一个Web API,它在Linux ubuntu 18.04 os下运行。
要在服务器重新启动时启动此服务,请将此过程放入rc.local文件中(例如,这是那里的唯一记录,api仅使用数据库,而没有其他内容)
但是当服务器要启动Web API时,在服务器重启时,我遇到了异常:
Unhandled Exception: System.IO.IOException: Invalid argument
at System.IO.Enumeration.FileSystemEnumerator`1.FindNextEntry()
at System.IO.Enumeration.FileSystemEnumerator`1.MoveNext()
at System.IO.FileSystemWatcher.RunningInstance.AddDirectoryWatchUnlocked(WatchedDirectory parent, String directoryName)
message repeated 5 times: [ at System.IO.FileSystemWatcher.RunningInstance.AddDirectoryWatchUnlocked(WatchedDirectory parent, String directoryName)]
at System.IO.FileSystemWatcher.RunningInstance..ctor(FileSystemWatcher watcher, SafeFileHandle inotifyHandle, String directoryPath, Boolean includeSubdirectories, NotifyFilters notifyFilters, CancellationToken cancellationToken)
at System.IO.FileSystemWatcher.StartRaisingEvents()
at System.IO.FileSystemWatcher.StartRaisingEventsIfNotDisposed()
at System.IO.FileSystemWatcher.set_EnableRaisingEvents(Boolean value)
at Microsoft.Extensions.FileProviders.Physical.PhysicalFilesWatcher.TryEnableFileSystemWatcher()
at Microsoft.Extensions.FileProviders.Physical.PhysicalFilesWatcher.CreateFileChangeToken(String filter)
at Microsoft.Extensions.Primitives.ChangeToken.OnChange(Func`1 changeTokenProducer, Action changeTokenConsumer)
at Microsoft.Extensions.Configuration.FileConfigurationProvider..ctor(FileConfigurationSource source)
at Microsoft.Extensions.Configuration.Json.JsonConfigurationSource.Build(IConfigurationBuilder builder)
at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()
at Microsoft.AspNetCore.Hosting.WebHostBuilder.BuildCommonServices(AggregateException& hostingStartupErrors)
at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
at LiveServer.WebApi.Program.BuildWebHost(String[] args) in /mnt/c/Workspace/Playground/LiveServer/LiveServer.WebApi/Program.cs:line 15
at LiveServer.WebApi.Program.Main(String[] args) in /mnt/c/Workspace/Playground/LiveServer/LiveServer.WebApi/Program.cs:line 11
因此,在mu程序cs中看起来好像有问题,并且看起来是:
public class Program
{
private static Logger logger = LogManager.GetLogger("Startup");
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args)
{
try
{
return WebHost.CreateDefaultBuilder(args)
.UseUrls(GetWebApiUrl())
.UseKestrel()
.UseStartup<Startup>()
.Build();
}
catch (Exception ex)
{
var msg = $"Error while starting WebApi: Error - {ex.Message} Inner Error - {ex.InnerException.Message} Stack - {ex.StackTrace} InnerStack {ex.InnerException.Message}";
logger.Fatal(msg);
throw new Exception(msg);
}
}
private static string GetWebApiUrl()
{
var assemblyPathInfo = new FileInfo(Assembly.GetEntryAssembly()?.Location ?? throw new InvalidOperationException());
var configurationFilePath = Path.Combine(assemblyPathInfo.DirectoryName, "appsettings.json");
var config = new ConfigurationBuilder().AddJsonFile(configurationFilePath, optional: false).Build();
var webApiUrl = config["WebApi:URL"];
return webApiUrl;
}
}
appsettings.json如下:
{
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Warning"
}
},
"Console": {
"LogLevel": {
"Default": "Warning"
}
}
},
"WebApi": {
"URL": "http://*:5001"
}
}
我可以推断出某些东西阻止了json设置文件,但我可能是错的。但是有趣的是,当我通过dot net命令手动运行此服务器时,或者在服务器启动后通过启动rc.local文件运行它时,都没有问题。