i found many entries to this issue but no solution worked for me.
In short i have a .net Core 3.1 console application which i want to run in Docker. If i start the Application in Visual Studio or via the commandline all works fine (Debug and Release build).
If i put it in a Docker container, i get the following exception:
Unhandled exception. System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'IServiceProvider'.
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ThrowHelper.ThrowObjectDisposedException()
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider)
My Program.cs looks like this:
using System;
using System.Runtime.Loader;
using System.Threading;
using Microsoft.Extensions.DependencyInjection;
namespace testapp
{
internal class Program
{
private static IServiceProvider _serviceProvider;
private static void Main(string[] args)
{
Startup.Init(out _serviceProvider);
var ended = new ManualResetEventSlim();
var starting = new ManualResetEventSlim();
var dataStorage = _serviceProvider.GetService<IDataStorageService>();
AssemblyLoadContext.Default.Unloading += ctx =>
{
Console.WriteLine("Unloding fired");
starting.Set();
Console.WriteLine("Waiting for completion");
ended.Wait();
};
Console.WriteLine("Waiting for signals");
starting.Wait();
Console.WriteLine("Received signal gracefully shutting down");
Thread.Sleep(5000);
ended.Set();
}
}
}
And my Startup.cs
using System;
using System.Collections.Specialized;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Sinks.Elasticsearch;
namespace testapp
{
public static class Startup
{
public static void Init(out IServiceProvider serviceProvider)
{
Injection(out serviceProvider);
}
private static void Injection(out IServiceProvider serviceProvider)
{
var builder = new HostBuilder()
.ConfigureServices((hostContext, services) =>
{
services.AddSingleton<IDataStorageService, DataStorageService>();
services.AddLogging(configure => configure.AddSerilog());
}).Build();
serviceProvider = builder.Services;
var configService = serviceProvider.GetService<IConfigService>();
Console.WriteLine("ElasticUrl: " + configService.GetElasticEndpoint());
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.WriteTo.File("consoleapp.log")
.WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(configService.GetElasticEndpoint()))
{
AutoRegisterTemplate = true,
TypeName = "_doc",
ModifyConnectionSettings = c => c.GlobalHeaders(new NameValueCollection
{{"Authorization", configService.GetElasticAuthentication()}})
})
.CreateLogger();
builder.RunAsync();
}
}
}
The Exception is thrown at this line in Program.cs:
var dataStorage = _serviceProvider.GetService<IDataStorageService>();
In the Startup.cs the line var configService = serviceProvider.GetService<IConfigService>();
is working.
As i mentioned, all is running and working in visual Studio or if i launch the commandline tool within the CLI. Only in Docker the Exception occures. Here is the DockerFile:
FROM mcr.microsoft.com/dotnet/core/runtime:3.1
COPY bin/Release/netcoreapp3.1/publish/ app/
ENV TZ=Europe/Berlin
ENV ASPNETCORE_ENVIRONMENT=Production
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
ENTRYPOINT ["dotnet", "app/testapp.dll"]
thanks for any help!