1

I'm having the above error after running an azure function called "Test" that redirects to an external URL of a service we want to use.

 [FunctionName("Test")]
        public IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequest req)
        {
            Log.Information("C# HTTP trigger function processed a request.");
            
            string url = _authenticationService.GetAuthorizationUri().ToString();
            return new RedirectResult(url);

        }

The site at the URL prompts the user to authorize use of their data and performs a redirect to the previously authorized url of our "AuthorizationCallback", along with a query string parameter.

[FunctionName("AuthorizationCallback")]
        public async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req)
        {
            Log.Information("C# HTTP trigger function processed a request.");

            string code = req.Query["code"];
            try
            {
                if (!string.IsNullOrEmpty(code))
                { 
                    await _authenticationService.ExchangeCodeForAccessToken(code);
                    return new OkResult();
                }
            }
            catch (System.Exception)
            {
                return new UnauthorizedResult();
            }
            return new NotFoundResult();
        }

The AuthorizationCallback function is hit but produces the following error in the console: enter image description here

These are the dependencies of the current project on the solution (which is set as the startup project): These are my deps

I've tried installing both the latest stable version (5.0.0) and the version before that (3.1.13) of Microsoft.Extensions.Primitives in the current project, but I'm still getting the same error. I've noticed the package that can't be loaded is within microsoft.azure.webjobs (3.0.23), which is within microsoft.azure.webjobs.extensions.storage (4.0.4), but these are used in another project entirely, for another azure function (blob triggered). Any ideas on how to overcome this error? Thank you all.

GUZZ
  • 61
  • 5

1 Answers1

4

The Azure Functions host for .NET Core 3 uses an in-process hosting model, which essentially means you are limited in what versions of Microsoft assemblies you can use. What's happening is that something in your project has a reference to a newer version of Microsoft.Extensions.Primitives, but an older version of that library is already loaded by the Azure Functions host application.

For Azure Functions .NET Core 3, you should restrict all Microsoft.Extensions.* libraries to v3.x. You currently have Microsoft.Extensions.DependencyInjection 5.0.1, which should be changed to 3.x. Check for any other Microsoft.Extensions.* libraries either at the Packages level or anywhere beneath (tip: you can find them quickly by putting Microsoft.Extensions in the input box at the top of the Solution Explorer). You may need to downgrade some other library that has Microsoft.Extensions.Primitives as a dependency.

You might also be able to get away with manually writing a bindingRedirect pointing the newer version to an older version. The Microsoft.Extensions.* packages are relatively stable across versions, so that may work. It would make me very nervous, though.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • 2
    I had a related issue accessing the HttpRequest Query object - Microsoft.Extensions.Primitives v5.0.0 was being referenced by a rogue Microsoft.Extensions.Logging and Microsoft.Extensions.Caching.Memory v5 reference. Dropping them to v3 made it work. I was also having issues with logging and memory cache too - probably not unrelated :). Much googling without success until this answer so dropping a few keywords so I can find this again next month!! Thanks! – kidshaw Aug 28 '21 at 09:21