修复 LINQPad 中 System.ServiceModel 的 "They type 'X' exists in both 'Y' and 'Z'" C# 错误

Fixing "They type 'X' exists in both 'Y' and 'Z'" C# error for System.ServiceModel in LINQPad

如果我:

  1. 在 LINQPad 6 中创建一个新的 C# 查询。
  2. 添加 System.ServiceModel.Http NuGet package or another package that references it.
  3. 尝试从 System.ServiceModel 命名空间实例化一个 class,例如 System.ServiceModel.BasicHttpBinding.

指向以下 .linq 文件:

<Query Kind="Expression">
  <NuGetReference>System.ServiceModel.Http</NuGetReference>
</Query>

new System.ServiceModel.BasicHttpBinding()

然后我得到一个编译错误: CS0433 The type 'BasicHttpBinding' exists in both 'System.Private.ServiceModel, Version=4.7.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' and 'System.ServiceModel.Http, Version=4.7.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

如果我在 Visual Studio 中创建项目并添加相同的 NuGet 包和代码,我不会收到此错误。

根据 the C# Language reference for the error,应该可以通过使用 -reference 编译器选项或不引用其中一个程序集来解决。但是,我似乎找不到在 LINQPad 中使用此编译器选项的方法,也找不到任何方法删除对 System.Private.ServiceModel.

的程序集引用

如何修复错误?

这是 LINQPad 中的一个错误,由一个模糊的场景触发。 System.Private.ServiceModel 包包含一个包含运行时所需程序集的 lib 文件夹,以及一个包含 underscore.underscore 文件的 ref 文件夹,该文件指示编译器不应引用任何程序集。因为 LINQPad 没有找到引用程序集,它向编译器提供了 lib 文件夹中的程序集,这导致了错误。

我已准备好修复程序,目前正在进行回归测试 运行。该修复程序可能会进入 6.11.2 beta 版本,应该会在一两天内发布。