graphql-platform: Azure Function DI Null Exception
Describe the bug Null Exception during Dependency Injection within Azure Functions. Working in 10.0.1 only.
To Reproduce Using Repository located https://github.com/OneCyrus/GraphQL-AzureFunctions-HotChocolate
Steps to reproduce the behavior:
- Download the above sample repository
- Query data and get back results - Success eg
{
droid(id:2000) {
name
}
}
- Update packages to any version after 10.0.1 (both 10.2.0 and 11.0.0-preview.70 have same issue)
- Run Query in step 2 - results in a NULL ref exception
{
"data": {},
"extensions": {},
"errors": [
{
"message": "Unexpected Execution Error",
"code": null,
"path": null,
"locations": [],
"exception": {
"ClassName": "System.NullReferenceException",
"Message": "Object reference not set to an instance of an object.",
"Data": null,
"InnerException": null,
"HelpURL": null,
"StackTraceString": " at lambda_method(Closure , IResolverContext )\r\n at DryIoc.Factory.<>c__DisplayClass26_0.<ApplyReuse>b__2() in C:\\azure-webjobs-sdk-script\\src\\WebJobs.Script.WebHost\\DependencyInjection\\DryIoc\\Container.cs:line 6596\r\n at DryIoc.Scope.TryGetOrAdd(ImMap`1 items, Int32 id, CreateScopedValue createValue, Int32 disposalOrder) in C:\\azure-webjobs-sdk-script\\src\\WebJobs.Script.WebHost\\DependencyInjection\\DryIoc\\Container.cs:line 7840\r\n at DryIoc.Scope.GetOrAdd(Int32 id, CreateScopedValue createValue, Int32 disposalOrder) in C:\\azure-webjobs-sdk-script\\src\\WebJobs.Script.WebHost\\DependencyInjection\\DryIoc\\Container.cs:line 7825\r\n at DryIoc.Factory.ApplyReuse(Expression serviceExpr, Request request) in C:\\azure-webjobs-sdk-script\\src\\WebJobs.Script.WebHost\\DependencyInjection\\DryIoc\\Container.cs:line 6595\r\n at DryIoc.Factory.GetExpressionOrDefault(Request request) in C:\\azure-webjobs-sdk-script\\src\\WebJobs.Script.WebHost\\DependencyInjection\\DryIoc\\Container.cs:line 6555\r\n at DryIoc.Factory.GetDelegateOrDefault(Request request) in C:\\azure-webjobs-sdk-script\\src\\WebJobs.Script.WebHost\\DependencyInjection\\DryIoc\\Container.cs:line 6625\r\n at DryIoc.Container.ResolveAndCacheDefaultFactoryDelegate(Type serviceType, IfUnresolved ifUnresolved) in C:\\azure-webjobs-sdk-script\\src\\WebJobs.Script.WebHost\\DependencyInjection\\DryIoc\\Container.cs:line 210\r\n at DryIoc.Container.DryIoc.IResolver.Resolve(Type serviceType, IfUnresolved ifUnresolved) in C:\\azure-webjobs-sdk-script\\src\\WebJobs.Script.WebHost\\DependencyInjection\\DryIoc\\Container.cs:line 195\r\n at Microsoft.Azure.WebJobs.Script.WebHost.DependencyInjection.JobHostServiceProvider.GetService(Type serviceType, IfUnresolved ifUnresolved) in C:\\azure-webjobs-sdk-script\\src\\WebJobs.Script.WebHost\\DependencyInjection\\JobHostServiceProvider.cs:line 101\r\n at Microsoft.Azure.WebJobs.Script.WebHost.DependencyInjection.JobHostServiceProvider.GetService(Type serviceType) in C:\\azure-webjobs-sdk-script\\src\\WebJobs.Script.WebHost\\DependencyInjection\\JobHostServiceProvider.cs:line 79\r\n at lambda_method(Closure , IServiceProvider , QueryDelegate )\r\n at HotChocolate.Execution.ClassMiddlewareFactory.<>c__DisplayClass0_0`1.<Create>b__1(IServiceProvider s, QueryDelegate n)\r\n at HotChocolate.Execution.ClassMiddlewareFactory.<>c__DisplayClass2_0`1.<CreateDelegate>b__0(IQueryContext context)\r\n at HotChocolate.Execution.ExceptionMiddleware.InvokeAsync(IQueryContext context)",
"RemoteStackTraceString": null,
"RemoteStackIndex": 0,
"ExceptionMethod": null,
"HResult": -2147467261,
"Source": "Anonymously Hosted DynamicMethods Assembly",
"WatsonBuckets": null
},
"extensions": {
"message": "Object reference not set to an instance of an object.",
"stackTrace": " at lambda_method(Closure , IResolverContext )\r\n at DryIoc.Factory.<>c__DisplayClass26_0.<ApplyReuse>b__2() in C:\\azure-webjobs-sdk-script\\src\\WebJobs.Script.WebHost\\DependencyInjection\\DryIoc\\Container.cs:line 6596\r\n at DryIoc.Scope.TryGetOrAdd(ImMap`1 items, Int32 id, CreateScopedValue createValue, Int32 disposalOrder) in C:\\azure-webjobs-sdk-script\\src\\WebJobs.Script.WebHost\\DependencyInjection\\DryIoc\\Container.cs:line 7840\r\n at DryIoc.Scope.GetOrAdd(Int32 id, CreateScopedValue createValue, Int32 disposalOrder) in C:\\azure-webjobs-sdk-script\\src\\WebJobs.Script.WebHost\\DependencyInjection\\DryIoc\\Container.cs:line 7825\r\n at DryIoc.Factory.ApplyReuse(Expression serviceExpr, Request request) in C:\\azure-webjobs-sdk-script\\src\\WebJobs.Script.WebHost\\DependencyInjection\\DryIoc\\Container.cs:line 6595\r\n at DryIoc.Factory.GetExpressionOrDefault(Request request) in C:\\azure-webjobs-sdk-script\\src\\WebJobs.Script.WebHost\\DependencyInjection\\DryIoc\\Container.cs:line 6555\r\n at DryIoc.Factory.GetDelegateOrDefault(Request request) in C:\\azure-webjobs-sdk-script\\src\\WebJobs.Script.WebHost\\DependencyInjection\\DryIoc\\Container.cs:line 6625\r\n at DryIoc.Container.ResolveAndCacheDefaultFactoryDelegate(Type serviceType, IfUnresolved ifUnresolved) in C:\\azure-webjobs-sdk-script\\src\\WebJobs.Script.WebHost\\DependencyInjection\\DryIoc\\Container.cs:line 210\r\n at DryIoc.Container.DryIoc.IResolver.Resolve(Type serviceType, IfUnresolved ifUnresolved) in C:\\azure-webjobs-sdk-script\\src\\WebJobs.Script.WebHost\\DependencyInjection\\DryIoc\\Container.cs:line 195\r\n at Microsoft.Azure.WebJobs.Script.WebHost.DependencyInjection.JobHostServiceProvider.GetService(Type serviceType, IfUnresolved ifUnresolved) in C:\\azure-webjobs-sdk-script\\src\\WebJobs.Script.WebHost\\DependencyInjection\\JobHostServiceProvider.cs:line 101\r\n at Microsoft.Azure.WebJobs.Script.WebHost.DependencyInjection.JobHostServiceProvider.GetService(Type serviceType) in C:\\azure-webjobs-sdk-script\\src\\WebJobs.Script.WebHost\\DependencyInjection\\JobHostServiceProvider.cs:line 79\r\n at lambda_method(Closure , IServiceProvider , QueryDelegate )\r\n at HotChocolate.Execution.ClassMiddlewareFactory.<>c__DisplayClass0_0`1.<Create>b__1(IServiceProvider s, QueryDelegate n)\r\n at HotChocolate.Execution.ClassMiddlewareFactory.<>c__DisplayClass2_0`1.<CreateDelegate>b__0(IQueryContext context)\r\n at HotChocolate.Execution.ExceptionMiddleware.InvokeAsync(IQueryContext context)"
}
}
],
"contextData": {}
}
Expected behavior I am not sure what fundamental changes occur in between 10.0.1 and 10.2.0, but something is affecting the DI stopping it from working in Azure Functions.
Desktop (please complete the following information):
- OS: Windows 10
- Visual Studio Community 2019
- .Net Core 3.0
- Azure Runtime v3
Additional context
Here are some of the variables I see when the exception is encountered.

