2011-12-13 78 views
1

當我使用AssemblyScanner.FromAssembliesInPath方法時會拋出異常。使用AssemblyScanner FromAssembliesInPath方法時,如何避免使用vshost32的FileNotFoundException?

我該如何解決這個問題?我正在使用the Ninject.Extensions.Conventions extension。我正在使用一小時前從NuGet中抓取的版本。

  • Ninject 2.2.1.4更新5/7/2011
  • Ninject.Extensions.Conventions 2.2.0.5 5/7/2011

我使用的掃描儀裝配這樣的更新:

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

避免這種臭名昭着的異常的方法是返回一個枚舉值,而不是:http://thedailywtf.com/Articles/What_Is_Truth_0x3f_.aspx –

+0

我同意。如果你沒有幽默感,那麼請忽略任何關於dailywtf的鏈接。 –

+0

@Windowsprogrammer:哦,哈哈!我完全摒棄了這一點。感謝您的鏈接,將刪除評論:) –

回答

4

這是一個錯誤,但不會再固定的,因爲目前的測試版是一個新界面的完整重新實現。

替換FromAssembliesInPathFrom(Assembly.GetEntryAssembly())應該這樣做。 (或者如果你沒有創建一個可執行文件,將其徹底刪除)所有的dll文件都已經被FromAssembliesMatching("*.dll")加載了,唯一可以接受的exe文件就是正在運行的程序集。

+0

+1;聽起來不錯:)當我開始工作時,我會嘗試一下。我很確定這會解決問題。問題:執行這些行中的每一行*過濾*現有的選定程序集,或者它們是否可能拉入其他程序集? –

+0

我不明白你的意思。但是,如果程序集引用其他的(例如System.SomeSystemAssembly),那麼它們將被.NET加載。但是這些程序集將不會被掃描。 –

+0

我想如果界面正在改變,評論可能毫無意義。但我希望這些行自己不做任何事情,並像SQL或Linq'where where子句一樣工作 - 'x.FromAssembliesMatching(「*。dll「);'添加一個只接受DLL的過濾器,'x.FromAssembliesInPath(」。「);'添加一個只接受當前路徑中的程序集的過濾器。 ? –

相關問題