gpt4 book ai didi

c# - Azure Function v2 中的 Microsoft.Azure.WebJobs.Host : Cannot bind parameter 'myContext' to type DataContext. 错误

转载 作者:行者123 更新时间:2023-12-03 01:39:56 24 4
gpt4 key购买 nike

要求:创建可以注入(inject)Entity Framework上下文的Azure函数 使用依赖注入(inject)来 Run 方法。

这是我的Startup

   [assembly: WebJobsStartup(typeof(Startup))]
namespace MessagesToSqlDbFuncApp
{
internal class Startup : IWebJobsStartup
{
public void Configure(IWebJobsBuilder builder) =>
builder.AddDependencyInjection<ServiceProviderBuilder>();
}
}

这是ServiceProviderBuilder

public class ServiceProviderBuilder : IServiceProviderBuilder
{
public IServiceProvider Build()
{
IConfigurationRoot config = new ConfigurationBuilder()
.SetBasePath(Environment.CurrentDirectory)
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();

var connectionString = config.GetConnectionString("SqlConnectionString");

var services = new ServiceCollection();

services.AddDbContext<DataContext>(options => options.UseSqlServer(connectionString));

return services.BuildServiceProvider(true);
}
}

这是我的功能

   [FunctionName("MessagesToSqlDbFuncApp")]
public static async Task Run([BlobTrigger("messagecontainer/{name}", Connection = "AzureWebJobsStorage")]
Stream myBlob,
string name,
ILogger log,
[Inject] DataContext myContext)
{

}

这是运行函数时抛出的错误

[2/20/2019 4:25:10 AM] Error indexing method 'MessagesToSqlDbFuncApp' [2/20/2019 4:25:10 AM] Microsoft.Azure.WebJobs.Host: Error indexing method 'BlobQCMessagesToSqlDbFuncApp'. Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'myContext' to type DataContext. Make sure the parameter Type is supported by the binding. 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.).

这是 nuget 软件包和版本

  • Azure 函数版本:2
  • Visual Studio:2017
  • Microsoft.EntityFrameworkCore:2.1.4
  • Microsoft.EntityFrameworkCore.Design:2.1.4
  • Microsoft.EntityFrameworkCore.SqlServer:2.1.4
  • Microsoft.Extensions.DependencyInjection:2.2.0
  • Microsoft.NET.Sdk.Functions:1.0.24
  • Microsoft.NETCore.App:2.1.0

重要提示:调试器未命中 Startup 类!如何初始化 startup 类?

最佳答案

假设您使用包 Willezone.Azure.WebJobs.Extensions.DependencyInjection ,右键单击您的函数项目,Edit <functionProject>.csproj ,并将 TargetFramework 从 netcoreapp2.1 更改为 netstandard2.0。

<TargetFramework>netstandard2.0</TargetFramework>

之所以存在不一致,是因为非官方包没有跟上Function SDK的变化,official guidance正在进行中。

Most of the core pieces to add this support are done. We'll be in a better position to provide an ETA on that once the last SDK items are completed.

关于c# - Azure Function v2 中的 Microsoft.Azure.WebJobs.Host : Cannot bind parameter 'myContext' to type DataContext. 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54778925/

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