I have an Azure Function with a Timer Trigger, developed with C# and .NET6 using Visual Studio 2022 Pro v17.4.3.
Current goal is to get a DbContext working in this function so I can query data from the underlying postgreSQL database.
A future goal is to use dependency injection and get the DbContext instantiation out of the function code.
using System;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using MyProject.Data;
using MyProject.Repositories;
namespace QP.Overwatch.FunctionApp
{
public class MyFunction
{
[FunctionName("MyFunction")]
public async Task Run([TimerTrigger("0 * * * * *")]TimerInfo myTimer, ILogger log)
{
log.LogInformation($"MyFunction - executed at: {DateTime.Now}");
var optionsBuilder = new DbContextOptionsBuilder<MyContext>().UseNpgsql(System.Environment.GetEnvironmentVariable("MyConnection"));
var dbContext = new MyContext(optionsBuilder.Options);
var myRepository = new MyRepository(dbContext);
var data = await myRepository.GetAllAsync();
}
}
}
Before adding any of the code to setup DbContext the function ran just fine. All it did at that point was log the message about execution with a time stamp. But after adding the reference to Microsoft.EntityFrameworkCore, all of a sudden it can’t recognize the function at all. I get this error:
[2023-09-18T15:31:27.052Z] 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.).
[2023-09-18T15:31:27.092Z] The 'Function1' function is in error: Could not load file or assembly 'Microsoft.Extensions.Logging.Abstractions, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.
I tried manually adding a package reference for Microsoft.Extensions.Logging.Abstractions v7.0.0.0 but the problem persists.
Why does a reference to EF Core suddenly cause the whole function to be not found?
2
Answers
This thread helped me find the issue.
In my case, the timer trigger function has a dependency upon another project that references EF Core and contains my DbContext class. This project was using EF Core 7.0.2 which in turn requires Microsoft.Extensions.Logging.Abstractions, Version=7.0.0.0
I had to downgrade to Microsoft.Extensions.Logging.Abstractions, Version=6.0.3 for everything to work with the Azure function. This required downgrading EF Core to 6.0.22. After that everything started working as usual.
I used Azure SQL Database for database.
I was also getting same error when I was using your code.
To resolve this issue, I have used .NET 6 isolated runtime and lower Version
6.0.22
forMicrosoft.EntityFrameworkCore
.Some changes I had to make based on the isolated runtime. here are all the files.
MyContext.cs
PlaceholderEntity.cs
MyRepository.cs
IMyRepository.cs
Function1.cs
Program.cs
MyProject.csproj
Output: