【问题标题】:MEF - wrong assembly versionMEF - 错误的程序集版本
【发布时间】:2015-11-04 15:50:19
【问题描述】:

我有一个使用 MEF、Webapi 和 OData 的 Web 应用程序。 对于 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 设置为 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.

【问题讨论】:

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


    【解决方案1】:

    可能有很多事情会导致这种情况。需要检查的一些事项:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-15
      • 2018-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-09
      • 1970-01-01
      相关资源
      最近更新 更多