-
Notifications
You must be signed in to change notification settings - Fork 740
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
Crash on page reload (Pressing F5) in ASP.Net.Core 2.2 while using Unity container #1301
Comments
@ENikS when I run this example https://github.com/unitycontainer/examples/tree/master/src/web/ASP.Net.Unity.Example, I get an ArgumentNullException:
|
Yes, when service provider is disposed the container reference is set to null. If you request a service from disposed provider it fails with NullReferenceException. I will add a null check and throw an ObjectDisposedException instead. |
@ENikS this callstack is different from the issue you called out above. This happens at startup. |
My apologies. It was running in Diagnostic mode. I switched the mode in example and released new version of Please update example and try again. |
Did you find the cause for the issue? I was considering spending some time and perhaps trying to solve the issue but was wandering if you would be willing to accept a PR with the fix. If not, perhaps you could suggest what Unity should do to work around this problem? It is still broken and I can not release new version of the extension without doing something about this bug. |
@ENikS does the same repro work now? |
I have not changed it
|
I tried to run it and I get the same argument null exception. Do I need to update the example to 5.10.2? |
The latest throws correct error
|
There's a bug in the container implementation that causes this issue. Here's the example. Even though Foo is a singleton, the service provider injected is from the first scope it's resolved from. That's a recipe for disaster. It's possible this bug was always there (it needs to be fixed ASAP if that's the case). using System;
using Microsoft.Extensions.DependencyInjection;
using Unity;
using Unity.Microsoft.DependencyInjection;
namespace ASP.Net.Unity.Example
{
public class Program
{
public class Foo
{
public Foo(IServiceProvider sp)
{
ServiceProvider = sp;
}
public IServiceProvider ServiceProvider { get; }
}
public static void Main(string[] args)
{
var container = new UnityContainer();
var factory = new ServiceProviderFactory(container);
var services = new ServiceCollection();
services.AddSingleton<Foo>();
var sp = factory.CreateServiceProvider(services);
IServiceProvider scopedSp1 = null;
IServiceProvider scopedSp2 = null;
Foo foo1 = null;
Foo foo2 = null;
using (var scope1 = sp.CreateScope())
{
scopedSp1 = scope1.ServiceProvider;
foo1 = scope1.ServiceProvider.GetRequiredService<Foo>();
}
using (var scope2 = sp.CreateScope())
{
scopedSp2 = scope2.ServiceProvider;
foo2 = scope2.ServiceProvider.GetRequiredService<Foo>();
}
Console.WriteLine($"foo1.ServiceProvider == foo2.ServiceProvider = {foo1.ServiceProvider == foo2.ServiceProvider}");
Console.WriteLine($"foo1.ServiceProvider == scopedSp1 = {foo1.ServiceProvider == scopedSp1}");
Console.WriteLine($"foo2.ServiceProvider == scopedSp2 = {foo2.ServiceProvider == scopedSp2}");
}
}
} UnityContainer:
DefaultContainer:
This should probably be part of our compat testing. I'll make sure we add that case. |
Makes sense. Thank you!
I am driving from NY to LA at the moment. Will fix when I get to California
|
Describe the bug
Calling disposed service provider on refresh.
While using Unity as a Service Provider, loading a page the first time runs fine but on refresh, hitting F5, this exception is thrown.
To Reproduce
Steps to reproduce the behavior:
This example reproduces the behavior. It was working fine with previous releases.
Additional context
The instance of service provider is released by this line
The stack at the moment of Dispose call:
the offending call to disposed ServiceProvider is coming from here. The stack at the moment of call as follows:
The text was updated successfully, but these errors were encountered: