Search code examples
web-servicessoapvisual-studio-2013wsdlservice-reference

Visual Studio 2013 error creating service data source with valid wsdl file


I have been writing a project to use several different web services for getting shipping quotes. For the first two services, I had no problem adding them as a service data source by providing the url of the .asmx file for the service.

For the most recent service I was only given a link to a wsdl file. I used wsdl-analyzer.com to validate the file which seems to be completely valid. The WSDL fails with the below error message when I attempt to add it to the Visual Studio project as a service data source though.

There was an error downloading 'http://api.shipprimus.com/webservicesPrimus.wsdl/_vti_bin/ListData.svc/$metadata'.

The request failed with HTTP status 404: Not Found.

Metadata contains a reference that cannot be resolved: 'http://api.shipprimus.com/webservicesPrimus.wsdl'.

The content type text/plain of the response message does not match the content type of the binding (application/soap+xml; charset=utf-8). If using a custom encoder, be sure that the IsContentTypeSupported method is implemented properly. 

The first 1024 bytes of the response were:

'<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns:tns="http://api.shipprimus.com" xmlns:xsd1="http://api.shipprimus.com" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/"  targetNamespace="http://api.shipprimus.com">
<!-- TYPES -->
<wsdl:types>
    <xsd:schema targetNamespace="http://api.shipprimus.com">
        <!-- GetRatesRequest -->
        <xsd:element name="GetRatesRequest">
            <xsd:complexType>
                <xsd:sequence>     
                    <xsd:element name="Username" type="xsd:string"/>
                    <xsd:element name="Password" type="xsd:string"/>
                    <xsd:element name="Carrier" minOccurs="0" type="xsd:string"/>
                    <xsd:element name="OriginZipCode" type="xsd:string"/>
                    <xsd:element '.

If the service is defined in the current solution, try building the solution and adding the service reference again.

The first step I tried was to contact the company offering the service and ask if they could help, but they do not use Visual Studio, so they were of no help trying to add a Visual Studio service.

I then began to look around to see if there were other ways to possibly add the service from what I was given. I found this question, which is similar, but not exactly the same. There are several other questions regarding WSDL files and adding services to Visual Studio, but none of them have helped me.

When I try adding the service using a local path to the downloaded WSDL file, as is suggested in the answer receiving the bounty for the question above, it still does not work albeit with a different error.

The document at the url file:///C:/Users/isellar/Downloads/webservicesPrimus.wsdl was not recognized as a known document type.
The error message from each known type may help you fix the problem:

- Report from 'WSDL Document' is 'There is an error in XML document (364, 19).'.
- The element was not expected in this context:
<xsd:anotation xmlns:xsd='http://www.w3.org/2001/XMLSchema'>..</xsd:anotation>.
Expected elements: http://www.w3.org/2001/XMLSchema:annotation.
If the service is defined in the current solution, try building the solution and adding the service reference again.

It references what I assume to be a line number although I am not particularly familiar with the output of 'WSDL Document' whatever that is. Neither line 364 nor line 19 of the file has an <xsd:anotation> element though.

I also tried running the file through WSDL.exe as another answer from the above question suggests. With that I got a similar error to the one I received from Visual Studio. I assume Visual Studio just uses the WSDL.exe to process WSDL files since the outputs were almost identical and WSDL.exe is found under Visual Studio in Program Files.

When I attempt to add the service as a Web Reference I get the following:

Primus Service as Web Reference Error

As you can see it correctly grabs the method signatures from the WSDL but claims to have failed with a 404 and not found anything for a different metadata file.

I decided to see if the service was even active using SoapUI to connect to the service and it worked without any problems.

I am hoping that there is an easy way to fix the WSDL file so Visual Studio can simply import the service to my project as I have done with the other services.


Important Edit

I have gotten the WSDL to import by changing the line in the Definitions element from what it is above to xmlns:xsd="http://www.w3.org/2001/XMLSchema:annotation" by simply adding ":annotation" to the end.

This caused the WSDL to import, but with no generated code in the Reference.cs, I feel so very close to solving this, but yet so far away. I am still hoping that there is a simple change where I can get the WSDL to create a service reference correctly and the Reference code to generate for me as I spent a while already trying to write the code myself in the file without realizing that it would all be erased when Visual Studio tried to (incorrectly) regenerate the code.


Attempting to run modified WSDL through SVCUtil.exe

I attempted first to run the WSDL through svcutil per Leandros answer since I did not have access to an SVC file for the service. The first run received the same error message as the second code box above. I tried also to run with the modified WSDL that initially imported, but did not generate code from just above. This gave me the below error.

Error: Cannot import wsdl:portType
Detail: An exception was thrown while running a WSDL import extension: System.ServiceModel.Description.DataContractSerializerMessageContractImporter
Error: Schema with target namespace 'http://api.shipprimus.com' could not be found.
XPath to Error Source: //wsdl:definitions[@targetNamespace='http://api.shipprimus.com']/wsdl:portType[@name='WebservicePrimusServicePort']


Error: Cannot import wsdl:binding
Detail: There was an error importing a wsdl:portType that the wsdl:binding is dependent on.
XPath to wsdl:portType: //wsdl:definitions[@targetNamespace='http://api.shipprimus.com']/wsdl:portType[@name='WebservicePrimusServicePort']
XPath to Error Source: //wsdl:definitions[@targetNamespace='http://api.shipprimus.com']/wsdl:binding[@name='WebservicePrimusSoapBinding']


Error: Cannot import wsdl:port
Detail: There was an error importing a wsdl:binding that the wsdl:port is dependent on. XPath to wsdl:binding: //wsdl:definitions[@targetNamespace='http://api.shipprimus.com']/wsdl:binding[@name='WebservicePrimusSoapBinding']
XPath to Error Source: //wsdl:definitions[@targetNamespace='http://api.shipprimus.com']/wsdl:service[@name='WebservicePrimus']/wsdl:port[@name='WebservicePrimusServicePort']


Generating files...
Warning: No code was generated.
If you were trying to generate a client, this could be because the metadata documents did not contain any valid contracts or services or because all contracts/services were discovered to exist in /reference assemblies. Verify that you passed all the meta data documents to the tool.

Warning: If you would like to generate data contracts from schemas make sure to use the /dataContractOnly option.

Solution

  • This is fairly trivial, the .wsdl file on the web IS invalid, as it contains some illegal elements, ie a typo on <xsd:anotation>. Thus the xsd schema defining the types is being rejected.

    To fix

    1. Download the webservicesPrimus.wsdl file locally as specified in the prior solution
    2. Do a global search and replace of xsd:anotation with xsd:annotation
    3. Add that updated file as a service reference, you'll get a fully populated References.cs
    4. As a courtesy contact the owner of the webService and point out the error.