Azure Functions CLI - such a pretty logo for such an awesome functionality

Azure Function build fails with “Mono.Cecil.AssemblyResolutionException: Failed to resolve assembly: Microsoft.AspNetCore.Mvc.Core”

2 min read.

Another fun one with Azure Functions! At least it’s a simple one: this time, I ran into a weird issue while doing some code reusing – well, copy-pasting – between a couple of different projects.

I was lovingly hand-crafting some Azure Functions, while suddenly the Azure Functions host would throw me this in ugly, red letters:

Mono.Cecil.AssemblyResolutionException: Failed to resolve assembly: 'Microsoft.AspNetCore.Mvc.Core, Version=3.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'
   at Mono.Cecil.BaseAssemblyResolver.Resolve(AssemblyNameReference name, ReaderParameters parameters)
   at Mono.Cecil.BaseAssemblyResolver.Resolve(AssemblyNameReference name)
   at Mono.Cecil.DefaultAssemblyResolver.Resolve(AssemblyNameReference name)
   at Mono.Cecil.MetadataResolver.Resolve(TypeReference type)
   at Mono.Cecil.ModuleDefinition.Resolve(TypeReference type)
   at Mono.Cecil.TypeReference.Resolve()
   at MakeFunctionJson.AttributeExtensions.IsWebJobsAttribute(CustomAttribute attribute)
   at MakeFunctionJson.ParameterInfoExtensions.<>c.<ToFunctionJsonBindings>b__1_0(CustomAttribute a)
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.ToList()
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at MakeFunctionJson.ParameterInfoExtensions.ToFunctionJsonBindings(ParameterDefinition parameterInfo)
   at MakeFunctionJson.MethodInfoExtensions.<>c.<ToFunctionJson>b__6_1(ParameterDefinition p)
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.ToArray()
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at MakeFunctionJson.MethodInfoExtensions.ToFunctionJson(MethodDefinition method, String assemblyPath)
   at MakeFunctionJson.FunctionJsonConverter.GenerateFunctions(IEnumerable`1 types)+MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at MakeFunctionJson.FunctionJsonConverter.TryGenerateFunctionJsons()
   at MakeFunctionJson.FunctionJsonConverter.TryRun()
Error generating functions metadata
	C:\Users\[southwindadmin]\.nuget\packages\microsoft.net.sdk.functions\3.0.13\build\Microsoft.NET.Sdk.Functions.Build.targets	32

Reason

Table of Contents

I’m sure there could be multiple different reasons this happens – but there’s one that I’ve stumbled into multiple times, so that’s the one I’ll document the solution to. 😉

You’re using attributes that are valid for an ASP.NET Controller or parameters – something like [FromQuery].

Solution

Remove the references to FromQuery. You’ll need to access your POST parameters some other way – let me show you how!

Time needed: 15 minutes.

How to fetch POST values in your Azure Functions (instead of using [FromQuery])

  1. Make sure your Function has “post” as the method

  2. Scratch the [FromQuery] attributes – they don’t and won’t work

    And while you’re at it, remove the parameters altogether. They don’t belong in the method signature as they won’t be bound by the runtime.

  3. (OPTIONAL) Define your POST body as a class

    Okay, this step only applies if you’re POSTing JSON. If you’re posting a simple string or name-value -pairs, just ignore this step.

    This might look somewhat like this:

    public class ArbitraryNameForYour_AzureFunction_PostData
    {
    public string id { get; set; }
    public string shoeSize { get; set; }
    }

  4. Parse your POST body

    Multiple different options here, really. In case you’re parsing JSON, you can use the class from above like this:
    var data = await JsonSerializer.DeserializeAsync<ArbitraryNameForYour_AzureFunction_PostData>(req.Body);

    But if you’re fine with just strings, something like this will do:
    string content= await new StreamReader(req.Body).ReadToEndAsync();


References

mm
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments