Google-api-dotnet-client: GetFallbackPolicyAsync does not have an implementation

Created on 4 Aug 2019  路  26Comments  路  Source: googleapis/google-api-dotnet-client

Using dotnet core version 3 (Preview 7) results in the error

System.Reflection.ReflectionTypeLoadException: 'Unable to load one or more of the requested types.
Method 'GetFallbackPolicyAsync' in type 'Google.Apis.Auth.AspNetCore.GoogleScopedPolicyProvider' from assembly 'Google.Apis.Auth.AspNetCore, Version=1.40.2.0, Culture=neutral, PublicKeyToken=4b01fa6e34db77ab' does not have an implementation.'

What I am trying to do is pull back blog posts first, and then I want to integrate with some other APIs. I do have the latest Google.Apis libraries from NuGet. It was working with 3.0 Preview 5, but ASP.NET core has a new @code that is replacing @function in razor pages.

The full trace is

   at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
   at System.Reflection.RuntimeAssembly.get_DefinedTypes()
   at Microsoft.AspNetCore.Mvc.ApplicationParts.AssemblyPart.get_Types()
   at Microsoft.AspNetCore.Mvc.Controllers.ControllerFeatureProvider.PopulateFeature(IEnumerable`1 parts, ControllerFeature feature)
   at Microsoft.AspNetCore.Mvc.ApplicationParts.ApplicationPartManager.PopulateFeature[TFeature](TFeature feature)
   at Microsoft.AspNetCore.Mvc.ApplicationModels.ControllerActionDescriptorProvider.GetControllerTypes()
   at Microsoft.AspNetCore.Mvc.ApplicationModels.ControllerActionDescriptorProvider.GetDescriptors()
   at Microsoft.AspNetCore.Mvc.ApplicationModels.ControllerActionDescriptorProvider.OnProvidersExecuting(ActionDescriptorProviderContext context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.DefaultActionDescriptorCollectionProvider.UpdateCollection()
   at Microsoft.AspNetCore.Mvc.Infrastructure.DefaultActionDescriptorCollectionProvider.Initialize()
   at Microsoft.AspNetCore.Mvc.Infrastructure.DefaultActionDescriptorCollectionProvider.GetChangeToken()
   at Microsoft.AspNetCore.Mvc.Routing.ActionEndpointDataSourceBase.<>c__DisplayClass11_0.<Subscribe>b__0()
   at Microsoft.Extensions.Primitives.ChangeToken.ChangeTokenRegistration`1..ctor(Func`1 changeTokenProducer, Action`1 changeTokenConsumer, TState state)
   at Microsoft.Extensions.Primitives.ChangeToken.OnChange(Func`1 changeTokenProducer, Action changeTokenConsumer)
   at Microsoft.AspNetCore.Mvc.Routing.ActionEndpointDataSourceBase.Subscribe()
   at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionEndpointDataSource..ctor(IActionDescriptorCollectionProvider actions, ActionEndpointFactory endpointFactory)
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite singletonCallSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass1_0.<RealizeService>b__0(ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider)
   at Microsoft.AspNetCore.Builder.RazorPagesEndpointRouteBuilderExtensions.EnsureRazorPagesServices(IEndpointRouteBuilder endpoints)
   at Microsoft.AspNetCore.Builder.RazorPagesEndpointRouteBuilderExtensions.MapFallbackToPage(IEndpointRouteBuilder endpoints, String page)
needs investigating p2 bug

Most helpful comment

@chrisdunelm Any updates on this? we are pretty blocked with .net core 3.0 migrations, and .net core 2.2 is touching the "End Of Life" in 2 weeks (23/12/19)

All 26 comments

Assigned to Chris to investigate. I know there are some breaking changes in ASP.NET Core 3, but I wouldn't expect us to fall foul of them.

Update: tried with 1.40.3

Unable to load one or more of the requested types.
Method 'GetFallbackPolicyAsync' in type 'Google.Apis.Auth.AspNetCore.GoogleScopedPolicyProvider' from assembly 'Google.Apis.Auth.AspNetCore, Version=1.40.3.0, Culture=neutral, PublicKeyToken=4b01fa6e34db77ab' does not have an implementation.

Though I'm sure that without knowing this issue when it was developed that a new version might or might not have addressed it.

It looks like this is due to the GetFallbackPolicyAsync method having been added to the IAuthorizationPolicyProvider interface in ASP.NET Core 3.
Which means the GoogleScopedPolicyProvider class now doesn't fully implement the interface.
I can't immediately find any docs about this new method, but there is a SO question about it here: https://stackoverflow.com/questions/56688483/asp-net-core-3-implement-iauthorizationpolicyprovider

I'm not able to immediately investigate this further, but expect to get to it within a week or two.

aspnet/Announcements#375 is the announcement, its safe to just return null always for this method if the implementation doesn't want to add support for the concept of fallback policy. It might also be reasonable to just return the DefaultPolicy in the short term too.

Also, unless the AuthorizationMiddleware is being used, generally the fallback policy won't be used at all so the implementation does not matter much.

Might be easiest to just return null.

Returning null seems to cause problems. I'm migrating from 2.2 to 3.0 preview 8 and I had to implement the interface for our custom IAuthorizationPolicyProvider. First implementation was this:

    public Task<AuthorizationPolicy> GetFallbackPolicyAsync()
    {
        return null;
    }

But:

System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.AspNetCore.Authorization.AuthorizationPolicy.CombineAsync(IAuthorizationPolicyProvider policyProvider, IEnumerable1 authorizeData) at Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter.GetEffectivePolicyAsync(AuthorizationFilterContext context) at Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter.OnAuthorizationAsync(AuthorizationFilterContext context) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker) at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger) at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context) at NSwag.AspNetCore.Middlewares.SwaggerUiIndexMiddleware1.Invoke(HttpContext context)
at NSwag.AspNetCore.Middlewares.RedirectToIndexMiddleware.Invoke(HttpContext context)
at NSwag.AspNetCore.Middlewares.SwaggerDocumentMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.SetRoutingAndContinue(HttpContext httpContext)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

I changed the implementation to the following and got past the Object reference not set to an instance of an object:

    public Task<AuthorizationPolicy> GetFallbackPolicyAsync()
    {
        var result = new AuthorizationPolicyBuilder();
        result = result.RequireAssertion(context =>
        {
            return true;
        });

        return Task.FromResult(result.Build());
    }

Hi there, it's been a while. Is there something I can do on my end to fix this? I would like to be able to use the Google Api again without having to drop back to 2.x.

This bug is the only reason why I can't yet upgrade to 3.0. Is there an ETA?

I'm sorry that we haven't had time to work on this. It's possible that we could just call GetFallbackPolicyAsync on our default policy provider. With so little documentation provided, however, it's not clear whether that's actually the correct thing to do.
While we'd definitely like to get this working with 3.0, we have other higher priorities at the moment.

What we might be able to do is create a beta release which just delegates to the default policy provider, and let users try that at their own risk. Note that even that will take a bit of work as we'll need to have a 3.0 target in order to call GetFallbackPolicyAsync on our default provider :( I'll talk with @chrisdunelm about how to proceed; feedback about whether a speculative beta makes sense would be welcome.

I've been looking into this today, and I suspect we're going to have to have two separate packages, i.e. introduce Google.Apis.Auth.AspNetCore3 or something similar. While I've managed to tease the current code into implementing the interface for GetFallbackPolicyAsync, there are issues due to the dependencies on 2.x assemblies. A clean break to AspNetCore3 is likely to be simpler. Will try that this afternoon and keep updating the issue with progress.

Okay, well there's significant progress, but we're far from done, I'm afraid.

Steps taken:

  • Copy the Google.Apis.Auth.AspNetCore project (as AspNetCore3) and upgrade it to ASP.NET Core 3
  • Copy the Google.Apis.Auth.AspNetCore.IntegrationTests project (as AspNetCore3.IntegrationTests) and upgrade it to ASP.NET Core 3
  • Tweak routing etc
  • Run the tests

Results:

  • Logout: pass (in that it forces login again for the other tests)
  • Show auth tokens: pass
  • Test scope listing: pass
  • Test storage bucket listing: 404 for Account/AccessDenied; I may be missing a setup step here
  • Translate: after authorization, throws a SecurityTokenException: Unable to validate the 'id_token', no suitable ISecurityTokenValidator was found for: ''."
  • Force token refresh: pass

Curiously, the ASP.NET Core 2.0 integration tests don't work at all for me at the moment - I just go into a login cycle. Not sure what's going on there.

I'll consult with @chrisdunelm on Monday.

Thank you for the update

So... what did @chrisdunelm say last Monday?

We've got quite a bit of work to do on fixing integration tests, then preparing the new ASP.NET3.0 package.
I'm fairly sure we now intend to do this work, but this won't be immediate. I would expect by early/mid-November we might have a beta package ready.
This issue will be updated with progress.

@chrisdunelm Any updates on this? we are pretty blocked with .net core 3.0 migrations, and .net core 2.2 is touching the "End Of Life" in 2 weeks (23/12/19)

@chrisdunelm Any updates on this? we are pretty blocked with .net core 3.0 migrations, and .net core 2.2 is touching the "End Of Life" in 2 weeks (23/12/19)

Any update eon this, please? :(

Apologies, we've been held up with some other priorities; I expect to be looking at this over the next few days.

Also please note that although .NET Core 2.2 is very nearly EOL, .NET Core 2.1 is the LTS version, with support through to August 2021 (ref: https://dotnet.microsoft.com/platform/support/policy/dotnet-core)

I am running into this issue, is there any progress?

@KuzonCode: Yes, there's a beta of the new package: https://www.nuget.org/packages/Google.Apis.Auth.AspNetCore3/1.43.0-beta01
Sorry that we hadn't put that in this issue!

Not sure why we hadn't closed this issue before - basically, the solution is "use the Google.Apis.Auth.AspNetCore3 package when targeting ASP.NET Core 3".
I haven't seen any feedback saying there are any problems with this, so I suspect we'll release a GA version soon.

(From my side, upgrading to .NET Core 3.0 is not something we do on an afternoon. So yeah we haven't done it yet, but it's scheduled)

@Doidel: Very much acknowledged. If you run into any problems when you do so, please file a new bug :)

Any fix for these issue?
I'm using net core 3.1.0 and GoogleApi 1.43.0

@jmmccota: Yes, as per the previous comments, use the new Google.Apis.Auth.AspNetCore3 package.

Hi, I wanted to upgrade from .NET 2.2 + .NET Standard 2.0 to new .NET Core 3.1 with class library on .NET Standard 2.1 - When I try to use Google.Apis.Auth.AspNetCore, I got the GetFallbackPolicyAsync error, when I use Google.Apis.Auth.AspNetCore3 - it says it's not compatible with netstandard...
The package is used inside the class library, therefore that netstandard is needed.
What should I do?

@jakubbloksa: This would be better as a new issue, but to answer it briefly - there's nothing we can really do here. Google.Apis.Auth.AspNetCore3 depends on Microsoft.AspNetCore.Authentication.OpenIdConnect, which targets netcoreapp3.0.

That said, you can target netcoreapp3.0 (or netcoreapp3.1) in your library. Just because it's a library doesn't mean you have to target netstandard - as is proved by both Google.Apis.Auth.AspNetCore3 and Microsoft.AspNetCore.Authentication.OpenIdConnect being libraries that target netcoreapp...

Was this page helpful?
0 / 5 - 0 ratings