runtime icon indicating copy to clipboard operation
runtime copied to clipboard

DependencyContext.Default throws when Assembly.GetEntryAssembly returns null

Open jbogard opened this issue 9 years ago • 5 comments

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

jbogard avatar Jan 04 '17 14:01 jbogard

@gkhanna79 - I don't think I am the right assignee for this bug

moozzyk avatar Jan 06 '17 17:01 moozzyk

@schellap @pakrym Do either of you own this in Core-Setup repo?

gkhanna79 avatar Jan 09 '17 22:01 gkhanna79

@eerhardt any idea who this should go to?

schellap avatar Jan 10 '17 04:01 schellap

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.

eerhardt avatar Jan 10 '17 15:01 eerhardt

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.

eerhardt avatar Mar 30 '17 17:03 eerhardt