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

Upgrading the Unity.Container dependency will cause a NullReferenceException when starting up #108

Scooletz opened this issue Apr 25, 2019 · 8 comments


Copy link

commented Apr 25, 2019

Who's affected

All customers upgrading to Unity 5.10.x and higher


Upgrading Unity to a newer minor (e.g. v5.11.1 but not limited to this version) has broken the factory resolution. The current nuget range allows people to upgrade to this version and that will cause a NullReferenceException cascade at startup


Downgrade unity to previous versions


A customer reported that when they bumped up the Unity to 5.10.3 the following exception occurred:

Exception Object reference not set to an instance of an object.

at NServiceBus.ReceiveComponent.AddReceivers() in C:\BuildAgent\work\ed946b9f0e4aae01\src\NServiceBus.Core\Receiving\ReceiveComponent.cs:line 146

at NServiceBus.ReceiveComponent.Initialize() in C:\BuildAgent\work\ed946b9f0e4aae01\src\NServiceBus.Core\Receiving\ReceiveComponent.cs:line 68

at NServiceBus.StartableEndpoint.Start() in C:\BuildAgent\work\ed946b9f0e4aae01\src\NServiceBus.Core\StartableEndpoint.cs:line 32


This comment has been minimized.

Copy link

commented Apr 25, 2019

We are experiencing the same issue with all versions back to at least Unity 5.9.7. We have been waiting in version 5.8.13 until all our third parties had fixed the breaking changes in Unity in their own dependencies. The same code but with the MS Dependecy Injection implementation works just fine.

We have already bumped all our packages to the latest versions of both Unity and NServiceBus, since we considered it solved and stable. And I would really prefer not to go through the process of rolling everything back.

Please keep us posted on any progress, if you need any input from us and if not able to solve the problem at least give us a hint regarding what could give this error.

Thanks in advance.



This comment has been minimized.

Copy link

commented Apr 26, 2019

I have investigated the problem further and kept on trying to isolate the problem and making sure that the problem really is in your and not our code.

I have now and established that it most certainly is in your code and that the problem most likely lies in differences in what Endpoint.Create(endpointConfiguration), IStartableEnpoint.Start() in NServiceBus expects and what endpointConfiguration.UseContainer() hands them.

I have ruled everything specific to our code and the following test case reproduces the issue.

public async Task SamplesUnity_UnityContainer_DoesNotThrow()
    const string license = //insert license
    const string connectionString = //insert connection string

    var endpointConfiguration = new EndpointConfiguration(endpointName: "unity-samples");
    endpointConfiguration.SendFailedMessagesTo(errorQueue: "unity-samples-centralized-error-queue");

    var transport = endpointConfiguration.UseTransport<AzureServiceBusTransport>();
    transport.TopicName(topicName: "unity-samples-topic");
    var startableEndpoint = await Endpoint.Create(endpointConfiguration);
    Assert.DoesNotThrowAsync(async () => await startableEndpoint.Start());

I would really appreciate some feedback on how things is progressing on your side and when we can expect a fix rolled out for this given that you can find what causes it.

Thanks in advance!



This comment has been minimized.

Copy link

commented May 2, 2019

Any updates regarding this issue?



This comment has been minimized.

Copy link

commented May 7, 2019

hi @gustavnyberg can you please let me know what NServiceBus.Unity version are you using while encountering errors with previous errors of Unity (pre 5.10.3)?

We are aware of this issue exist in NServiceBus.Unity 10.0.0 and Unity 5.10.3. The latest NServiceBus.Unity version should work for supported Unity packages lower than 5.10.3


This comment has been minimized.

Copy link

commented May 9, 2019

I am using NServiceBus.Unity 10.0.0.

The problem is present at least as far back as Unity (5.9.7).

I have tried every version of Unity from 5.9.7 up to 5.10.3 and I am getting the same error.

Due to other chained dependencies I can't go back further than Unity 5.9.7.

But the last working version for us was Unity 5.8.13 which was before the first severe breaking change in Unity.

The same goes for NServiceBus.

Last working versions were:

<PackageReference Include="NServiceBus" Version="7.1.6" />
<PackageReference Include="NServiceBus.Unity" Version="9.0.0" />
<PackageReference Include="Unity.Container" Version="5.8.13" />


This comment has been minimized.

Copy link

commented May 10, 2019

@gustavnyberg thank you for your response. Unfortunately, I was unable to encounter errors on the version that you specified.

I copied the code from our Samples and changed the transport to ASB, feel free to try it out:

On my end the only combination of versions that it doesn't work for is NServiceBus.Unity 10.0.0
and Unity.Container 5.10.3. Would you be so nice to verify if this is the same for you with this code base?

The reason why I got hung up on that is that what you say (that Unity throws an exception on every version from 5.9.7) is something new to us and I would like to verify this before moving forward with this issue.

The code requires connection string to ASB.


This comment has been minimized.

Copy link

commented May 14, 2019


I have tried the samples you sent.

The .Net Framework project works.

The Core version does not build. You are missing two Nuget packages:

  • NServiceBus.Newtonsoft.Json

  • NServiceBus.Transport.AzureServiceBus

You also need to replace transport.UseForwardingTopology() since that extension method is a part of the NServiceBus.Azure.Transports.WindowsAzureServiceBus Nuget package, which seems to be missing a Core implementation.

I have made some alterations to fix the build errors in the Core project and been able to build it and once able to build and run it, it also works.

I have also tried running our test case and the sample projects and they are working too.

Unfortunately we haven't been able to wait for the feedback from you risking having a late release and last minute fixes.

So we have proceeded by replacing Unity with an in-house .Net Framework compatible implementation of Microsoft's .Net Core Dependency Injection Framework.

I have tried recreating the old conditions by reverting these changes, but I am not 100% sure I have managed to do so.

Anyway, now I am not able to reproduce the error for any other combinations than NServiceBus.Unity 10.0.0 and Unity.Container 5.10.3.

Since we have been able to completely replace Unity and since we no longer can reproduce the error I don't see that there is much left to do but to make the changes I suggested to your sample projects (which are very nice to have when troubleshooting) and maybe add a .Net Standard sample too, then close this particular issue and to fix it for NServiceBus.Unity 10.0.0 and Unity.Container 5.10.3.



This comment has been minimized.

Copy link

commented Jun 28, 2019

I have reproduced this locally as well using Unity.Container v5.11.1. The issue appears to be related to how singleton instance factories are resolved from the container. A failing test has been submitted here.

@danielmarbach danielmarbach added this to the 10.1.0 milestone Jul 30, 2019

@danielmarbach danielmarbach added the bug label Jul 30, 2019

@danielmarbach danielmarbach changed the title Unity 5.10.3 potentially breaks things Upgrading the Unity.Container dependency will cause a NRE when starting up Jul 30, 2019

@danielmarbach danielmarbach changed the title Upgrading the Unity.Container dependency will cause a NRE when starting up Upgrading the Unity.Container dependency will cause a NullReferenceException when starting up Jul 30, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
5 participants
You can’t perform that action at this time.