Haim Katz
Haim Katz

Reputation: 461

Dependency injection of EF context in WebJobApp

I'm trying to create my first webjobapp that requires the use of Entity Framework. I am trying to inject my context into the builder of the app, but I'm getting an error:
Cannot bind parameter 'db' to type ApplicationDbContext. Make sure the parameter Type is supported by the binding There are several examples on the web but the versions of the Azure WebJob appear to have changed quickly and I can't find one that works for the version I'm using. These are the package versions I'm using: Webjobs 3.0 EF.NET Core 7.0 Targeting .NET 7.0

Here is my code:

 class Program
{
    static async Task Main()
    {
        var builder = new HostBuilder();
        builder.ConfigureWebJobs(b =>
        {
            b.AddTimers();
            b.AddAzureStorageCoreServices();
        });
        builder.ConfigureLogging((context, b) =>
        {
            b.AddConsole();
        });
        builder.ConfigureAppConfiguration((hostContext, configApp) =>
         {
             configApp.SetBasePath(Directory.GetCurrentDirectory());
             configApp.AddEnvironmentVariables(prefix: "ASPNETCORE_");
             configApp.AddJsonFile($"appsettings.json", true);
             configApp.AddJsonFile($"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json", true);

         });
       
       builder.ConfigureServices(services => { services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer("My full connection string"));
            services.AddTransient<IEmailSender, EmailSender>();
                                              });
        var host = builder.Build();
        using (host)
        //{
            await host.RunAsync();
        }
    }
}

public class Functions

public static void Sendemails([TimerTrigger("0 0 20 * * SUN-THU")] TimerInfo timer,
         ILogger logger,  ApplicationDbContext db)

    {
   
  
        var mylist = db.Users.ToList();
    
    }
}

I tried to configure the services using the following code instead of the above code. When inspecting the builder I can see that the services were added to the builder. But inspecting the host after the call to builder.Build(), The services do not show up in the host container.

var serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection);
     .....

  builder.ConfigureServices((context, sc) =>  sc= serviceCollection);
        var host = builder.Build();
   
        using (host)
        {
            await host.RunAsync();
        } 
}
 .....

    private void ConfigureServices(IServiceCollection services)
{
   
    services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(_configuration.GetConnectionString("DefaultConnection")))
           
        
    }

Project File

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <None Remove="appsettings.Development.json" />
    <None Remove="appsettings.json" />
  </ItemGroup>

  <ItemGroup>
    <Content Include="appsettings.Development.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <ExcludeFromSingleFile>true</ExcludeFromSingleFile>
      <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
    </Content>
    <Content Include="appsettings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <ExcludeFromSingleFile>true</ExcludeFromSingleFile>
      <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
    </Content>
  </ItemGroup>

  <ItemGroup>
    <PackageReference 
Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="7.0.1" />
    <PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.33" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="4.0.1" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.OpenApi.Configuration.AppSettings" Version="1.5.0" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.SendGrid" Version="3.0.2" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="5.0.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.1" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
    <PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
    <PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="7.0.0" />
    <PackageReference Include="SendGrid" Version="9.28.1" />
  </ItemGroup>

</Project>

Upvotes: 2

Views: 480

Answers (2)

sedaghat
sedaghat

Reputation: 72

I do the steps from the beginning.


1-

public class ApplicationDbContext: IdentityDbContext<User, Role, Guid>
{
    public ApplicationDbContext(DbContextOptions options)
       : base(options)
    {
       
    }
 
     
    }

2-add in appsettings.json


"ConnectionStrings": {
  "SqlServer": "Data Source=;Initial Catalog=SpaceManagment;User ID=sa;Password=654321;MultipleActiveResultSets=true;TrustServerCertificate=True;Connection Timeout = 60"
},

3-


add in program.cs


 builder.Services.AddDbContext<ApplicationDbContext>(options =>
        {
            options
                .UseSqlServer(settings.SqlServer);
            //Tips
            // .ConfigureWarnings(warning => warning.Throw(RelationalEventId.QueryClientEvaluationWarning));
        });

Upvotes: 0

Harshitha
Harshitha

Reputation: 7297

Initially Even I got the same error.

enter image description here

Done few changes in Program.cs .

My Program.cs:

using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

class Program
{
    static async Task Main()
    {
        var builder = new HostBuilder()
            .ConfigureWebJobs(b =>
            {
                b.AddTimers();
                b.AddAzureStorageCoreServices();
            })
            .ConfigureLogging((context, b) =>
            {
                b.AddConsole();
            })
             .ConfigureAppConfiguration((hostContext, configApp) =>
             {
                 configApp.SetBasePath(Directory.GetCurrentDirectory());
                 configApp.AddEnvironmentVariables(prefix: "ASPNETCORE_");
                 configApp.AddJsonFile($"appsettings.json", true);
                 configApp.AddJsonFile($"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json", true);
             })

        .ConfigureServices(services =>
        {
            services.AddDbContext<ApplicationDbContext>(
        options => options.UseSqlServer("MyDefaultConnection"));
            services.AddTransient<ApplicationDbContext, ApplicationDbContext>();
        });


        var host = builder.Build();
        using (host)
        {
            await host.RunAsync();
        }
    }
}

If you are running locally, cross check the local.settings.json file once.

My local.settings.json :

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet"
  }
}

My appsettings.json:

{
  "ConnectionStrings": {
    "AzureWebJobsStorage": "Storage Account Connection String"
  }
}

enter image description here

Also have a look at SO Thread once.

References taken from MSDoc.

Upvotes: 1

Related Questions