Azure-Functions icon indicating copy to clipboard operation
Azure-Functions copied to clipboard

Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'myContext' to type DataContext. error in Azure Function v2

Open deepakkumpala opened this issue 7 years ago • 18 comments

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

Here is my Startup class

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

Here is ServiceProviderBuilder class

 `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);
    }
  }`

This is my function

  `  [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] 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.).

Here is the nuget packages and versions

  • Azure Function Version : 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

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

deepakkumpala avatar Feb 20 '19 04:02 deepakkumpala

This is using an external binding/extension, so can't provide much information about its behavior. About the startup question, have you tried updating to the latest version of the extension metadata generator (https://www.nuget.org/packages/Microsoft.Azure.WebJobs.Script.ExtensionsMetadataGenerator/)? There were fixes in that version to address that issue.

fabiocav avatar Feb 20 '19 23:02 fabiocav

Any update here? I am having the same issue

SpicySyntax avatar Mar 21 '19 20:03 SpicySyntax

It is to do with right version of nuget packages. please reverify the packages.

deepakkumpala avatar Mar 27 '19 17:03 deepakkumpala

I am having the same problem. Is there any update on this?

p3rcy avatar Jul 04 '19 15:07 p3rcy

Same Issue here, Any update?

Sandeep321 avatar Feb 12 '20 13:02 Sandeep321

It started working after adding { "version": "2.0" } in host.json file. However, not sure how it was working earlier being host.json as empty.

Sandeep321 avatar Feb 13 '20 10:02 Sandeep321

+1 Getting the same issue in Azure function v2 netcoreapp2.2

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>
    <AzureFunctionsVersion>v2</AzureFunctionsVersion>
    <RootNamespace>Microsoft.Crm.Assistant</RootNamespace>
    <AssemblyName>ActionCardFlowService</AssemblyName>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <DebugType>full</DebugType>
    <DebugSymbols>true</DebugSymbols>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="AutofacOnFunctions" Version="1.0.14" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.24" />
    <PackageReference Include="System.Net.Http" Version="4.3.4" />
  </ItemGroup>
  <ItemGroup>
    <Reference Include="Microsoft.CSharp" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

sandeepnmenon avatar Feb 14 '20 14:02 sandeepnmenon

If its in your local system, please try my solution above. If its in portal, please make sure that you have azure function set correctly with FUNCTIONS_EXTENSION_VERSION. Please check https://docs.microsoft.com/en-us/azure/azure-functions/set-runtime-version for more.

Sandeep321 avatar Feb 15 '20 08:02 Sandeep321

I have some problem, it works!

It started working after adding { "version": "2.0" } in host.json file. However, not sure how it was working earlier being host.json as empty.

haoolii avatar Feb 19 '20 02:02 haoolii

@unnhao is right There was an issue were giving an empty host.json would add some default extension bundles in the azure function, causing it to fail loading. On removing the same, it seems to work. That is why, providing a non-empty host.json resolved the issue

sandeepnmenon avatar Feb 20 '20 07:02 sandeepnmenon

Indeed. Can you please confirm that the mitigation above addressed the problem? The latest version also has a fix to address this scenario.

fabiocav avatar Feb 20 '20 22:02 fabiocav

@fabiocav Yes, by re-deploying the function app with providing { "version": "2.0" } resolved the issue. Can you please elaborate on "The latest version also has a fix to address this scenario." Is it the latest version of the extension bundle, the function app or something else?

sandeepnmenon avatar Feb 21 '20 03:02 sandeepnmenon

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.

ghost avatar Feb 25 '20 06:02 ghost

Still happening to me.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>
    <AzureFunctionsVersion>v2</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.0.0" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.DurableTask" Version="2.1.1" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.EventHubs" Version="4.1.1" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.31" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

euvin3582 avatar Feb 27 '20 03:02 euvin3582

Still happening to me.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>
    <AzureFunctionsVersion>v2</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.0.0" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.DurableTask" Version="2.1.1" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.EventHubs" Version="4.1.1" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.31" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

@euvin3582 What does your host.json look like?

sandeepnmenon avatar Feb 29 '20 06:02 sandeepnmenon

I was able to fix this issue by installing the NuGet package Microsoft.Azure.WebJobs.Script.ExtensionsMetadataGenerator

sudheeshix avatar Mar 04 '20 12:03 sudheeshix

Seems related to this issue

sudheeshix avatar Mar 04 '20 12:03 sudheeshix

Microsoft.Azure.WebJobs.Script.ExtensionsMetadataGenerator does not fix this error for me.

This was all working a week ago, then we updated Xcode 11.4, OS X 10.15.4 and the netcoreapp from 2.* to 3.1 Several pieces from this post have been helpful but I'm still stuck. When running, I get: [3/28/2020 2:32:15 PM] Microsoft.Azure.WebJobs.Host: Error indexing method '...'. Microsoft.Azure.WebJobs.Host: Cannot bind parameter '...' to type IAsyncCollector`1. 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.).