My guess is there is a change in what this bit of code does which is causing the DI issues
builder.Services.AddGraphQL(sp => SchemaBuilder.New()
.AddServices(sp)
.AddQueryType<QueryType>()
.AddType<HumanType>()
.Create());
Any pointers or assistance would be greatly apreciated
Thanks, Basil
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 18 (15 by maintainers)
looks like manually registering the HashDocumentProvider after the GraphQL-initialization fixed the issue.
https://github.com/OneCyrus/GraphQL-AzureFunctions-HotChocolate/commit/a37c68a3384e3e47ebad7796318b0cad9b809338#diff-fbd03c53f6f1dc7701583e41f0cda5c8
the feat_nativeDI branch works now with the latest HC version: https://github.com/OneCyrus/GraphQL-AzureFunctions-HotChocolate
Thank you. I have been able to get it working using the suggested approach for now.
Keep me posted if it gets fixed in V 11.
Thanks
i just gave this one a try again. and it doesn’t look like the azure functions DI behaves incorrectly. the scoped instances are created correctly. there’s also a sample on the AzF repo which shows this:
https://github.com/Azure/azure-functions-dotnet-extensions/tree/master/src/samples/DependencyInjection/Scopes (though i had to update the dependencies to get it up and running)
it looks like there’s really an issue where hotchocolate behaves strange in combination with AzF DI.