Daddoon / Blazor.Polyfill

Blazor server-side Polyfills and fixes for Internet Explorer 11 & Edge Legacy
MIT License
120 stars 19 forks source link

`NullReferenceException` when running multiple tests #65

Closed benm-eras closed 2 years ago

benm-eras commented 3 years ago

Firstly, thank you for this awesome library, without it I wouldn't be able to use Blazor!

I am getting a NullReferenceException on start up when running more than one unit test using Microsoft.VisualStudio.TestTools.UnitTesting.WebApplicationFactory . I have created a minimal reproduction (just the Blazor Server template with Blazor.Polyfill and a test project added) of the issue here, just run all the tests (there are only 2) at once and you should get the following exception:

System.NullReferenceException: Object reference not set to an instance of an object. at React.AspNet.HttpContextLifetimeProvider.get_Registrations() at React.AspNet.HttpContextLifetimeProvider.ReleaseObject() at React.TinyIoC.TinyIoCContainer.CustomObjectLifetimeFactory.Dispose() at React.TinyIoC.SafeDictionary2.set_Item(TKey key, TValue value) at React.TinyIoC.TinyIoCContainer.AddUpdateRegistration(TypeRegistration typeRegistration, ObjectFactoryBase factory) at React.TinyIoC.TinyIoCContainer.RegisterInternal(Type registerType, String name, ObjectFactoryBase factory) at React.TinyIoC.TinyIoCContainer.Register(Type registerType, Type registerImplementation) at React.TinyIoC.TinyIoCContainer.Register[RegisterType,RegisterImplementation]() at React.AssemblyRegistration.Register(TinyIoCContainer container) at React.Initializer.InitializeIoC(Func2 requestLifetimeRegistration) at React.Initializer.Initialize(Func2 requestLifetimeRegistration) at React.AspNet.ReactBuilderExtensions.UseReact(IApplicationBuilder app, Action1 configure, BabelFileOptions fileOptions) at Blazor.Polyfill.Server.BlazorPolyfillMiddlewareExtensions.InitReact(IApplicationBuilder builder) at Blazor.Polyfill.Server.BlazorPolyfillMiddlewareExtensions.UseBlazorPolyfill(IApplicationBuilder builder, BlazorPolyfillOptions options) at Blazor.Polyfill.Server.BlazorPolyfillMiddlewareExtensions.UseBlazorPolyfill(IApplicationBuilder builder, Action`1 configureOptions) at Blazor.Polyfill.Server.BlazorPolyfillMiddlewareExtensions.UseBlazorPolyfill(IApplicationBuilder builder) at Blazor.Polyfill.Startup.Configure(IApplicationBuilder app, IWebHostEnvironment env) in C:\Users\BenMiller\Downloads\Blazor.Polyfill\Blazor.Polyfill\Startup.cs:line 51 at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at Microsoft.AspNetCore.Hosting.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder) at Microsoft.AspNetCore.Hosting.ConfigureBuilder.<>cDisplayClass4_0.b0(IApplicationBuilder builder) at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>cDisplayClass15_0.b1(IApplicationBuilder app) at Microsoft.AspNetCore.Mvc.Filters.MiddlewareFilterBuilderStartupFilter.<>cDisplayClass0_0.gMiddlewareFilterBuilder|0(IApplicationBuilder builder) at Microsoft.AspNetCore.HostFilteringStartupFilter.<>cDisplayClass0_0.b0(IApplicationBuilder app) at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken)

Rather confusingly I named my project Blazor.Polyfill so it's not abundantly clear in the stack trace above, but my call to add your middleware with Blazor.Polyfill.Server.BlazorPolyfillMiddlewareExtensions.UseBlazorPolyfill(IApplicationBuilder builder) is the (slightly) bold line above.

My tests all pass when I run them one at a time, and I have turned off the option to run tests in parallel, but if I try running more than one test at a time I get the exception above.

Do you have any ideas?

UPDATE:

I have fixed an issue with my reproduction project, I messed up when adding to source control and excluded the test project

Daddoon commented 3 years ago

Well, i think the issue is:

See source code here: https://github.com/Daddoon/Blazor.Polyfill/blob/13c1c251f9488aea87ab2b912421a63297598141/src/Blazor.Polyfill.Server/BlazorPolyfillMiddlewareExtensions.cs

I'm not a Unit testing expert. So i would like to know, is yours tests all launched in the same AppDomain / Memory at unit testing ?

I think the error would probably disappear if each tests is loaded in a different AppDomain, as React would not be statically registered twice i imagine, same for low-level Javascript Engine library, at least not in the shared memory.

Keep me informed !