2

I've created an Azure Function version 3 using .NET 5 and dependency injection trough the constructor of the class. See dummy code below:

public class MyAzureFunction
{
    private readonly IMyRepository _myRepository;

    public MyAzureFunction(IMyRepository myRepository) 
    {
        _myRepository = myRepository;
    }

    [Function("MyAzureFunction")]
    public async Task Run([TimerTrigger("0 */15 * * * *")] TimerInfo myTimer, FunctionContext context)
    {
        ILogger logger = context.GetLogger("MyAzureFunction");
        logger.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

        List<object> result = await _myRepository.GetAllAsync();

        // Keep going...
    }
}

Inside the Startup class the scopes are added.

[assembly: FunctionsStartup(typeof(MyNamespace.Functions.Startup))]
namespace MyNamespace.Functions
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services
                .AddScoped<IMyRepository, MyRepository>();
        }
    }
}

The Program file looks as follow:

public class Program
{
    public static void Main()
    {
        IHost host = new HostBuilder()
            .ConfigureFunctionsWorkerDefaults()
            .Build();

        host.Run();
    }
}

Inside the .csproj file stands this line of code:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>net5.0</TargetFramework>
        <AzureFunctionsVersion>v3</AzureFunctionsVersion>
        <OutputType>Exe</OutputType>
    </PropertyGroup>
    <ItemGroup>
        <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
        <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Timer" Version="4.1.0" />
        <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.3.0" />
        <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.6.0" />
    </ItemGroup>
    <!-- Skiped some lines -->
</Project>

The problem is when I want to run the Azure Function. I've got this warning:

No job functions found. Try making your job classes and methods public. If you're using binding extensions (e.g. Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. builder.AddAzureStorage(), builder.AddServiceBus(), builder.AddTimers(), etc.).

I've tried next things:

  1. I've added builder.AddTimers() inside the Startup class but IFunctionsHostBuilder contains no definition for it. Even if I add Microsoft.Azure.Functions.Worker.Extensions.Timer.

  2. Making everything static inside MyAzureFunction but don't work because the static constructors can't contain parameters.

  3. Also builder.Services.AddTimers() (like in the documentation) is not defined.

My question is now how could I use dependency injection using Azure Functions and .NET 5 using the constructor.

H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
  • 1
    *I've added builder.AddTimers() inside the Startup class but IFunctionsHostBuilder contains no definition for it.* Then you're missing an assembly reference or NuGet package. Did you try to locate and install the appropriate package? – Daniel Mann Nov 03 '21 at 15:15
  • @DanielMann: Well, I've added [`Microsoft.Azure.Functions.Worker.Extensions.Timer`](https://www.nuget.org/packages/Microsoft.Azure.Functions.Worker.Extensions.Timer/4.1.0?_src=template) but same problem. – H. Pauwelyn Nov 03 '21 at 15:19
  • 1
    @H.Pauwelyn You are using dotnet-isolated version and dependency work differently over there and I have provided my answer. It must work. – dotnetstep Nov 03 '21 at 15:57

2 Answers2

3

In Program.cs do following things. ( Make sure you add refence for namespace)

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace FunctionApp2
{
    public class Program
    {
        public static void Main()
        {
            var host = new HostBuilder()
                .ConfigureServices(services =>
                {
                    services.AddScoped<IMyRepository, MyRepository>();
                })
                .ConfigureFunctionsWorkerDefaults()
                .Build();

            host.Run();
        }
    }
}
dotnetstep
  • 17,065
  • 5
  • 54
  • 72
1

You need to add packages:

  • Microsoft.Azure.Functions.Extensions

  • Microsoft.Azure.Functions.Worker

  • Microsoft.Azure.Functions.Worker.Sdk

Then make sure the previous packages for .NET Core 3 are removed.

H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
  • They were already installed in the project but didn't solve the problem. I've updated my question. – H. Pauwelyn Nov 04 '21 at 07:54
  • Regarding the 'No job functions found. Try making your job classes and methods public' issue, maybe can refer to https://stackoverflow.com/questions/47682760/no-job-functions-found-try-making-your-job-classes-and-methods-public. I think last time encountered this issue too locally and updated the azure-functions-core-tools. – Levanan Gan Nov 04 '21 at 08:39
  • Also just to double check the ` PreserveNewest PreserveNewest Never ` present in your csproj file right – Levanan Gan Nov 04 '21 at 08:42