Visual Studio Web Reference without Web Service (or direct from WSDL file)

This may be documented elsewhere, but I had a hard time finding it so I’m going to write a quick blog entry about it for my reference.

I needed to build a Visual Studio 2013 project that had a connection to a Web Service. However the the code would only have access to the web service once deployed. In essence I needed to tell Visual Studio how to access the web service without actually having access to the web service.

The solution was actually quite simple, though not exactly intuitive. First get the WSDL file for the web service to be referenced. This usually involves appending the ?wsdl variable to the web service URI.

Example:

http://www.foo.com/foo.asmx?wsdl

In my case I used Internet Explorer from a computer that could access the web service (i.e. behind the firewall) and copied the WSDL content to a local file.

Once you have a local copy of the web service WSDL file; adding the Web Reference to Visual Studio is pretty straightforward.

  1. Add Web Reference
  2. In the URL box provided enter the path to the LOCAL WSDL file:
    file://C:\Path\To\Local\File\webservice.txt

This should load everything Visual Studio needs to know how to call the web service. This won’t really help you test the code, but it will let you get a clean build.

A simple code example using VB.NET LAMDA’s to create an inline BackgroundWorker object. I’m not a VB guru so there may be better ways to do this.
MY REQUIREMENTS:

  1. Inline (all started from a single CLICK event)
  2. Async HTTPWebRequest
  3. Populate ComboBox/Input with JSON formatted request results
  4. VB.NET
Dim requestWorker As BackgroundWorker = New BackgroundWorker()

AddHandler requestWorker.DoWork, Sub(workerSender As Object, workerEvent As System.ComponentModel.DoWorkEventArgs)

Dim request As HttpWebRequest = CType(WebRequest.Create("http://www.contoso.com/somepage.html"), HttpWebRequest)

request.Method = "POST" ' Also "GET" allowed

' For POST operations request, not required for GET

Dim data As String = "var1=1&var2=2&var3=test"

request.ContentType = "application/x-www-form-urlencoded"

request.ContentLength = System.Text.Encoding.ASCII.GetBytes(data).Length

Dim requestStreamWriter As StreamWriter = New StreamWriter(request.GetRequestStream())

requestStreamWriter.Write(data)

requestStreamWriter.Flush()

requestStreamWriter.Close()

' End POST operation requirements

Dim response As String = New StreamReader(resourcesRequest.GetResponse().GetResponseStream(), System.Text.Encoding.UTF8).ReadToEnd()

Dim results As Dictionary(Of String, Object) = New Script.Serialization.JavaScriptSerializer().Deserialize(Of Dictionary(Of String, Object))(response)

' Do something with the results 
If results.ContainsKey("Response") Then

' Allows cross thread input manipulation
Me.Invoke(Sub(Items As Dictionary(Of String, Object))

Me.cmbMyList.Items.Clear()

For Each Item In results.Item("Response")

Me.cmbResource.Items.Add(Item.value)

Next

End Sub, results)

End If

End Sub

requestWorker.RunWorkerAsync()

about me

An information technology professional with twenty five years experience in systems administration, computer programming, requirements gathering, customer service, and technical support.