【问题标题】:Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'myContext' to type DataContext. error in Azure Function v2Microsoft.Azure.WebJobs.Host:无法将参数“myContext”绑定到类型 DataContext。 Azure 函数 v2 中的错误
【发布时间】:2019-07-13 16:55:25
【问题描述】:

要求:创建可以注入 Entity Framework 上下文的 Azure 函数 使用依赖注入到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] 错误索引方法“MessagesToSqlDbFuncApp” [2/20/2019 4:25:10 AM] Microsoft.Azure.WebJobs.Host:错误索引方法“BlobQCMessagesToSqlDbFuncApp”。 Microsoft.Azure.WebJobs.Host:无法将参数“myContext”绑定到类型 DataContext。确保绑定支持参数类型。如果您正在使用绑定扩展(例如 Azure 存储、ServiceBus、计时器等),请确保您已在启动代码中调用了扩展的注册方法(例如 builder.AddAzureStorage()、builder.AddServiceBus( )、builder.AddTimers() 等)。

这里是 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 类?

【问题讨论】:

  • 我已经打开了一个关于相同(我猜是相同)问题的问题github.com/Azure/azure-webjobs-sdk/issues/…
  • 有任何进展,您能接受一个有用的解决方案还是简单地发布您的解决方案供其他人参考?
  • 标记了答案

标签: c# azure entity-framework-core azure-functions autofac


【解决方案1】:

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

<TargetFramework>netstandard2.0</TargetFramework>

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

添加此支持的大部分核心部分都已完成。一旦最后一个 SDK 项目完成,我们将能够更好地提供 ETA。

【讨论】:

    【解决方案2】:

    启动类和 azure 函数存在问题。见Azure function Publish not creating Startup class entry in extensions.json

    其中一些问题是产品在 片刻。我们正在为面向客户的 DI 提供一流的支持 Azure Functions 中的功能,它将附带一整套 文档,包括您可以依赖的服务和官方 指导。

    一种解决方案是使用IExtensionConfigProvider(参见Azure Function run code on startup

    就我个人而言,我刚刚使用静态构造函数进行初始化,并使用 azure 函数作为不起眼的对象,如下所述:Integrating Simple Injector in Azure Functions

    编辑 25/02/2019

    Microsoft.Azure.WebJobs.Script.ExtensionsMetadataGenerator 的 1.0.2 版本修复了 extensions.json 中的绑定问题。 Microsoft.NET.Sdk.Functions 应该很快就会包含最新版本的 ExtensionsMetadataGenerator。

    【讨论】:

      猜你喜欢
      • 2020-04-05
      • 1970-01-01
      • 1970-01-01
      • 2021-12-30
      • 1970-01-01
      • 1970-01-01
      • 2018-09-15
      • 2021-08-16
      • 2015-02-22
      相关资源
      最近更新 更多