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

Mr.*_*. T 5 docker .net-core

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

我的应用程序具有用于AppDomain.CurrentDomain.ProcessExit和的处理程序AssemblyLoadContext.Default.Unloading

        AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit;
        AssemblyLoadContext.Default.Unloading += Default_Unloading;
Run Code Online (Sandbox Code Playgroud)

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

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

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

Mat*_*ias 4

我无法重现这种行为。

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

    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();
    }
Run Code Online (Sandbox Code Playgroud)

使用这个泊坞窗文件:

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"]
Run Code Online (Sandbox Code Playgroud)

这是输出:(带有docker stop <id>

06/01/2018 16:31:16
06/01/2018 16:31:17
06/01/2018 16:31:18
unload
process exit
Run Code Online (Sandbox Code Playgroud)

我正在将 Docker for Windows 与 Linux 容器结合使用。