System.ArgumentNullException: Value cannot be null.
Unity.Microsoft.DependencyInjection gives exception if running in a docker container.
Unhandled Exception: System.ArgumentNullException: Value cannot be null. Parameter name: source at System.Linq.Enumerable.Reverse[TSource](IEnumerable`1 source) at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication() at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
Docker image is microsoft/aspnetcore:2.0
Program.cs public class Program { public static void Main(string[] args) { BuildWebHost(args).Run(); }
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseUnityServiceProvider()
.UseStartup<Startup>()
.Build();
}
Startup.cs public virtual void ConfigureContainer(IUnityContainer container) {
container.RegisterType<ITest, Test>();
}
You have to give me more details on setup. I am not familiar with it. Perhaps a test app...
This seems to happen on any environment except Windows (I tested it on Ubuntu). This line seems to be the problem: https://github.com/aspnet/Hosting/blob/dev/src/Microsoft.AspNetCore.Hosting/Internal/WebHost.cs#L212 For the love of me if I can figure out why this is happening.
Do you have your filters as type constrained covariant/contravariants?
Can you see in the debugger what is returned by this line?
_applicationServices.GetService<IEnumerable<IStartupFilter>>();
It should be an array with all the resolved filters and one of them will be null. If you could tell me which one I could probably guess why it is null...
@winromulus interesting that the startupFilters variable doesn't have a null check.
The error message that you see states:
Unhandled Exception: System.ArgumentNullException: Value cannot be null. Parameter name: source at System.Linq.Enumerable.Reverse[TSource](IEnumerable`1 source)
The startupFilters list variable is null. That is why calling Reverse() throws that null error.
I might do a null check before the foreach and if null then return builder.Build() or do a configure(builder) before the return.
But I don't really know the code base. Not sure if that is a valid return.
@issafram that is not my code. That is part of the default application builder. What needs to be clarified (at least in my mind) is why those filters are null on Linux and Mac only. The same application works fine on Windows.
@winromulus yes I understood that it isn't your code.
You might want to step into the _applicationServices GetService method to see why you are having this issue.
Sorry that I couldn't help.
This fixed it for me: https://stackoverflow.com/questions/48641224/web-api-core-project-with-unity.
services.AddMvcCore().AddJsonFormatters(); instead of services.AddMvc();
@jgorman-exp I don't think that's a valid solution. It bypasses the code that resolves the services. I don't have a Linux machine with VS code or a debugger to investigate this (the code I was running was on headless machine running Linux).
Did you try to ask @davidfowl ? He is the main expert in the area.
@ENiks do you return null IEnumerable<T> from the container?
No, enumerable returned as empty array.
What was the resolution to this issue? I’m seeing this problem in a .net core 2.1 app that I’m trying to run on CentOS. On Windows it works as expected.
The latest release has fixed several issues on 2.1
Can I try the latest release of 2.1? Or are you implying I have to upgrade to 3.1 to see this fixed?
Just the latest Unity.
Thanks, upgrading to the latest and greatest Unity packages got me past the issue.