Asp.net web api MEF-错误的程序集版本

Asp.net web api MEF-错误的程序集版本,asp.net-web-api,assemblybinding,assembly-binding-redirect,system.web.http,Asp.net Web Api,Assemblybinding,Assembly Binding Redirect,System.web.http,我有一个使用MEF、Webapi和OData的web应用程序。 对于MEF,我创建了一个helper类,用于按需加载目录(首次访问时) 以下是MEF集成代码 Public Shared ReadOnly Property Catalog As AggregateCatalog Get If _catalog Is Nothing Then Dim catFolder = "." Dim path = AppDomain.C

我有一个使用MEF、Webapi和OData的web应用程序。 对于MEF,我创建了一个helper类,用于按需加载目录(首次访问时)

以下是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的程序集)时,它抛出了一个运行时错误,记录在下面。 这个问题只发生在部署时,在生产机器上。在开发中,一切正常

从下面的错误中可以看出,即使项目引用System.Web.Http版本5.2.3.0,运行时System.Web.Http.Odata程序集似乎也在尝试加载System.Web.Http版本5.2.2.0(!?!?)

在我的项目中,引用设置为正确的dll(版本5.2.3.0),dll设置为Copy Local=True,在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.
2015-11-04 07:17:02.7758 | ERROR | MyPermitNow.Log |尝试加载时出错m:\web\www.MyPermitNow.org\web\u 1\bg processor\MyPermitNow.judiction.dll
2015-11-04 07:17:02.7758 |错误| MyPermitNow.Log | System.Reflection.ReflectionTypeLoadException:无法加载一个或多个请求的类型。有关详细信息,请检索LoaderExceptions属性。
位于System.Reflection.RuntimeModule.GetTypes(RuntimeModule)
在System.Reflection.RuntimeModule.GetTypes()中
在System.Reflection.Assembly.GetTypes()处
在System.ComponentModel.Composition.Hosting.AssemblyCatalog.get_InnerCatalog()中
位于System.ComponentModel.Composition.Hosting.AssemblyCatalog.GetEnumerator()处
在System.Linq.EnumerableQuery`1.GetEnumerator()处
位于System.Linq.EnumerableQuery`1.System.Collections.Generic.IEnumerable.GetEnumerator()
在System.Linq.Buffer`1..ctor处(IEnumerable`1源)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1源)
在MyPermitNow.MEFHelper.get_Catalog()上
System.Reflection.ReflectionTypeLoadException:无法加载一个或多个请求的类型。有关详细信息,请检索LoaderExceptions属性。
位于System.Reflection.RuntimeModule.GetTypes(RuntimeModule)
在System.Reflection.RuntimeModule.GetTypes()中
在System.Reflection.Assembly.GetTypes()处
在System.ComponentModel.Composition.Hosting.AssemblyCatalog.get_InnerCatalog()中
位于System.ComponentModel.Composition.Hosting.AssemblyCatalog.GetEnumerator()处
在System.Linq.EnumerableQuery`1.GetEnumerator()处
位于System.Linq.EnumerableQuery`1.System.Collections.Generic.IEnumerable.GetEnumerator()
在System.Linq.Buffer`1..ctor处(IEnumerable`1源)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1源)
在MyPermitNow.MEFHelper.get_Catalog()上
2015-11-04 07:17:02.7758 | ERROR | MyPermitNow.Log | System.IO.FileLoadException:无法加载文件或程序集'System.Web.Http,Version=5.2.2.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35'或其依赖项之一。定位的程序集的清单定义与程序集引用不匹配。(HRESULT中的异常:0x8013040)
文件名:“System.Web.Http,版本=5.2.2.0,区域性=中性,PublicKeyToken=31bf3856ad364e35”
==预绑定状态信息===
日志:DisplayName=System.Web.Http,版本=5.2.2.0,区域性=neutral,PublicKeyToken=31bf3856ad364e35
(详细说明)
日志:Appbase=file:///M:/web/www.mypermitnow.org/web_1/bg-处理机/
日志:初始PrivatePath=NULL
调用程序集:System.Web.Http.OData,版本=5.7.0.0,区域性=neutral,PublicKeyToken=31bf3856ad364e35。
===
日志:此绑定在默认加载上下文中启动。
日志:未找到应用程序配置文件。
日志:使用主机配置文件:
日志:使用C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config中的计算机配置文件。
日志:发布策略参考:System.Web.Http,版本=5.2.2.0,区域性=中立,PublicKeyToken=31bf3856ad364e35
日志:正在尝试下载新URLfile:///M:/web/www.mypermitnow.org/web_1/bg-处理器/System.Web.Http.DLL。
警告:比较程序集名称导致不匹配:内部版本号
错误:未能完成程序集的设置(hr=0x8013100)。探测已终止。

可能有很多因素会导致这种情况。有些因素需要检查:

  • 服务器上是否有旧版本的DLL出现问题?可能CSPROJ现在对程序集有Private=true,但它不使用,然后该库包含在主应用程序/站点项目中。因此,新库不会被复制,但旧库位于那里,但位于更直接的位置,因此它将首先加载
  • 正在进行的任何app/web.config转换都可能会替换程序集绑定XML吗
  • 如果它不在直接应用程序文件夹中,那么GAC中有什么
  • 打开Fusion程序集加载日志记录