Thank for the help. I have reverted back to use IWebHostBuilder.
After migrating from IWebHostBuilder to IHostBuilder, the ASP.NET Core API is unable to access the 'applicationUrl' configuration from 'launchSettings.json'.
Hello,
I recently transitioned my ASP.NET Core API from utilizing IWebHostBuilder to IHostBuilder as part of an upgrade to align with modern .NET practices. Post-migration, I'm facing a challenge: the API fails to launch correctly in both development environments and within Linux Docker containers for production deployment. This issue arises specifically when the 'config.Sources.Clear()' command is active in the 'ConfigureAppConfiguration' method, which is essential for custom configuration management. I need guidance on how to successfully run the API with 'config.Sources.Clear()' in place, ensuring that it works seamlessly across different environments while adhering to the enhanced configuration patterns introduced by IHostBuilder.
Exception :
Exception error -
Failed to bind to address http://127.0.0.1:5000: address already in use.
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.<BindEndpointAsync>d__3.MoveNext()
at Microsoft.AspNetCore.Server.Kestrel.Core.LocalhostListenOptions.<BindAsync>d__2.MoveNext()
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.DefaultAddressStrategy.<BindAsync>d__0.MoveNext()
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.<BindAsync>d__0.MoveNext()
at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.<BindAsync>d__33.MoveNext()
at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.<StartAsync>d__30`1.MoveNext()
at Microsoft.AspNetCore.Hosting.GenericWebHostService.<StartAsync>d__37.MoveNext()
at Microsoft.Extensions.Hosting.Internal.Host.<StartAsync>d__12.MoveNext()
at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.<RunAsync>d__4.MoveNext()
at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.<RunAsync>d__4.MoveNext()
at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost host)
at AIProducerBackend.Web.Program.Main(String[] args) in C:\Users\SubirDas\source\repos\AIProducerBackend\SubirLocalRepo\AiProducerBackend.Web\Program.cs:line 28
Inner exception
Only one usage of each socket address (protocol/network address/port) is normally permitted.
at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketConnectionListener.Bind()
at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketTransportFactory.BindAsync(EndPoint endpoint, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure.TransportManager.<BindAsync>d__9.MoveNext()
at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.<>c__DisplayClass30_0`1.<<StartAsync>g__OnBind|0>d.MoveNext()
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.<BindEndpointAsync>d__3.MoveNext()
Reupdate:
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((webHostBuilderContext, configurationBuilder) =>
{
var env = webHostBuilderContext.HostingEnvironment;
DotNetEnv.Env.LoadMulti(new[] {
$".env",
$".env.{env.EnvironmentName}"
});
// Clear default IHostBuilder config providers
configurationBuilder.Sources.Clear();
configurationBuilder
.AddJsonFile($"appsettings.json", optional: false)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables("APP_");
})
.UseStartup<Startup>();
Post update:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
var env = hostingContext.HostingEnvironment;
config.Sources.Clear();
DotNetEnv.Env.LoadMulti(new[] {
$".env",
$".env.{env.EnvironmentName}"
});
config
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables("APP_");
})
.UseSerilog((hostingContext, loggerConfiguration) =>
loggerConfiguration
.ReadFrom.Configuration(hostingContext.Configuration)
.Enrich.FromLogContext()
.Enrich.WithExceptionDetails()
.WriteTo.Console(new JsonFormatter())
)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});