gpt4 book ai didi

Docker 从 Dotnet Core 2.0 应用程序正常关闭

转载 作者:行者123 更新时间:2023-12-05 00:12:36 26 4
gpt4 key购买 nike

我有一个 .NET Core 2.0 控制台应用程序在本地 Docker Linux 容器(Docker CE 18.03.1 稳定 channel )中运行,我正在尝试处理正常关闭。

我的应用程序有两个处理程序 AppDomain.CurrentDomain.ProcessExitAssemblyLoadContext.Default.Unloading :

        AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit;
AssemblyLoadContext.Default.Unloading += Default_Unloading;

处理程序在被调用时简单地写入日志消息。

我发现,当我做 docker stop <containerid> 时,在短暂的延迟之后,两个处理程序都不会被调用。容器只是停止运行。我尝试在 Visual Studio 中调试应用程序,并尝试在没有调试器的情况下运行。在所有情况下,都不会调用处理程序。

有没有我遗漏的其他魔法咒语?

最佳答案

我无法重现这种行为。

这是我在控制台应用程序中使用的代码:

    private static readonly AutoResetEvent _closing = new AutoResetEvent(false);

static void Main(string[] args)
{
AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit;
AssemblyLoadContext.Default.Unloading += Default_Unloading;

Task.Factory.StartNew(() =>
{
while (true)
{
Console.WriteLine(DateTime.Now.ToString());
Thread.Sleep(1000);
}
});
Console.CancelKeyPress += new ConsoleCancelEventHandler(OnExit);
_closing.WaitOne();
}

private static void Default_Unloading(AssemblyLoadContext obj)
{
Console.WriteLine("unload");
}

private static void CurrentDomain_ProcessExit(object sender, EventArgs e)
{
Console.WriteLine("process exit");
}

protected static void OnExit(object sender, ConsoleCancelEventArgs args)
{
Console.WriteLine("Exit");
_closing.Set();
}

使用此 docker 文件:
FROM microsoft/dotnet:2.0-sdk
WORKDIR /app

# copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# copy and build everything else
COPY . ./
RUN dotnet publish -c Release -o out
ENTRYPOINT ["dotnet", "out/shutdown.dll"]

这是输出:(与 docker stop <id> )
06/01/2018 16:31:16
06/01/2018 16:31:17
06/01/2018 16:31:18
unload
process exit

我正在将 Docker for Windows 与 Linux 容器一起使用。

关于Docker 从 Dotnet Core 2.0 应用程序正常关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50646549/

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