MEF - 错误的装配版本

时间:2015-11-04 15:50:20

标签: asp.net-web-api assemblybinding assembly-binding-redirect system.web.http

我有一个使用MEF,Webapi和OData的网络应用程序。 对于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的程序集时,它会抛出运行时错误,记录如下)。 仅在部署中,在生产计算机上发生此问题。在开发过程中一切正常。

正如您在下面的错误中看到的那样,即使项目引用System.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.

1 个答案:

答案 0 :(得分:0)

可能会有很多事情可能导致这种情况发生。有些事要检查:

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