NServiceBus.Persistence.Sql icon indicating copy to clipboard operation
NServiceBus.Persistence.Sql copied to clipboard

Container storage session lambda can throw NullReferenceException (NRE)

Open ramonsmits opened this issue 4 years ago • 0 comments

The container registration for storage context can throw a NullReferenceException. This fails any container registrations verifications at startup.

Alternatively the container registration should not throw NullReferenceException but at runtime throw an InvalidOperationExcetpion to make the registration container friendly. This method has been applied in the following pull requests:

  • https://github.com/Particular/NServiceBus.Persistence.Sql/pull/798
  • https://github.com/Particular/NServiceBus.Persistence.Sql/pull/797

Note, that the actual lambda works fine at runtime but that this is only problematic when the lambda is used in a context where no session context is active.

Workaround: To not be affected by this, do not have the DI container verify the registrations.

Example exception when using SimpleInjector and invoke container.Verify() to verify is registrations are valid.

[NullReferenceException: Object reference not set to an instance of an object.]
   <>c__DisplayClass0_0.<Setup>b__2() in /_/src/SqlPersistence/SynchronizedStorage/StorageSessionFeature.cs:35
   NServiceBus.ObjectBuilder.SimpleInjector.<>c__DisplayClass12_0`1.<Configure>b__0() +17
   lambda_method(Closure ) +42
   lambda_method(Closure ) +46
   SimpleInjector.InstanceProducer.BuildAndReplaceInstanceCreatorAndCreateFirstInstance() +41
   SimpleInjector.InstanceProducer.GetInstance() +82

[ActivationException: The registered delegate for type ISqlStorageSession threw an exception. Object reference not set to an instance of an object.]
   SimpleInjector.InstanceProducer.GetInstance() +285
   SimpleInjector.InstanceProducer.VerifyInstanceCreation() +29

[InvalidOperationException: The configuration is invalid. Creating the instance for type ISqlStorageSession failed. The registered delegate for type ISqlStorageSession threw an exception. Object reference not set to an instance of an object.]
   SimpleInjector.InstanceProducer.VerifyInstanceCreation() +131
   SimpleInjector.Container.VerifyInstanceCreation(InstanceProducer[] producersToVerify, Scope verificationScope) +48
   SimpleInjector.Container.VerifyInternal(Boolean suppressLifestyleMismatchVerification) +286
   SimpleInjector.Container.Verify() +53
   _00067261.<ConfigureAsync>d__1.MoveNext() in C:\Users\Ramon\src\support\00067261\00067261\Startup.cs:126
   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +32
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +62
   System.Runtime.CompilerServices.TaskAwaiter.GetResult() +29
   _00067261.Startup.Configuration(IAppBuilder app) in C:\Users\Ramon\src\support\00067261\00067261\Startup.cs:65

ramonsmits avatar Oct 08 '21 10:10 ramonsmits