-
Notifications
You must be signed in to change notification settings - Fork 5
Description
Describe the problem
When trying to use NServiceBus for Azure Functions and register Entity Framework (EF) into the container, get the following exception:
Unable to resolve service for type 'Microsoft.Azure.WebJobs.Script.IEnvironment' while attempting to activate
'Microsoft.Azure.WebJobs.Script.Configuration.ScriptHostOptionsSetup'.
After raising the Azure/azure-functions-host#6783 issue with Azure Functions it became apparent that the way NServiceBus.AzureFunctions is leveraging the ServiceCollection is wrong for both the static and non-static usages.
When instantiating an NServiceBus.AzureFunctions FunctionEndpoint using the static approach, the ServiceCollection does not share any services/components with Azure Functions SDK. When instantiating an NServiceBus.AzureFunctions FunctionEndpoint using the non-static approach using DI, the ServiceCollection that we get in the Startup class to configure services is a subset of all the services. Regardless of the approach, the service provider built by NServiceBus will not contain all the dependencies.
Potential workaround
Not possible with the current implementation.
Potential solutions
The NServiceBus.AzureFunctions implementation needs to treat the Azure Functions hosted endpoint as an endpoint with an externally-managed container, using the service collection and service provider supplied by Azure Functions. This means using the DI style of Functions and not the static implementation, providing an extension method such as .UseNServiceBus(...). This extension method would be used to configure the FunctionEndpoint.
Additional information to help with this issue
I've created a repository with all of the findings
- Statically configured NServiceBus endpoint
- NServiceBus endpoint configured using DI (Startup class approach)
- Working native Azure Function using DI
- Attempt to reproduce the issue w/o using NServiceBus but emulating what it does
- A console application to generate a kick-off message
Summary
This issue prevents the adoption of NServiceBus.AzureFunctions when 3rd party dependencies fail to be resolved.