SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software
ASP.NET, VB.NET, Microsoft .NET Framework, Microsoft Visual Studio, Windows Forms, Controls and more Tutorials and Articles for Programmers


Consume Service Reference in Visual Studio 2015

This Visual Studio tutorial shows how to add service reference for a web service and how to consume web service as service reference using VB.NET as shown in sample Windows Forms application project. Web services are very common and Microsoft .NET developers frequently require to consume web services. With the latest improvements, consuming a web service WSDL as service reference is more common.

In this VB.NET tutorial, programmers can see how to consume a web service in a Visual Studio project which is added as a service reference and how to code for calling service reference. Finally with sample web service, programmer will be able to see a sample code for parsing response in XML format for the desired values.

Sample scenario:
Assume that you are a VB.NET developer who wants to consume web service country.asmx which is provided free by WebserviceX.NET
In our VB.NET tutorial, we want to consume Country web service as a Service Reference instead of consuming as Web Service
I'll also share sample codes and sample project showing how to consume web service as a Web Reference in an other Visual Studio tutorial.

For whom wondering the difference between web service vs service reference, please refer to explanation provided at by Vjeko

Let's start by checking sample web service and web service methods provided with country.asmx

sample web service and its methods
Sample web service and methods provided to consume in Visual Studio

Let's click on GetCountryByCountryCode method which returns the country name for given country code.
We can test the web service method from URL GetCountryByCountryCode using TR as input country code parameter.
Finally press Invoke button to call web service using web browser for test purposes.

testing GetCountryByCountryCode web service method
Test GetCountryByCountryCode web service method

The response of the web service is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<string xmlns="http://www.webserviceX.NET"><NewDataSet> <Table> <countrycode>tr</countrycode> <name>Turkey</name> </Table> <Table> <countrycode>tr</countrycode> <name>Turkey</name> </Table> </NewDataSet></string>
Code

The inner text of the name node of web service response is showing the country name requested from the service.

Let's consume web service tested above using a Visual Studio project as service reference

Launch Visual Studio 2015 IDE
Using menu File > New > Project... create a new project
I chosed Windows Forms Application template for sample Visual Studio 2015 project to consume web service as service reference for this tutorial.

Visual Studio 2015 Windows Forms Application project
Visual Studio 2015 Windows Forms Application project

I used the VS2015 project and solution name as ConsumeServiceReference.
Press OK to complete project creation from installed templates

I arranged the form1.vb as seen in below screenshot.
A textbox for input country code parameter for the service reference.
A button to launch call to service reference.
An other textbox to display service reference output for country name information.

Visual Studio project form1.vb in design mode
Form1.vb in design mode

Now .NET developers can add the web service as service reference to their Visual Studio 2015 project.
On Solution Explorer windows in Visual Studio IDE, right click on project name.
On context menu follow options "Add > Service Reference..." as seen in below screenshot.

add service reference in Visual Studio 2015
Add Service Reference in Visual Studio 2015

On Add Service Reference dialog screen, type the WSDL address for the web service (http://www.webservicex.net/country.asmx?WSDL) as follows then click "Go" button

Developers can also change the default namespace from ServiceReference1 to a more meaningful name like "webservicex" for example then click "OK" button

add Service Reference in Visual Studio 2015
Add Service Reference in Visual Studio 2015

Now our new service reference is listed under the "Service References" node in Solution Explorer window

Let's type some code in VB.NET for our sample Visual Studio project now.

Double click on button in desing mode and switch to code view

In button click event handler, between TRY and CATCH block type following codes:

Imports System.Xml

Public Class Form1
 Private Sub btnCallServiceReference_Click(sender As Object, e As EventArgs) Handles btnCallServiceReference.Click

  Try
   Dim client As New webservicex.countrySoapClient
   Dim xmlDoc As New XmlDocument
   xmlDoc.LoadXml(client.GetCountryByCountryCode(txtCountryCode.Text))
   Dim elemList As XmlNodeList = xmlDoc.GetElementsByTagName("name")
   txtCountryName.Text = elemList(0).InnerXml
  Catch ex As Exception
   Console.Write(ex.ToString)
  End Try

 End Sub
End Class
Code

A few notes on above code:

First of all, since the service reference call return a response in XML format, to process the XML message in VB.NET code programmers require to import System.XML namespace

Secondly, during debugging of above code block , developers can experience some possible errors

For example;
An endpoint configuration section for contract 'webservicex.countrySoap' could not be loaded because more than one endpoint configuration for that contract was found. Please indicate the preferred endpoint configuration section by name.

In case you experience error "more than one endpoint configuration for that contract was found", check App.config configuration for service reference.
Comment out one of the endpoint definitions.

Visual Studio App.config modification for endpoint configuration
App.config for endpoint configuration

An other possible error is related with proxy settings for reaching out internet addresses from your company network
The remote server returned an unexpected response: (407) Proxy Authorization Required.

If you experience Proxy Authorization Required error, add following XML configuration commands in app.config file

<system.net>
 <defaultProxy useDefaultCredentials="true" />
</system.net>
Code

Visual Studio App.config update for defaultProxy
Visual Studio App.config update for defaultProxy

Please note that according to the XML format of the response for web service reference you are using, programmers should change the XmlDocument parsing code blocks for their requirements.
For the case in this tutorial, I get the list of nodes according to tag name which has the node name as "name"
Then I read the inner xml or text of the first node for country name

Here is the screenshot of the final VB.NET Visual Studio 2015 project for sample input TR as country code and the Turkey as output from Service Reference.

Visual Studio sample project to consume service reference
consume web service as Service Reference in Visual Studio project

Visual Studio developers can download sample project from ConsumeServiceReference.rar



Visual Studio


Copyright © 2004 - 2021 Eralper YILMAZ. All rights reserved.