DependencyContext.Default throws when Assembly.GetEntryAssembly returns null
I have some code that depends on DependencyContext.Default (an extension to IServiceCollection that scans DependencyContext.Default for assemblies. MVC Core gets around this by assuming the IHostingEnvironment.ApplicationName is the entry point assembly name.
MVC Core tooling assumes that DependencyContext.Default can be null (but it can't).
Steps to reproduce
Create an AppDomain and do not set its entry assembly, for example in a unit test:
[Fact]
public void ShouldNotThrowForNullEntryAssembly()
{
DependencyContext defaultContext = null;
Action act = () => defaultContext = DependencyContext.Default;
act.ShouldNotThrow();
defaultContext().NotBeNull();
}
Expected behavior
That it not throw, perhaps with a null object representation that can be Merged with other assemblies
Actual behavior
DependencyContextLoader.Load throws a NullReferenceException
Environment data
Windows 10 x64
@gkhanna79 - I don't think I am the right assignee for this bug
@schellap @pakrym Do either of you own this in Core-Setup repo?
@eerhardt any idea who this should go to?
any idea who this should go to?
Probably someone from the CLI team (either me or @livarcocc). Or possibly @pakrym, if he wants to take it.
I'm fixing this to return "null" when Assembly.GetEntryAssembly() returns null.
Unfortunately, a "null object" can't really be returned because of the way the Merge logic works. It uses the TargetInfo and CompilationOptions of the context it was invoked on. See https://github.com/dotnet/core-setup/blob/master/src/Microsoft.Extensions.DependencyModel/DependencyContext.cs#L72-L78.
Callers will need to check for null returning from DependencyContext.Default, and if they want to "merge" with a different context, do a null check, and just use the other context instead.