Method not found: 'Microsoft.Extensions.Primitives.StringValues Microsoft.AspNetCore.Http.IHeaderDictionary.get_Item

Steven Dwyer 126 Reputation points
2021-03-30T20:46:25+00:00

Hello,
I am using Visual Studio 2019 to develop Azure Functions on .net Core 3.1. I have a particular Azure function which uses and Http trigger. In this function, I am getting some header information from the HttpRequest object:

public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)], HttpRequest req, ILogger log)
...
var token = req.Headers[tokenName];
...

When this function is triggered I get the following error:

Method not found: 'Microsoft.Extensions.Primitives.StringValues Microsoft.AspNetCore.Http.IHeaderDictionary.get_Item(System.String)'

This error is seen in the Azure portal for the Function resource. It is important to note that when developing and testing locally, I do not get this issue and the function completes successfully as expected.

Here are a few settings on my Azure Functions resource which will be useful.
FUNCTIONS_EXTENSION_VERSION = ~3
FUNCTIONS_WORKER_RUNTIME = dotnet

The loaded packages that I am using for this project are all the latest:
C:\Users\<user>\source\repos\<project>\AzureFunctions>dotnet list AzureFunctions.csproj package
Project 'AzureFunctions' has the following package references
[netcoreapp3.1]:
Top-level Package Requested Resolved

Azure.Messaging.ServiceBus 7.1.1 7.1.1

Microsoft.Azure.ServiceBus 5.1.2 5.1.2

Microsoft.Azure.WebJobs.Extensions.ServiceBus 4.2.1 4.2.1

Microsoft.Azure.WebJobs.Extensions.Storage 4.0.4 4.0.4

Microsoft.Extensions.Caching.StackExchangeRedis 5.0.1 5.0.1

Microsoft.NET.Sdk.Functions 3.0.11 3.0.11

Newtonsoft.Json 13.0.1 13.0.1

System.Drawing.Common 5.0.2 5.0.2

System.ServiceModel.Http 4.8.1 4.8.1

I have seen this article which is my exact issue with the exception that they were using Azure Function Runtime 2.0
https://github.com/Azure/azure-functions-core-tools/issues/774
I could not see how I could use this solution for my issue.

I am not sure if this is playing a part, but previous to getting this issue I did have another issue where the Microsoft.Extensions.Primitives.dll was not being found. I was able to resolve this by adding the following line in my project file:
<_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>

Does anyone have any thoughts as to what I could be doing wrong?

Any insight would be very much appreciated.

Many thanks,
Steve

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,117 questions
0 comments No comments
{count} votes

Accepted answer
  1. Steven Dwyer 126 Reputation points
    2021-03-31T15:52:17.143+00:00

    All packages are upgraded to the latest in my Function App. I went ahead and created a new simpler project that has less dependencies than my original project. This new project does a simple HTTP trigger that obtains some header information using this as before.

    var token = req.Headers[tokenName];

    This worked.

    The only package that this new project has is the Microsoft.Net.Sdk.Functions (3.0.11) package. I found that one of its dependencies is Microsoft.Extensions.Primitives (2.1.6). In my original project that is having the issue, I am also using the Microsoft.Net.Sdk.Functions (3.0.11) package, however this package is showing that the Microsoft.Extensions.Primitives dependency is at version 5.0.0. This is because I have another package (Microsoft.Extensions.Caching.StackExchangeRedis 5.0.1) that requires the Microsoft.Extensions.Primitives to be at version 5.0.0. I believe this is my cause of the issue. I then rolled the Microsoft.Extensions.Caching.StackExchangeRedis package back from 5.0.1 to 3.1.13 which requires Microsoft.Extensions.Primitives 3.1.13 and then everything started to work.

    @MayankBargali-MSFT ... thank you again for your help.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. MayankBargali-MSFT 70,536 Reputation points
    2021-03-31T12:21:51.287+00:00

    @Steven Dwyer Have you tried upgrading all your packages. Did that help you to resolve the issue? There might be chances that any of the packages that you are using might be using in your project have dependencies of the unsupported version for functions as mentioned in github issue. What happens when you create a new project with HTTP trigger? Do you observe the same issue with the default HTTP trigger so it will help if there are any dependencies on your existing project causing this issue.


  2. Kabilan VK 101 Reputation points
    2021-05-21T16:22:42.237+00:00

    I got into the same exact issue with another library that tried to bump up the primitives version to 5.x. If .Net core resolved the binding redirect problem then how this issue comes now. So, it's not gone completely even in the core.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.