gpt4 book ai didi

c# - Docker容器中的.net核心控制台应用程序引发System.ObjectDisposedException

转载 作者:行者123 更新时间:2023-12-02 18:35:02 29 4
gpt4 key购买 nike

我发现有很多关于此问题的条目,但是没有解决方案对我有用。

简而言之,我有一个要在Docker中运行的.net Core 3.1控制台应用程序。如果我在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"]

谢谢你的帮助!

最佳答案

@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。对我来说很好

关于c# - Docker容器中的.net核心控制台应用程序引发System.ObjectDisposedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60745131/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com