【问题标题】:.NET Core MVC app runs locally but not in container (System.DirectoryServices.Protocols not supported).NET Core MVC 应用程序在本地运行,但不在容器中(不支持 System.DirectoryServices.Protocols)
【发布时间】:2019-10-25 20:34:42
【问题描述】:

我们使用 Visual Studio 2017 的项目模板“.NET Core Web Application (Model-View-Controller)”创建了一个应用程序,选择“Change Authentication->Individual User Accounts”

然后我们开发了应用程序并准备推送我们的第一个容器。

发布到文件夹后,我们通过运行在发布文件夹内进行测试

dotnet nameof.dll

所有测试都通过了。来自docker log的片段:

Hosting environment: Production
Content root path: \publish
Now listening on: http://localhost:5000
Application started.  Press Ctrl+C to shut down.

到目前为止一切顺利。

然后我们尝试用这个Dockerfile 构建一个简单的容器:

FROM microsoft/aspnetcore:2
WORKDIR app
EXPOSE 80
COPY  publish .
CMD ["dotnet", "nameof.dll"]

该网站出现了,但有一个警告。

warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
      No XML encryptor configured. Key {8f452e97-17ae-45cb-bcfb-d97a81ce1199} may be persisted to storage in unencrypted form.
Hosting environment: Production
Content root path: /app
Now listening on: http://[::]:80
Application started. Press Ctrl+C to shut down.

除此之外,一切看起来都很好。浏览到网站。看起来不错。

当我们尝试登录时抛出此异常:

fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[0]
      An unhandled exception has occurred: System.DirectoryServices.Protocols is not supported on this platform.
System.PlatformNotSupportedException: System.DirectoryServices.Protocols is not supported on this platform.
   at System.DirectoryServices.Protocols.DirectoryIdentifier..ctor()
   at System.DirectoryServices.Protocols.LdapDirectoryIdentifier..ctor(String server)
   at CycleMenu.Controllers.AccountController.<Login>d__11.MoveNext() in C:\gg\AramarkCycleMenu\src\CycleMenu\CycleMenu\Controllers\AccountController.cs:line 78
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeActionMethodAsync>d__12.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeNextActionFilterAsync>d__10.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeInnerFilterAsync>d__14.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeNextResourceFilter>d__22.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeFilterPipelineAsync>d__17.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeAsync>d__15.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.<Invoke>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>d__6.MoveNext()

很明显,docker 映像中缺少某些内容,但如果它不在发布文件夹中,那么它在哪里?这怎么能在容器外面而不在里面工作?

我们尝试过

  • 使用不同的基础图像构建
  • Linux 和 Windows 容器
  • 添加 Microsoft.Windows.Compatibility(我丢失了指导我们这样做的文章的链接)
  • 我们的头撞在墙上。

根据这个讨论:https://github.com/dotnet/corefx/issues/2089 System.DirectoryServices.Protocols 最初不包含在 .NET Core 中,但现在(2017 年 11 月)。可能是 docker hub 上的图像还不是最新的,这就是为什么我们会得到“...此平台不支持”?

运行 dotnet --version 在容器内部和外部都返回 2.1.4。

您能否提供任何帮助或见解如何克服此错误并将原本可以正常工作的代码正确地放入 docker 映像中?

编辑回答:在 Windows 10 上运行 Docker for Windows

【问题讨论】:

  • 容器主机上运行的是什么操作系统?
  • @DavidG:添加答案

标签: docker .net-core


【解决方案1】:

我在基于mcr.microsoft.com/dotnet/core/aspnet:3.0 的dotnet core 3.0 docker 容器中使用Nuget 包System.DirectoryServices.Protocols 版本4.6.0 时遇到了同样的问题。

但是,正如我们在您提到的问题中看到的那样,将不支持 Windows 以外的其他平台:https://github.com/dotnet/corefx/issues/2089#issuecomment-373219911

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-29
    • 2020-01-15
    • 1970-01-01
    • 1970-01-01
    • 2019-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多