question

adrianamalea-4040 avatar image
0 Votes"
adrianamalea-4040 asked PramodValavala-MSFT commented

Azure Functions v3 dependency injection issue

Hello,

When trying to use DI with [FromServices], it fails with the following build error:

Error 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 MakeFunctionJson.FunctionJsonConverter.TryGenerateFunctionJsons()
at MakeFunctionJson.FunctionJsonConverter.TryRun()
Error generating functions metadata

I have used:

 Microsoft.NET.Sdk.Functions" Version="3.0.13" and "Microsoft.Azure.Functions.Extensions" Version="1.1.0".

Looks like a few people have bumped into that one: https://stackoverflow.com/questions/64185565/failed-to-resolve-assembly-microsoft-aspnetcore-mvc-core-version-3-1-0-0-when, but there is no official workaround for this.


I haven't seen any response/solution from Microsoft. Is there an incompatibility between libs, what is the combination that I should use?
If this method injection doesn't work, can you please tell me what other alternatives do I have?

Thank you!



azure-functions
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

PramodValavala-MSFT avatar image
0 Votes"
PramodValavala-MSFT answered PramodValavala-MSFT commented

@adrianamalea-4040 The attribute you are trying to use for ASP.NET Core MVC Controllers. They are not applicable for Azure Functions and removing it should help like mentioned in the answer of the SO post you shared.

While Azure Functions does inject certain dependencies (like ILogger) into functions, custom services are only injected into class constructors. The Dependency Injection for .NET Functions doc covers the supported scenarios.


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

So, to be sure that I understood it correctly: to inject custom services I can only use class constructors, method injection is not supported.
For netcore 2.2 in one of my old projects, I have used the lib Willezone.Azure.WebJobs.Extensions.DependencyInjection to do the method injection.
I was wondering if there exists a similar lib for netcore 3.1.
Thank you for you support!

0 Votes 0 ·

@adrianamalea-4040 This library is leveraging a binding extension to perform dependency injection like how the input bindings work in the official extensions. I haven't tested it myself but I suppose it could still work for newer versions too but can't be sure since it isn't an official extension.

0 Votes 0 ·