Microsoft.Azure.WebJobs.Host:无法绑定参数"myContext"以键入DataContext. Azure Function v2中的错误 [英] Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'myContext' to type DataContext. error in Azure Function v2

查看:182
本文介绍了Microsoft.Azure.WebJobs.Host:无法绑定参数"myContext"以键入DataContext. Azure Function v2中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Requirement: Create Azure Function which can inject Entity Framework context to Run method using dependency injection.

这是我的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)
    {

    }

这是运行功能时抛出的错误

Here is the error which thrown while running the funciton

[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,Timer等),请确保已在启动代码中调用了扩展的注册方法(例如builder.AddAzureStorage(),builder.AddServiceBus( ),builder.AddTimers()等).

[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软件包和版本

Here is the nuget packages and versions

  • 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类?

Important Note: Debugger is not hitting the Startup class!. How do I initialize the startup class?

推荐答案

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

Assume you work with the package Willezone.Azure.WebJobs.Extensions.DependencyInjection, right click on your function project, Edit <functionProject>.csproj, and change TargetFramework from netcoreapp2.1 to netstandard2.0.

<TargetFramework>netstandard2.0</TargetFramework>

之所以存在不一致之处,是因为非官方软件包无法赶上Function SDK中的更改,

The inconsistency exists because the unofficial package doesn't catch up the changes in Function SDK, the official guidance is underway.

添加此支持的大多数核心部分都已完成.一旦完成最后一个SDK项,我们将可以更好地提供ETA.

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.

这篇关于Microsoft.Azure.WebJobs.Host:无法绑定参数"myContext"以键入DataContext. Azure Function v2中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