代码之家  ›  专栏  ›  技术社区  ›  Michael La Voie Frederik Gheysels

.NET选取错误的引用程序集版本

  •  127
  • Michael La Voie Frederik Gheysels  · 技术社区  · 13 年前

    我刚刚将一个现有的项目复制到一台全新的机器上,开始在它上开发,并且遇到了一个与我引用的某个程序集版本有关的问题(实际上是一个telerik dll)。

    项目最初引用了程序集的旧版本(我们称之为v1.0.0.0)。我的新机器安装了程序集的最新版本,所以我想我已经更新了它(让我们称之为新版本v2.0.0.0)。

    现在问题出现了:如果我将旧的v1.0.0.0 dll复制到项目文件夹中,并将其作为引用添加,则网站将毫无问题地启动。如果删除该引用(同时从系统中删除旧的dll)并添加新版本(v2.0.0.0),则页面显示以下异常:

    无法加载文件或程序集 'XXXXXX,版本=1.0.0.0, 文化=中性, publickeytoken=121fae78165ba3d4'或 它的一个依赖项。所在地 程序集的清单定义执行了 与程序集引用不匹配。 (来自hresult的异常:0x80131040)

    显然,代码正在查找过期版本,找不到它。但是为什么呢?

    我将该版本号的解决方案文件夹设为灰色,找不到单个引用。我仔细检查了.csproj文件的文本,发现该版本正确地显示了最新版本,hintpath正确地显示了新dll的路径。此外,因为我没有在系统上安装旧的DLL,所以它不会显示在我的GAC中(尽管V2.0.0.0是这样的,正如预期的那样)。

    然后,我启用了Fusion Log Viewer,试图找出为什么要查找旧版本,但没有运气:

    Assembly Load Trace: The following information can be helpful to determine why the assembly 'XXXXXX, Version=1.0.0.0, Culture=neutral, PublicKeyToken=121fae78165ba3d4' could not be loaded.
    
    
    === Pre-bind state information ===
    LOG: User = MyComp\me
    LOG: DisplayName = XXXXXX, Version=1.0.0.0, Culture=neutral, PublicKeyToken=121fae78165ba3d4
     (Fully-specified)
    LOG: Appbase = file:///d:/My Documents/Visual Studio 2010/Projects/CoolProj/WebApp/
    LOG: Initial PrivatePath = d:\My Documents\Visual Studio 2010\Projects\CoolProj\WebApp\bin
    Calling assembly : WebApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
    ===
    LOG: This bind starts in default load context.
    LOG: Using application configuration file: d:\My Documents\Visual Studio 2010\Projects\CoolProj\WebApp\web.config
    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: XXXXXX, Version=1.0.0.0, Culture=neutral, PublicKeyToken=121fae78165ba3d4
    LOG: Attempting download of new URL file:///C:/WINDOWS/Microsoft.NET/Framework/v4.0.30319/Temporary ASP.NET Files/root/90233b18/10d54998/XXXXXX.DLL.
    LOG: Attempting download of new URL file:///C:/WINDOWS/Microsoft.NET/Framework/v4.0.30319/Temporary ASP.NET Files/root/90233b18/10d54998/XXXXXX/XXXXXX.DLL.
    LOG: Attempting download of new URL file:///d:/My Documents/Visual Studio 2010/Projects/CoolProj/WebApp/bin/XXXXXX.DLL.
    WRN: Comparing the assembly name resulted in the mismatch: Major Version
    ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.
    

    所有的一切都是从寻找那个旧的集会开始的。我试着在网上找到一个解决方案,看到了类似的情况 SO question 但这似乎与我的问题完全相反。那个发问者的程序发现了错误的DLL而不是引用的DLL。然而,我的问题是,程序神秘地在寻找错误的dll,当正确的dll可以在bin文件夹和gac中本地找到时,无法找到它。

    为什么我要找旧版本?我还能在哪里找到这个坏的证明人?

    21 回复  |  直到 5 年前
        1
  •  139
  •   Chris Conway    13 年前

    我猜您使用的另一个程序集正在引用旧的dll。您是否熟悉正在使用的所有其他项目引用,并且其中任何一个引用了telerik DLL?

    你能像这样在web.config文件中加入绑定重定向吗?

    <dependentAssembly>
     <assemblyIdentity name="Telerik" publicKeyToken="121fae78165ba3d4"/>
     <bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0"/>
    </dependentAssembly>
    
        2
  •  24
  •   NotMe    13 年前

    我和克丽丝·康威在这个问题上(投了赞成票)。问题是,您所引用的是项目中的一个telerik程序集,它引用的是另一个不存在的程序集。

    首先,我不会在GAC中安装任何供应商(即telerik)组件。无论如何,telerik的东西被编译成两个程序集(telerik.web.design和telerik.web.ui)。只需将它们与应用程序一起部署。

    其次,在每个.proj文件(如.csproj)中 <reference include..> 指向telerik.web.ui文件。这通常包含一个版本号。确保放入bin文件夹的程序集与该版本匹配。

    第三,确保所有项目都使用最新的程序集。还要确保他们从本地路径而不是GAC获取程序集。(我真的不喜欢GAC。在我参与过的一些项目中,它没有引起任何问题的结束。我们通常有一个“assembly”文件夹,所有项目都将其用于外部程序集引用。

    第四,每次加载网站项目时,Visual Studio都会自动搜索GAC,如果在GAC中找到某些内容,则会重新定位程序集位置。我不记得它是否曾经为Web应用程序项目做过这样的工作,但是我很久没有遇到过这样的问题了。这可能会在部署期间导致类似的问题。

    第五,您可以在web.config中重新绑定程序集的版本号。在 runtime/assemblybinding

      <dependentAssembly>
        <assemblyIdentity name="Telerik.Web.UI" publicKeyToken="121fae78165ba3d4" />
        <bindingRedirect oldVersion="2008.0.0.0-2020.0.0.0" newVersion="2010.02.0713.35" />
      </dependentAssembly>
    
        3
  •  21
  •   RayLoveless    11 年前

    enter image description here

        4
  •  6
  •   Ronney Lira    8 年前

    • C:\Users\USERNAME\.nuget\packages\

        5
  •  3
  •   Reynan de la Cruz    11 年前

        6
  •  2
  •   AspNetDev    13 年前

        7
  •  2
  •   warrickh    12 年前

        8
  •  2
  •   Chris Moschini    10 年前

        9
  •  1
  •   Gedeon    11 年前

        10
  •  1
  •   RichieRich    11 年前

        11
  •  1
  •   brando    9 年前

        12
  •  1
  •   Edd    6 年前

        13
  •  0
  •   SQLKing    11 年前

        14
  •  0
  •   Carl Onager    10 年前

        15
  •  0
  •   Tim    10 年前

        16
  •  0
  •   AlanK    6 年前
    enter image description here

        17
  •  0
  •   aBlaze    6 年前

    Microsoft.IdentityModel.Clients.ActiveDirectory

        18
  •  0
  •   Siphamandla Ngwenya    6 年前

        19
  •  0
  •   Ben    5 年前

    .csproj

    <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release-ABC|AnyCPU'">
        <OutputPath>bin\Release-ABC</OutputPath>
        <PlatformTarget>x64</PlatformTarget>
    </PropertyGroup>
    

    PlatformTarget

        20
  •  0
  •   Ragavan Rajan    5 年前
        21
  •  0
  •   mimo    5 年前

    XXX.dll XXX-new-3.3.0.0.dll