7

I've managed to reproduce one of the errors in a test project with a similar structure to my production code. It consists of three simple projects:

Common (class library):

namespace Common
{
    public enum PrimaryColor
    {
        Red,
        Green,
        Blue
    };
}

Library (WCF service library), which has a reference to Common:

using Common;

namespace Library
{
    [ServiceContract]
    public interface ILibrary
    {
        [OperationContract]
        PrimaryColor GetColor();
    }

    public class Library : ILibrary
    {
        public PrimaryColor GetColor()
        {
            return PrimaryColor.Red;
        }
    }
}

ClientApp (console application), which has a reference to Common, and a service reference to Library called "LibraryServiceReference":

using Common;
using ClientApp.LibraryServiceReference;

namespace ClientApp
{
    class Program
    {
        static void Main(string[] args)
        {
            LibraryClient client = new LibraryClient("WSHttpBinding_ILibrary");
            PrimaryColor color = client.GetColor();
        }
    }
}

The app.config files in ClientApp and Library are auto-generated and I have not modified them, and I have not changed the default configuration for the LibraryServiceReference in ClientApp.

When I compile this solution, I get the following errors in the ClientApp project:

Error 1

'PrimaryColor' is an ambiguous reference between 'Common.PrimaryColor' and 'ClientApp.LibraryServiceReference.PrimaryColor'

Error 2

Cannot implicitly convert type 'ClientApp.LibraryServiceReference.PrimaryColor' to 'Common.PrimaryColor'. An explicit conversion exists (are you missing a cast?)

please help me to fix this.

4

10 回答 10

4

确保在Add service referenceConfigure Service Reference的高级选项中选择了“在所有引用的程序集中重用类型”

在此处输入图像描述

于 2012-06-18T13:37:00.660 回答
3

这是因为您正在构建 x64 而不是“AnyCpu”。我现在正在遇到这个问题,并试图弄清楚它是否是一个错误,或者它是否是预期的行为。

于 2016-06-23T19:04:40.427 回答
1
  1. 像这样装饰你的枚举:

    namespace Common
    {
        [DataContract]
        public enum PrimaryColor
        {
            [EnumMember]
            Red,
            [EnumMember]
            Green,
            [EnumMember]
            Blue
        };
    }
    
  2. 更新您的服务参考(检查重用类型,就像马克所说)。

  3. 重建您的客户端代码。

于 2012-06-18T14:25:05.143 回答
1

我已经多次以无害、不可预测的方式出现这个问题!我想我会分享我上次是如何“修复”它的。

我正在使用 Visual Studio 2013 - 但已经遇到了问题。

模棱两可的引用似乎是自己出现的。我没有做任何值得注意的事情。在最近的例子中,我正在调试一些代码,突然我有 7 个,然后是 22 个,然后是 49 个错误——性质相同。

我完全删除了服务引用并重新添加了它。简单地修改重用类型没有任何作用。我的解决方案有 WCF 服务、类库、UI 和控件库。我还删除了类库的 using - in some code behind。

这是一个非常麻烦的问题,幸好每隔几周才会发生一次。为什么这行得通?超出我的工资等级。我感受到了你的痛苦!希望这可以帮助。在这种情况下,当我在 xaml 页面上打开一些代码时,再次出现错误。

在此处输入图像描述

于 2016-08-09T14:47:33.287 回答
0

听起来您同时控制客户端和服务器代码。为什么要创建服务引用,是有特定原因还是被认为更容易?

在您控制客户端服务器应用程序双方的项目中,您最好创建一个“合同程序集”(这可能是您的常用程序集)。这包含与合同有关的接口和对象,应该由您的客户端和您的服务器引用。为了与服务通信,客户端使用创建代理类ChannelFactory,不需要专用的 WCF 客户端。

例子:

ChannelFactory<ISampleService> factory = new ChannelFactory<ISampleService>("Binding_from_config");

ISampleService sampleService = factory.CreateChannel();

sampleService.SomeCall();

factory.Close();

工厂模式也使它成为通过 IoC 注入代理的理想选择。

与创建服务引用相比,引用公共程序集的好处是:

  • 没有模棱两可的参考,因为不需要自动生成的类。
  • 您不必在每次更改合同时更新您的服务参考。
于 2012-06-18T14:33:07.717 回答
0

对于它的价值,在将我的数据合约移动到单独的库后,我遇到了同样的错误。多次更新服务引用并尝试所有设置组合以重用程序集,但无济于事。

最终为我解决的问题是 1)重新启动 Visual Studio 和 2)更新服务参考。服务定义中 Reference.cs 中自动生成的代码看起来非常不同,并且没有复制我的数据协定类。它使用了我图书馆中的正确参考。所以必须在IDE中缓存一些东西。

希望其他人觉得这很有用。

于 2019-06-20T16:43:54.260 回答
0

我可以通过右键单击服务引用来解决此问题,然后从“在所有引用的程序集中重用类型”更改为“在指定的引用程序集中重用类型”,然后检查特定的公共程序集。

于 2019-09-11T15:03:08.250 回答
0

只需从您的 ClientApp 项目中删除对 Common 项目的引用,错误就会消失。当您为服务创建代理时,服务中的所有相关代码都必须注入到代理中。如果您希望您的类型与服务端的类型相同,只需在生成代理代码时启用“重用类型”选项(否则它们将被放在不同的命名空间下)。

于 2020-03-13T11:29:59.027 回答
-1

这里的问题是 PrimaryColor 存在于 Common 和 ClientApp.LibraryServiceReference 中,并且您在类中引用了这两个命名空间。

要克服这个问题,要么明确引用您需要的实例,即

Common.PrimaryColor color = ....

或设置别名:

using Service = ClientLibraryServiceReference; ...

Service.PrimaryColor color = ......

于 2012-06-18T13:36:46.150 回答
-2

在进行服务参考时,是否有一些选项会说:“​​在生成的服务合同中包含常见类型”?

我的想法是,在您的服务参考中,类是“复制的”,这就是您收到此错误的原因。检查生成的服务文件,然后删除并使用“添加服务参考”再次添加它们,然后查看您有哪些选项。

编辑

虽然我几乎可以肯定 Type PrimaryColor 被定义了两次。一次在公共项目中,一次在您的服务参考中,您也可以在您的 clientApp 中尝试此操作(更明确地指定 PrimaryColor 类型):

Common.PrimaryColor color = client.GetColor();
于 2012-06-18T13:36:45.697 回答