ninject / Ninject

the ninja of .net dependency injectors
http://ninject.org/
Other
2.68k stars 530 forks source link

Loading assembly using `Assembly.LoadFrom` cause failure to find assemblies in references dlls #399

Open joli03 opened 1 year ago

joli03 commented 1 year ago

Hi

I have a situation where I Bootstrap our host (.NET Framework 4.7.2) under .NET 7 by loadings it's assembly and calling Main. The AppHost use Ninject via Topshelf.Ninject.

var assembly = Assembly.LoadFrom("AppHost.exe");
var program = assembly.GetType("Program");
var main = program.GetMethod("Main", BindingFlags.Static | BindingFlags.Public);
main.Invoke(program, new object[] { Array.Empty<string>() });

In AppHost, Ninject modules are instantiated

 HostFactory.Run(x =>
          {
              x.UseNinject(
                      new MyModule() // More goes here
              );
// Omitted for brevity...
}

MyModule is in a library that has a reference to System.ServiceModel and upon calling base.Load() it crashes

System.IO.FileNotFoundException: Could not load file or assembly 'System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified
File name: 'System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
   at BaseNinjectModule.Load()
   at MyModule.Load()
   at Ninject.Modules.NinjectModule.OnLoad(IKernel kernel)
   at Ninject.KernelBase.Load(IEnumerable`1 m)
   at Ninject.KernelBase..ctor(IComponentContainer components, INinjectSettings settings, INinjectModule[] modules)
   at Ninject.KernelBase..ctor(INinjectModule[] modules)
   at Ninject.StandardKernel..ctor(INinjectModule[] modules)
   at Topshelf.Ninject.NinjectBuilderConfigurator.get_Kernel()

I assume it has to do with not resolving them from the correct context, but can't figure out how to instruct Ninject (or earlier) about this.

Update: It just struck me that the issue might be that System.ServiceModel is not supported in .NET Runtime.. Need to check with the API Compatibility Tool.

Thanks