简体   繁体   English

添加Web服务参考后的参考不明确

[英]Ambigous Reference After Adding Web Service Reference

I am adding a reference to a web service. 我正在添加对Web服务的引用。 In the web service I have a class named CustomerRecord . 在Web服务中,我有一个名为CustomerRecord的类。 I have a method named GetCustomerData() which returns an array of CustomerRecord class. 我有一个名为GetCustomerData()的方法,该方法返回CustomerRecord类的数组。

The project which will consume the webservice also has CustomerRecord class.This is an exact representation of the one in Web Service. 使用Web服务的项目还具有CustomerRecord 。这是Web Service中的一个确切表示。

Now when I use the web service, there are 2 CustomerRecord classes.This leads to ambiguous reference error. 现在,当我使用Web服务时,有2个CustomerRecord类,这会导致模棱两可的引用错误。

The simple solution would be to rename one of them. 一种简单的解决方案是重命名其中之一。 But if you can't do that then you still have two options. 但是,如果您不能这样做,那么您仍然有两个选择。

Remove the using from the top of the file that references both CustomerRecord classes, and simply refer to it using the full namespace. 从引用两个CustomerRecord类的文件顶部删除using,并使用完整命名空间简单地引用它。

My.Long.Namespace.CustomerRecord customer = new My.Long.Namespace.CustomerRecord();

Or, in that same file, use using to alias one of them, just for that file. 或者,在同一个文件中,使用using别名其中一个,仅用于该文件。

using WsCustomerRecord = My.Long.Namespace.CustomerRecord;

// ...

    WsCustomerRecord customer = new WsCustomerRecord();

In the line that gives the error, the compiler cannot decide which one of these two classes it should pick. 在给出错误的行中,编译器无法决定应该选择这两个类中的哪一个。

To solve this there are thee options: 要解决这个问题,有以下选择:

  1. Use a different namespace for both classes. 为这两个类使用不同的命名空间。

  2. Delete one of the classes 删除其中一个类

  3. Use an alias 使用别名

The first option is only needed when both classes are in the same namespace and you want to keep them both. 只有当两个类位于同一名称空间中并且您希望将它们保留在一起时,才需要第一个选项。

The second option is useful when both classes are exactly the same. 当两个类完全相同时,第二个选项很有用。 You could delete the one in the client or, when adding the service reference, select the re-use existing classes option so you keep the client version. 您可以删除客户端中的那个,或者在添加服务引用时,选择重用现有类选项,以便保留客户端版本。

The last option works like this: 最后一个选项是这样的:

using ns1 = Some.CoolNamespace;
using ns2 = Another.CoolNaespace;

ns2.AClass x = new ...

If you use a web bservice, CustomerRecord class must have the same namespace as the web service. 如果使用Web服务,则CustomerRecord类必须具有与Web服务相同的命名空间。 To resolve the ambiguity right-click \\ resolve and then write explicitly the needed namespace any reference to a CustomerRecord object. 若要解决歧义,请右键单击\\ resolve,然后将对CustomerRecord对象的任何引用显式写入所需的名称空间。

Since CustomerRecord IS the same class, i suggest to delete the CustomerRecord that is in the project that consumes web service, and use only the web service's one. 由于CustomerRecord是同一个类,我建议删除使用Web服务的项目中的CustomerRecord ,并仅使用Web服务。

If you need to write particular code that is not needed by web service or is not serialized by SOAP remember that any class coming from a web method is PARTIAL, so you can write extra-code 如果您需要编写Web服务不需要的特定代码或者没有通过SOAP序列化,请记住来自Web方法的任何类都是PARTIAL,因此您可以编写额外的代码

public partial class CustomerRecord 
{
 ....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM