【问题标题】:error while getting a response from stripe "Could not load file or assembly 'System.Collections.Immutable'从条带获取响应时出错“无法加载文件或程序集‘System.Collections.Immutable’
【发布时间】:2019-08-18 23:06:25
【问题描述】:

引用的条纹库:Stripe.net
运行时版本:v4.0.30319
版本:25.2.0.0

从条带获得响应并调用收费函数()时,我收到错误“无法加载文件或程序集'System.Collections.Immutable,Version=1.2.3.0,Culture=neutral” 下面提到的代码

<form action="/Pay/Charge" method="POST">
    <article>
        <label>Amount: $5.00</label>
    </article>
    <script src="//checkout.stripe.com/v2/checkout.js"
            class="stripe-button"
            data-key="@ViewBag.StripePublishKey"
            data-locale="auto"
            data-description="Sample Charge"
            data-amount="500">
    </script>
</form>

这个视图调用下面的charge ActionResult

public ActionResult Charge(string stripeEmail, string stripeToken)
        {
            var customers = new CustomerService();
            var charges = new ChargeService();

            var customer = customers.Create(new CustomerCreateOptions
            {
                Email = stripeEmail,
                SourceToken = stripeToken
            });

//Error on this line--↓

            var charge = charges.Create(new ChargeCreateOptions
            {
                Amount = 500,
                Description = "Sample Charge",
                Currency = "usd",
                CustomerId = customer.Id
            });

            // further application specific code goes here

            return View();
        }


Additional information: Could not load file or assembly 'System.Collections.Immutable, Version=1.2.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

InnerException:Null

【问题讨论】:

  • 作为一个nuget包,你确定它已经发送到服务器了吗?
  • 错误信息的重要部分是The located assembly's manifest definition does not match the assembly reference.。这意味着您使用的'System.Collections.Immutable dll 与库预期的不同。如果您搜索错误消息,您会发现很多重复项。解决方案是在您的 web.config 中添加一个绑定重定向,该重定向指向您添加到项目中的 dll 版本。如果您通过 NuGet 添加该程序集,则会自动添加绑定重定向元素
  • 这是一个演示项目,我正在我的本地机器上尝试@BugFinder
  • System.Collections.Immutable 的最新稳定版本是 1.5。添加该版本并确保 web.config 包含重定向到此的条目
  • Stripe.NET 26.1.0 也面临同样的问题。而现在System.Collections.Immutable 的最新版本是 1.7.0。我假设它向后兼容 1.2.3.0,我不明白为什么 Stripe.NET 会在这种传递依赖上绊倒......

标签: c# .net asp.net-mvc stripe-payments


【解决方案1】:

这是我尝试解决此问题时的看法:

  1. 我需要将System.Collections.Immutable 添加到我的web.config 文件中(或者如果您碰巧在桌面应用程序上工作,那就是应用程序配置)。在出现此错误之前,web.config 中没有该程序集的条目。
  2. NuGet 包版本与程序集版本不同无论出于何种原因。这阻碍了我几个小时,因为我知道我必须将版本重定向添加到我的 web.config

目前NuGet 包版本是1.7,而汇编版本是1.2.5.0。最后我通过PowerShell 发现了这一点:

PS> [Reflection.AssemblyName]::GetAssemblyName('C:\Users\MyUserName\Documents\WebAppSourceCodePath\bin\System.Collections.Immutable.dll').Version

Major  Minor  Build  Revision
-----  -----  -----  --------
1      2      5      0

知道我添加了这个条目:

<configuration>
...
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
...
      <dependentAssembly>
        <assemblyIdentity name="System.Collections.Immutable" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="1.2.5.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
...
</configuration>

此外,您需要正确的publicKeyToken 和上面条目中的完整规范。也可以使用 PowerShell 读取。

PS> [System.Reflection.Assembly]::LoadFile('C:\Users\MyUserName\Documents\WebAppSourceCodePath\bin\System.Collections.Immutable.dll').FullName
System.Collections.Immutable, Version=1.2.5.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a

【讨论】:

    【解决方案2】:

    如果 System.Collections.Immutable 已安装,请在 packages.config 中检查它的版本,以防它无法工作,手动修改为与登录时相同的版本异常并重建您的解决方案以安装所需的版本。

    <package id="System.Collections.Immutable" version="1.2.3.0" targetFramework="net45" />
    

    更新:

    最初的问题是关于 Stripe.net 的 25.2.0 版本,在那个旧版本中依赖于 System.Collections.Immutable,但是最新版本不再依赖于:

    查看链接Stripe.net

    【讨论】:

    • 为什么 1.5 或 1.7 不能向后兼容 1.2.3.0?我真的不想仅仅因为 Stripe.NET 内部有些东西而降级该软件包
    • @CsabaToth 最新版本不再依赖System.Collections.Immutable,我会更新我的答案请检查它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-07
    • 2023-03-05
    • 2011-08-12
    相关资源
    最近更新 更多