使用AssemblyScanner.FromAssembliesInPath
方法时会抛出异常。
如何解决此问题?
我正在使用the Ninject.Extensions.Conventions extension。我使用的是一小时前刚从NuGet抓取的版本。
我正在使用这样的组装扫描仪:
Kernel.Scan(x =>
{
x.FromAssembliesMatching("*.dll");
x.FromAssembliesInPath(".");
x.WhereTypeInheritsFrom(typeof(DbContext));
x.BindWithDefaultConventions();
x.InTransientScope();
});
尝试从Visual Studio运行我的程序时,会抛出此异常:
未处理的异常:System.IO.FileNotFoundException:无法加载文件或程序集'vshost32,Version = 10.0.0.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a'或其依赖项之一。系统找不到指定的文件。
这行代码抛出了这个:
x.FromAssembliesInPath(".");
这是整个堆栈跟踪:
Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'vshost32, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
at System.Reflection.Assembly.Load(String assemblyString)
at System.UnitySerializationHolder.GetRealObject(StreamingContext context)
at System.AppDomain.Load(AssemblyName assemblyRef)
at Ninject.Extensions.Conventions.AssemblyScanner.<FindAssemblies>d__10.MoveNext() in c:\Projects\Ninject\Maintenance2.2\ninject.extensions.conventions\src\Ninject.Extensions.Conventions\AssemblyScanner.cs:line 238
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at Ninject.Extensions.Conventions.AssemblyScanner.From(IEnumerable`1 assemblies, Predicate`1 filter) in c:\Projects\Ninject\Maintenance2.2\ninject.extensions.conventions\src\Ninject.Extensions.Conventions\AssemblyScanner.cs:line 300
at Ninject.Extensions.Conventions.AssemblyScanner.FromAssembliesInPath(String path, Predicate`1 assemblyFilter) in c:\Projects\Ninject\Maintenance2.2\ninject.extensions.conventions\src\Ninject.Extensions.Conventions\AssemblyScanner.cs:line 325
at Ninject.Extensions.Conventions.AssemblyScanner.FromAssembliesInPath(String path) in c:\Projects\Ninject\Maintenance2.2\ninject.extensions.conventions\src\Ninject.Extensions.Conventions\AssemblyScanner.cs:line 312
at (Line for my code, `x.FromAssembliesInPath(".");`)
at Ninject.Extensions.Conventions.ExtensionsForIKernel.Scan(IKernel kernel, Action`1 scan) in c:\Projects\Ninject\Maintenance2.2\ninject.extensions.conventions\src\Ninject.Extensions.Conventions\ExtensionsForIKernel.cs:line 43
at (Line for my code, `Kernel.Scan(x =>`)
at Ninject.Modules.NinjectModule.OnLoad(IKernel kernel) in c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\Modules\NinjectModule.cs:line 60
at Ninject.KernelBase.Load(IEnumerable`1 modules) in c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\KernelBase.cs:line 222
at Ninject.KernelBase..ctor(IComponentContainer components, INinjectSettings settings, INinjectModule[] modules) in c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\KernelBase.cs:line 102
at Ninject.KernelBase..ctor(INinjectModule[] modules) in c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\KernelBase.cs:line 57
at Ninject.StandardKernel..ctor(INinjectModule[] modules) in c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\StandardKernel.cs:line 31
at (Line for my code, `using(IKernel kernel = new StandardKernel(new SomeModule()))`)
答案 0 :(得分:4)
这是一个错误,但不再修复,因为当前的测试版是一个完全重新实现的新界面。
FromAssembliesInPath
替换From(Assembly.GetEntryAssembly())
也应该这样做。 (或者如果你没有创建可执行文件,则完全删除它)所有dll都已由FromAssembliesMatching("*.dll")
加载,唯一可接受的exe程序集是正在运行的程序集。