在 Docker 中 运行 项目时获得 'System.TypeInitializationException'

Getting 'System.TypeInitializationException' when running project in Docker

我有一个任务,我必须将我的 CSOM .NET CORE 2.2 项目放在 docker 中。

该项目包含一个控制台应用程序,该应用程序调用更新分类字段的 CSOM 项目 (dll)。

当我 运行 项目独立运行时,它运行良好。

当我将项目放入 Docker 容器并尝试 运行 时,我得到以下结果

System.AggregateException: One or more errors occurred. (The type initializer for 'Microsoft.Win32.Registry' threw an exception.) ---> System.TypeInitializationException: The type initializer for 'Microsoft.Win32.Registry' threw an exception. ---> System.PlatformNotSupportedException: Registry is not supported on this platform.

这是我的 Docker 文件

FROM microsoft/aspnetcore-build:2.0
FROM microsoft/dotnet:2.2-aspnetcore-runtime
FROM microsoft/dotnet:2.2-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/WebJobCore.dll"]

这是控制台应用程序中的部分代码

    public static void Main(string[] args)
    {

        Console.WriteLine("Testing webjob");

        Program t = new Program();
        t.RunSynchronizer().Wait();
    }

    public async Task RunSynchronizer()
    {
        var traceWriter = new ConsoleTraceWriter();
        var taxonomyFactory = new ItaTaxonomyFactory();
        var synchronizer = new ItaTaxonomySynchronizer(traceWriter, taxonomyFactory);

        Stopwatch stopWatch = new Stopwatch();
        stopWatch.Start();

        await synchronizer.SyncAll();

        stopWatch.Stop();

        TimeSpan ts = stopWatch.Elapsed;
        // Format and display the TimeSpan value.
        string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
            ts.Hours, ts.Minutes, ts.Seconds,
            ts.Milliseconds / 10);
        Console.WriteLine("My RunTime " + elapsedTime);
    }

这是来自外部同步器 dll 的代码

    public async Task SyncAll()
    {
        using (ClientContext context = new ClientContext(ConfigSettings.SiteUrl))
        {

            context.Credentials = new SharePointOnlineCredentials(
                "username",
                "password");
            var taxonomyService = TaxonomySession.GetTaxonomySession(context);
            var termStore = taxonomyService.GetDefaultSiteCollectionTermStore();
            context.Load(taxonomyService);
            context.Load(termStore);
            await context.ExecuteQueryAsync();
            }
         }

这是我的 docker 构建命令

docker build -t mylatestimage .

这是我的docker运行命令

docker run mylatestimage

当我 运行 控制台应用程序的可执行文件或 Visual Studio 中的项目本身时,一切 运行 都很好。当我尝试 运行 docker 时,我只得到 'testing webjob' 输出并且程序在 await synchronizer.SyncAll() 行产生上述错误

请告诉我如何修复它

非常感谢您的帮助

在我切换到 Docker 上的 Windows 容器并使用此命令关闭 Nano Server 后才有可能

docker pull mcr.microsoft.com/windows/nanoserver