在Visual Studio中的XML命名空间问题产生的服务引用问题、空间、Visual、Studio

2023-09-06 14:54:06 作者:风吹内裤丶蛋蛋凉

我连接到由第三方提供商托管的Web服务。我已经在我的项目给Web服务增加了一个服务引用,VS已经产生的所有引用和类需要的。

I'm connecting to a web service hosted by a third-party provider. I've added a service reference in my project to the web service, VS has generated all the references and classes needed.

我连接着这片code(客户端名称和方法匿名):

I'm connecting with this piece of code (client name and methods anonymized):

 using (var client = new Client())
 {
     try
     {
         client.Open();
         var response = client.Method(...);
         return response.Status;
     }
     catch (SoapException ex)
     {
         throw CreateServiceException(ex);
     }
     finally
     {
          client.Close();
     }
 }

当到达client.Open(),我得到一个异常此消息:

When reaching the client.Open(), I get an exception with this message:

从上面的XML元素_return   命名空间引用不同的类型   可选System.Boolean和   Service.Status。   使用XML属性来指定另一个   XML名称或命名空间为元素   或类型。

The top XML element '_return' from namespace '' references distinct types System.Boolean and Service.Status. Use XML attributes to specify another XML name or namespace for the element or types.

在reference.cs,我可以看到_return变量装饰有

In reference.cs, I can see that the "_return" variable is decorated with

[System.ServiceModel.MessageBodyMemberAttribute(Namespace="", Order=0)]

有没有使用WSDL,生成的服务引用,或在我的code的一个问题?

Is there a problem with the wsdl, the generated service reference or in my code?

更新:生成服务作为一个老同学的Web服务解决了这个问题。我已经标记为现在接受西斯托的答案,但我还是好奇什么可能已经造成了问题,如果任何参数的服务发电机可以解决原来的问题。

Update: Generating the service as an old school Web Service solves the problem. I've marked Sixto's answer as accepted for now, but I'm still curious what could've caused the problem and if any parameters to the service generator could solve the original problem.

推荐答案

。唯一的例外消息说你有空间/类型的歧义问题_return。生成的code可能是使用它在某些方面是一个布尔值,而在另一个方面为Service.Status类型。

If you were able to create a service reference then the WSDL is valid. The exception message is saying you have namespace/type ambiguity problem with _return. The generated code is probably using it in some context as a boolean and in another context as a Service.Status type.

我调用服务的方法,因为我从来没有见过它的必要性才不叫ClientBase.Open方法。我也始终调用关闭和放大器;中止方法适当。 Open方法基本上只是改变了客户端的状态不再是可配置的。我不知道如何将触发code在生成的类,因为它是一种遗传性的方法。我会尝试只是删除该行,看看你是否得到相同的异常。否则,如果您还没有这样做,搜索生成的code所有_return使用的地方,看看你是否可以手动整理出相应的类型。你可以为每个上下文需要不同的名字。

I don’t call the ClientBase.Open method before invoking a service method because I’ve never seen the need for it. I do always call the Close & Abort methods as appropriate. The Open method basically just changes the state of the client to no longer be configurable. I’m not sure how that would trigger code in the generated class since it is an inherited method. I’d try just removing that line and see if you get the same exception. Otherwise, if you haven’t already done so, search the generated code for all the places _return is used and see if you can manually sort-out the appropriate type. You may need different names for each context.

另一种方式来解决该WSDL是创建一个Web引用(假设它是一个基于HTTP的服务),看看是否产生code按预期工作。如果它的工作,去与ASMX客户,除非你有一个需要WCF代理功能。

Another way to troubleshoot the WSDL is to create a Web Reference (assuming it’s an HTTP based service) and see if the generate code works as expected. If it does work, go with the ASMX client unless you have a need for WCF proxy capabilities.