0

I'm trying to build an Azure Function App with Entity Framework Core.

The following exception is thrown from WebJobsBuilderExtensions.cs at run time.

System.IO.FileNotFoundException: '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 have tried adding the Microsoft.Extensions.Logging.Abstractions package to the project, but I get the same exception. I have worked with Functions Apps before, but I am new to Entity Framework. I just can't get my head around the way dependencies are resolved. Is it possible there is some conflict between versions? I am using VS 2022(Version 17.4.1), if that's relevant.

I have reviewed posts to similar issues, but all were targeting earlier versions of .NET and the solutions didn't apply or did not work for me.

.csproj file:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="7.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.0" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.3" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

My DbContext class:

using Microsoft.EntityFrameworkCore;

namespace TestAPI
{
    public class Test
    {
        public string item1 { get; set; }
        public string item2 { get; set; }
    }

    public class MyDbContext : DbContext
    {
        public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
        { }

        public DbSet<Test> Data { get; set; }
    }
}

My Startup class:

using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;

[assembly: FunctionsStartup(typeof(TestAPI.Startup))]
namespace TestAPI
{
    class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            string ConnectionString = Environment.GetEnvironmentVariable("MyDBConnection");
            builder.Services.AddDbContext<MyDbContext>(
                options => options.UseSqlServer(ConnectionString)
                );
        }
    }
}

And the function:

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using System.IO;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace TestAPI
{
    public class HttpTrigger
    {
        private readonly MyDbContext _context;
        public HttpTrigger(MyDbContext context)
        {
            _context = context;
        }

        [FunctionName("Test1")]
        public async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string name = req.Query["name"];

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            name = name ?? data?.name;

            string responseMessage = string.IsNullOrEmpty(name)
                ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
                : $"Hello, {name}. This HTTP triggered function executed successfully.";

            return new OkObjectResult("ok");
        }
    }
}

Sorry for the code dump :/

Nick Oban
  • 13
  • 3
  • Does this [reference](https://stackoverflow.com/questions/74470489/system-io-filenotfoundexception-could-not-load-file-or-assembly-microsoft-exte/74473381#74473381) helps? – RithwikBojja Nov 18 '22 at 01:58

0 Answers0