views:

1466

answers:

1

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.

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();
     }
 }

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

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.

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

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

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

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.

+1  A: 

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.

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.

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.

Sixto Saez