Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dependency Injection not instantiating instances when using function.json rather than attributes #62

Open
burtonr opened this issue Jun 10, 2021 · 0 comments

Comments

@burtonr
Copy link

burtonr commented Jun 10, 2021

I've created an Azure Function in C#, and attempting to utilize the dependency injection feature while also defining the Function parameters with a function.json file rather than the .net attributes so that I can use route parameters in the Function methods as described in Customize the HTTP Endpoint.

When I use the HttpTrigger and FunctionName attributes, the dependency injection works as expected. However, when I remove the attributes, and manually define the function.json file, the runtime throws an exception:

System.Private.CoreLib: Exception while executing function: MyClass.myFunction. Anonymously Hosted DynamicMethods Assembly: Object reference not set to an instance of an object.

To be sure, I've copied the automatically generated function.json file when using the attributes in order to ensure I hadn't miss-typed a value somewhere (removed the configurationSource field and ensured my written function.json file is in the output directory)

Versions and other information:
.csproj file sample:

<PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <AzureFunctionsVersion>v3</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.16" />
    <PackageReference Include="Microsoft.Extensions.Http" Version="3.1.16" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.13" />
  </ItemGroup>

Functions Core Tools:

$ func --version
3.0.3331

I haven't seen a sample of using dependency injection with route parameters, so I may have missed something in the setup. My Startup.cs file matches the example in the Dependency Injection docs.

Happy to provide any other information, or a reproducible example repo if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant