aspnet/DataProtection

System.PlatformNotSupportedException: Registry is not supported on this platform.

todorgrigorov opened this issue · 10 comments

Here is a part of the full exception trace:

Autofac.Core.DependencyResolutionException: An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = XmlKeyManager (ReflectionActivator), Services = [Microsoft.AspNetCore.DataProtection.KeyManagement.IKeyManager], Lifetime = Autofac.Core.Lifetime.RootScopeLifetime, Sharing = Shared, Ownership = OwnedByLifetimeScope ---> An exception was thrown while invoking the constructor 'Void .ctor(Microsoft.Extensions.Options.IOptions'1[Microsoft.AspNetCore.DataProtection.KeyManagement.KeyManagementOptions], Microsoft.AspNetCore.DataProtection.Internal.IActivator, Microsoft.Extensions.Logging.ILoggerFactory)' on type 'XmlKeyManager'. ---> The type initializer for 'Microsoft.Win32.Registry' threw an exception. (See inner exception for details.) (See inner exception for details.) ---> Autofac.Core.DependencyResolutionException: An exception was thrown while invoking the constructor 'Void .ctor(Microsoft.Extensions.Options.IOptions'1[Microsoft.AspNetCore.DataProtection.KeyManagement.KeyManagementOptions], Microsoft.AspNetCore.DataProtection.Internal.IActivator, Microsoft.Extensions.Logging.ILoggerFactory)' on type 'XmlKeyManager'. ---> The type initializer for 'Microsoft.Win32.Registry' threw an exception. (See inner exception for details.) ---> System.TypeInitializationException: The type initializer for 'Microsoft.Win32.Registry' threw an exception. ---> System.PlatformNotSupportedException: Registry is not supported on this platform.

I am using Autofac as service provider.
The workaround for now is to register my MVC components via .AddMvcCore().

Can you provide a sample as a github repository that reproduces the bug (with instructions on how to run it)?

Sure, here is a sample:

https://github.com/todorgrigorov/aspnetcore-registry-proof

Just restore and run..
I have a feeling that this problem is somehow relate to aspnet/Logging#691 because the error changes if I do not provide custom registration for ILoggerFactory.

I'm assuming you're not on windows?

I'm on Mac but I got the same exception on Windows 10 as well.

Just ran this on windows 10 and i worked:

C:\Users\dfowler\Documents\GitHub\bugs\aspnetcore-registry-proof [master ≡]> dotnet run
info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
      User profile is available. Using 'C:\Users\dfowler\AppData\Local\ASP.NET\DataProtection-Keys' as key repository an
d Windows DPAPI to encrypt keys at rest.
Hosting environment: Production
Content root path: C:\Users\dfowler\Documents\GitHub\bugs\aspnetcore-registry-proof
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 17.5617ms 200
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/favicon.ico
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 0.5134ms 200

My application is using docker containers so I guess the issue came from executing it in a Linux environment?

That would make more sense as the error is specifically complaining about the registry...

I can reproduce an issue on OSX, it's this line of code:

builder.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource());

The custom autofac registration source is breaking ASP.NET Core. Why do you need it?

It's likely because of this:

https://github.com/aspnet/DataProtection/blob/dev/src/Microsoft.AspNetCore.DataProtection/Internal/KeyManagementOptionsSetup.cs#L34

This constructor is suddenly resolvable because the AnyConcreteTypeNotAlreadyRegisteredSource creates a concrete type for it. The code here is assuming that this type and consequently the constructor is never invoked on non windows machines because of this

if (OSVersionUtil.IsWindows())
{
services.TryAddSingleton<RegistryPolicyResolver>();
}
.

Interesting. Will have to find a way around the registration source now. Thanks.

PS: Closing the thread as it is not related to this repo anymore.