Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure function 2 Dependency injection throwing error

I have a simple trigger that is bound to a Service bus topic. I am trying to inject another service into the trigger but i am receiving an error:

Microsoft.Extensions.DependencyInjection.Abstractions: Unable to resolve service for type 'AzureSearchSBTrigger.SimpleClass' while attempting to activate 'AzureSearchSBTrigger.AzureServiceTrigger'.

If it remove the constructor the trigger works correctly and i can receive messages, etc.

This is an Azure function 2 (.net core) with Function Runtime Version: 2.0.12332.0

This is how it is setup (and not working)

The main function :

public AzureServiceTrigger(SimpleClass apiClient)
{
}

[FunctionName("AzureServiceTrigger")]
public async Task Run([ServiceBusTrigger("","",Connection = "SBConnectionString")]Message myQueueItem, ILogger log)
{
}

StartUp

[assembly: WebJobsStartup(typeof(StartUp))]
namespace AzureSearchSBTrigger
{
    internal class StartUp : IWebJobsStartup
    {
        public void Configure(IWebJobsBuilder builder)
        {
            builder.Services.AddSingleton<SimpleClass>();
        }
    }
}

Simple Class

public class SimpleClass
{
    public void Somethingsimple()
    {
    }
}
like image 647
Neil Gilbert Avatar asked Sep 11 '26 18:09

Neil Gilbert


1 Answers

Fixed by updating the nuget package Microsoft.NET.Sdk.Functions from 1.0.24 -> 1.0.26

like image 179
Neil Gilbert Avatar answered Sep 13 '26 06:09

Neil Gilbert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!