This post was most recently updated on February 7th, 2022.
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 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
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])
- Make sure your Function has “post” as the method
- 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.
- (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; }
} - 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
- Merging on GitHub Actions fails with “could not read Username for ‘https://github.com’: No such device or address”? - December 24, 2024
- How to close the Sidebar in Microsoft Edge - December 17, 2024
- How to stop Surface Headphones from autoplaying audio when you place them on the desk? - December 10, 2024