我发现了很多有关此问题的条目,但没有解决方案对我有用。
简而言之,我有一个.net Core 3.1控制台应用程序,我想在Docker中运行。如果我在Visual Studio中或通过命令行启动应用程序,则一切正常(调试和发布版本)。
如果将其放在Docker容器中,则会出现以下异常:
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)
我的Program.cs看起来像这样:
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();
}
}
}
还有我的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();
}
}
}
在Program.cs的这一行抛出异常:
var dataStorage = _serviceProvider.GetService<IDataStorageService>();
在Startup.cs中,行var configService = serviceProvider.GetService<IConfigService>();
在工作。
正如我提到的那样,所有这些都在Visual Studio中运行和工作,或者如果我在CLI中启动命令行工具。 仅在Docker中发生异常。 这是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"]
感谢您的帮助!
答案 0 :(得分:0)
@jandrew 感谢您的评论。 我无法使用docs(https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-3.1#call-services-from-main)1:1中的方法,因为我正在构建.net核心控制台应用程序而不是asp.net核心应用程序。
我使用ConfigureServices
代替了ConfigureWebHostDefaults
。对我来说很好