gpt4 book ai didi

c# - WebJobApp 中 EF 上下文的依赖注入(inject)

转载 作者:行者123 更新时间:2023-12-02 06:53:42 25 4
gpt4 key购买 nike

我正在尝试创建我的第一个需要使用 Entity Framework 的网络作业应用程序。我试图将我的上下文注入(inject)应用程序的构建器,但出现错误:
无法将参数“db”绑定(bind)到 ApplicationDbContext 类型。确保绑定(bind)支持参数类型网上有几个示例,但 Azure WebJob 的版本似乎变化很快,我找不到适合我正在使用的版本的示例。这些是我正在使用的软件包版本:网络招聘3.0EF.NET 核心 7.0面向 .NET 7.0

这是我的代码:

 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();

}
}

我尝试使用以下代码而不是上面的代码来配置服务。检查构建器时,我可以看到服务已添加到构建器中。但是在调用 builder.Build() 后检查主机,服务不会显示在主机容器中。

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 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>

最佳答案

最初即使我也遇到了同样的错误。

enter image description here

Program.cs 中进行了一些更改。

我的程序.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();
}
}
}

如果您在本地运行,请交叉检查 local.settings.json 文件一次。

我的local.settings.json:

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

我的appsettings.json:

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

enter image description here

另请查看 SO Thread一次。

引用资料取自MSDoc .

关于c# - WebJobApp 中 EF 上下文的依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75093005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com