Search code examples
c#docker.net-corecsom

Getting 'System.TypeInitializationException' when running project in Docker


I have an assignment where I have to put my CSOM .NET CORE 2.2 project inside the docker.

The project consists of a console application that calls CSOM project (dll) that updates taxonomy fields.

When I run the project on its own it works fine.

When I put the project inside the Docker container and try to run it I get the following

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.

Here is my Docker file

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"]

Here is the part of the code in the console app

    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);
    }

Here is the code from the external synchronizer 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();
            }
         }

Here is my docker build command

docker build -t mylatestimage .

Here is my docker run command

docker run mylatestimage

When I run the executable of a console app or the project itself within the Visual Studio, everything runs fine. When I try to run the docker, I ONLY get the 'testing webjob' output and the program breaks at the await synchronizer.SyncAll() line producing the aforementioned error

Please let me know how I could fix it

Thank you very much for your help


Solution

  • It was possible after I switched to Windows containers on the Docker and pulled down Nano Server using this command

    docker pull mcr.microsoft.com/windows/nanoserver