В общем, вызываю в Main:
var pathToExe = Process.GetCurrentProcess().MainModule.FileName;
Directory.SetCurrentDirectory(Path.GetDirectoryName(pathToExe));
А далее метод:
public static IWebHostBuilder CreateHostBuilder(string[] args, bool isService)
{
var config = new ConfigurationBuilder()
.AddEnvironmentVariables()
.AddInMemoryCollection(new[] { new KeyValuePair<string, string>("IsService", isService.ToString()) })
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.Build();
return WebHost.CreateDefaultBuilder(args).ConfigureLogging((context, logging) =>
{
logging.ClearProviders();
logging.AddSerilog(new LoggerConfiguration()
.ReadFrom.Configuration(context.Configuration)
.CreateLogger());
})
.UseConfiguration(config)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIIS()
.UseStartup<Startup>();
}
И когда пытаюсь все это запустить в IIS, то ловлю ошибку:
Unhandled exception. System.IO.FileNotFoundException: The configuration file 'appsettings.json' was not found and is not optional. The physical path is 'c:\windows\system32\inetsrv\appsettings.json'.
Почему он ищет конфигурационный файл не в той папке? Я же указал путь к базовой директории...