0

我有一個使用MEF,Webapi和OData的Web應用程序。 對於MEF,我創建了一個輔助類,可根據需要加載目錄(初次訪問時)MEF - 錯誤的彙編版本

以下是MEF集成代碼。

Public Shared ReadOnly Property Catalog As AggregateCatalog 
    Get 
     If _catalog Is Nothing Then 

      Dim catFolder = "." 
      Dim path = AppDomain.CurrentDomain.BaseDirectory.ToLower 
      _catalog = new AggregateCatalog() 
      Dim di = New DirectoryInfo(path) 
      Dim dlls = di.GetFileSystemInfos("MyApp.*.dll") 
      _catalog = New AggregateCatalog() 

      For Each dll In dlls 
       Try 
        Dim ac = New AssemblyCatalog(Assembly.LoadFile(dll.FullName)) 
        Dim parts = ac.Parts.ToArray() 'throws ReflectionTypeLoadException 
        _catalog.Catalogs.Add(ac) 

       Catch ex As ReflectionTypeLoadException 
        Log.Instance.Error("Error when Trying to load {0}", dll.FullName) 
        Log.Instance.Error(ex) 
        For Each iex In ex.LoaderExceptions 
         Log.Instance.Error(iex) 
        Next 
       End Try 
      Next 
     End If 
     Return _catalog 
    End Get 
End Property 

我的一個項目(實際的Web應用程序項目)的使用下面的NuGet包(其中包括): 的WebAPI 5.2.3 WebApi.OData 5.7.0

我的問題是,當MEF類嘗試加載該程序集(使用OData和WebApi的程序集,它會拋出運行時錯誤,該程序集記錄如下) 僅在部署中,在生產計算機上發生此問題。看到下面的錯誤,似乎即使項目引用了Sy stem.Web.Http版本5.2.3.0,在運行時System.Web.Http.Odata組件試圖加載System.Web.Http版本5.2.2.0(!?!?!?)

在我的項目,該參考值設置爲正確的DLL(版本5.2.3.0),該dll被設定爲複製本地=真,並在web.config中我有

<dependentAssembly> 
    <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" /> 
    <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" /> 
    </dependentAssembly> 

我沒有需要任何類由MEF在這個項目(程序集)中組成,所以我可以簡單地將它從MEF加載中排除,但是這個錯誤令我困惑,我想找到一個解決方案,以防在其他程序集中遇到類似問題。

以下是錯誤我得到

2015-11-04 07:17:02.7758|ERROR|MyPermitNow.Log|Error when Trying to load m:\web\www.mypermitnow.org\web_1\bg-processor\MyPermitNow.Jurisdiction.dll 
2015-11-04 07:17:02.7758|ERROR|MyPermitNow.Log|System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information. 
    at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module) 
    at System.Reflection.RuntimeModule.GetTypes() 
    at System.Reflection.Assembly.GetTypes() 
    at System.ComponentModel.Composition.Hosting.AssemblyCatalog.get_InnerCatalog() 
    at System.ComponentModel.Composition.Hosting.AssemblyCatalog.GetEnumerator() 
    at System.Linq.EnumerableQuery`1.GetEnumerator() 
    at System.Linq.EnumerableQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() 
    at System.Linq.Buffer`1..ctor(IEnumerable`1 source) 
    at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source) 
    at MyPermitNow.MEFHelper.get_Catalog() 
System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information. 
    at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module) 
    at System.Reflection.RuntimeModule.GetTypes() 
    at System.Reflection.Assembly.GetTypes() 
    at System.ComponentModel.Composition.Hosting.AssemblyCatalog.get_InnerCatalog() 
    at System.ComponentModel.Composition.Hosting.AssemblyCatalog.GetEnumerator() 
    at System.Linq.EnumerableQuery`1.GetEnumerator() 
    at System.Linq.EnumerableQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() 
    at System.Linq.Buffer`1..ctor(IEnumerable`1 source) 
    at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source) 
    at MyPermitNow.MEFHelper.get_Catalog() 
2015-11-04 07:17:02.7758|ERROR|MyPermitNow.Log|System.IO.FileLoadException: Could not load file or assembly 'System.Web.Http, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) 
File name: 'System.Web.Http, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' 

=== Pre-bind state information === 
LOG: DisplayName = System.Web.Http, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 
(Fully-specified) 
LOG: Appbase = file:///M:/web/www.mypermitnow.org/web_1/bg-processor/ 
LOG: Initial PrivatePath = NULL 
Calling assembly : System.Web.Http.OData, Version=5.7.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35. 
=== 
LOG: This bind starts in default load context. 
LOG: No application configuration file found. 
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config. 
LOG: Post-policy reference: System.Web.Http, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 
LOG: Attempting download of new URL file:///M:/web/www.mypermitnow.org/web_1/bg-processor/System.Web.Http.DLL. 
WRN: Comparing the assembly name resulted in the mismatch: Build Number 
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated. 

回答

0

可能有很多事情可能會導致此。有些事情要檢查:

  • 在服務器上有DLL的舊版本有問題嗎?當CSPROJ不使用時,它現在可能具有Private = true,那麼該庫就包含在主應用程序/站點項目中。所以新的不會被複制,但舊的坐在那裏,但在一個更直接的位置,所以它首先加載。
  • 任何應用程序/ web.config轉換可能會取代程序集綁定XML嗎?
  • 如果它不在直接應用程序文件夾中,那麼GAC中包含什麼?
  • 打開Fusion程序集加載日誌記錄