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:
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<AzureFunctionsVersion>v3</AzureFunctionsVersion>
<OutputType>Exe</OutputType>
</PropertyGroup>
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 (eg Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (eg
builder.AddAzureStorage()
,builder.AddServiceBus()
,builder.AddTimers()
, etc.) .
I've tried next things:
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
.
Making everything static inside MyAzureFunction
but don't work because the static constructors can't contain parameters.
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.
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();
}
}
}
You need to add package - 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.