When I add: Microsoft.Azure.WebJobs.Script.ExtensionsMetadataGenerator from NuGet, I then See build errors: /Users/seanreynolds/.nuget/packages/microsoft.net.sdk.functions/3.0.5/build/Microsoft.NET.Sdk.Functions.Build.targets(41,5): error : Mono.Cecil.AssemblyResolutionException: Failed to resolve assembly: 'Microsoft.Azure.WebJobs.Extensions.Http, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' /Users/seanreynolds/.nuget/packages/microsoft.net.sdk.functions/3.0.5/build/Microsoft.NET.Sdk.Functions.Build.targets(41,5): error : at Mono.Cecil.BaseAssemblyResolver.Resolve(AssemblyNameReference name, ReaderParameters parameters) /Users/seanreynolds/.nuget/packages/microsoft.net.sdk.functions/3.0.5/build/Microsoft.NET.Sdk.Functions.Build.targets(41,5): error : at Mono.Cecil.BaseAssemblyResolver.Resolve(AssemblyNameReference name) /Users/seanreynolds/.nuget/packages/microsoft.net.sdk.functions/3.0.5/build/Microsoft.NET.Sdk.Functions.Build.targets(41,5): error : at Mono.Cecil.DefaultAssemblyResolver.Resolve(AssemblyNameReference name) /Users/seanreynolds/.nuget/packages/microsoft.net.sdk.functions/3.0.5/build/Microsoft.NET.Sdk.Functions.Build.targets(41,5): error : at Mono.Cecil.MetadataResolver.Resolve(TypeReference type) /Users/seanreynolds/.nuget/packages/microsoft.net.sdk.functions/3.0.5/build/Microsoft.NET.Sdk.Functions.Build.targets(41,5): error : at Mono.Cecil.ModuleDefinition.Resolve(TypeReference type) /Users/seanreynolds/.nuget/packages/microsoft.net.sdk.functions/3.0.5/build/Microsoft.NET.Sdk.Functions.Build.targets(41,5): error : at Mono.Cecil.TypeReference.Resolve() /Users/seanreynolds/.nuget/packages/microsoft.net.sdk.functions/3.0.5/build/Microsoft.NET.Sdk.Functions.Build.targets(41,5): error : at MakeFunctionJson.AttributeExtensions.IsWebJobsAttribute(CustomAttribute attribute) /Users/seanreynolds/.nuget/packages/microsoft.net.sdk.functions/3.0.5/build/Microsoft.NET.Sdk.Functions.Build.targets(41,5): error : at MakeFunctionJson.ParameterInfoExtensions.<>c.b__0_0(CustomAttribute a) /Users/seanreynolds/.nuget/packages/microsoft.net.sdk.functions/3.0.5/build/Microsoft.NET.Sdk.Functions.Build.targets(41,5): error : at System.Linq.Enumerable.Any[TSource](IEnumerable1 source, Func2 predicate) /Users/seanreynolds/.nuget/packages/microsoft.net.sdk.functions/3.0.5/build/Microsoft.NET.Sdk.Functions.Build.targets(41,5): error : at MakeFunctionJson.ParameterInfoExtensions.IsWebJobSdkTriggerParameter(ParameterDefinition parameterInfo) /Users/seanreynolds/.nuget/packages/microsoft.net.sdk.functions/3.0.5/build/Microsoft.NET.Sdk.Functions.Build.targets(41,5): error : at MakeFunctionJson.MethodInfoExtensions.<>c.b__4_0(ParameterDefinition p) /Users/seanreynolds/.nuget/packages/microsoft.net.sdk.functions/3.0.5/build/Microsoft.NET.Sdk.Functions.Build.targets(41,5): error : at System.Linq.Enumerable.Any[TSource](IEnumerable1 source, Func2 predicate) /Users/seanreynolds/.nuget/packages/microsoft.net.sdk.functions/3.0.5/build/Microsoft.NET.Sdk.Functions.Build.targets(41,5): error : at MakeFunctionJson.MethodInfoExtensions.HasTriggerAttribute(MethodDefinition method) /Users/seanreynolds/.nuget/packages/microsoft.net.sdk.functions/3.0.5/build/Microsoft.NET.Sdk.Functions.Build.targets(41,5): error : at MakeFunctionJson.MethodInfoExtensions.HasValidWebJobSdkTriggerAttribute(MethodDefinition method) /Users/seanreynolds/.nuget/packages/microsoft.net.sdk.functions/3.0.5/build/Microsoft.NET.Sdk.Functions.Build.targets(41,5): error : at MakeFunctionJson.FunctionJsonConverter.GenerateFunctions(IEnumerable1 types)+MoveNext() /Users/seanreynolds/.nuget/packages/microsoft.net.sdk.functions/3.0.5/build/Microsoft.NET.Sdk.Functions.Build.targets(41,5): error : at System.Collections.Generic.List1..ctor(IEnumerable1 collection) /Users/seanreynolds/.nuget/packages/microsoft.net.sdk.functions/3.0.5/build/Microsoft.NET.Sdk.Functions.Build.targets(41,5): error : at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source) /Users/seanreynolds/.nuget/packages/microsoft.net.sdk.functions/3.0.5/build/Microsoft.NET.Sdk.Functions.Build.targets(41,5): error : at MakeFunctionJson.FunctionJsonConverter.TryGenerateFunctionJsons() /Users/seanreynolds/.nuget/packages/microsoft.net.sdk.functions/3.0.5/build/Microsoft.NET.Sdk.Functions.Build.targets(41,5): error : at MakeFunctionJson.FunctionJsonConverter.TryRun() /Users/seanreynolds/.nuget/packages/microsoft.net.sdk.functions/3.0.5/build/Microsoft.NET.Sdk.Functions.Build.targets(41,5): error : Error generating functions metadata /Users/seanreynolds/.nuget/packages/microsoft.net.sdk.functions/3.0.5/build/Microsoft.NET.Sdk.Functions.Build.targets(41,5): error : /Users/seanreynolds/.nuget/packages/microsoft.net.sdk.functions/3.0.5/build/Microsoft.NET.Sdk.Functions.Build.targets(41,5): error : Metadata generation failed.

I can still publish directly to Azure, it's just that running locally is hosed. Thoughts?

seanreynoldscs avatar Mar 28 '20 14:03 seanreynoldscs