Site Search:
Sign in | Join | Help
4Penny.net

VB.NET

Notes, Tricks and Tips on VB.NET

Code to call a web service

When I develop a VB application that calls a web service, I usually am developing the web service in the same solution. That's convenient, because you can step through and debug all the code.

But I'd also like to be able to deploy the web service and have my VB app call the live service. So...

Here's VB code that will call a web service with a dynamic URL. First, the app.config entry that makes it possible:

<appSettings>
    <add key="webservicezzz" value="<a href="http://localhost/GPConnect.asmx">http://localhost/GPConnect.asmx</a>" />
    <add key="webservice" value="<a href="http://localhost:1906/eConnect/GPConnect.asmx">http://localhost:1906/eConnect/GPConnect.asmx</a>" />
</appSettings>
<system.diagnostics>

What I do is manipulate the 'zzz's to call the dev or live service. (the one with the '1906' is the dev one... but you knew that)

Code for Visual Studio 2005:

Dim webService As localhost.GPConnect = New localhost.GPConnect


webService.Url = mstrURL


Try
    webService.myMethod()
    MsgBox("done")
Catch ex As Exception
    MsgBox(ex.Message)


End Try

It's just a little different in 2008:

Private Sub btnTestEconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTestEconnect.Click
    Dim strURL As String = System.Configuration.ConfigurationManager.AppSettings("webService")
    Dim url As New System.ServiceModel.EndpointAddress(strURL)
    
    Dim webService As New GPconnectLocal.GPConnectSoapClient
    webService.Endpoint.Address = url
    
    Try
        webService.Send("", "localhost", "")
        MsgBox("done")
    Catch ex As Exception
        MsgBox(ex.Message)
    
    End Try
End Sub

As always, I welcome and comments or feedback.

Comments

 

Jeganeedhi said:

Excellent! Steve .....It is very useful.

regards

Jeganeedhi

October 6, 2008 5:42 AM

Leave a Comment

(required)  
(optional)
(required)  
Add

About Steve Gray

Steve is a seasoned (translate: old) developer in VB and ASP.NET. He spends most of his time in Dynamics GP, writing custom mods for consulting firms. Crystal reports, eConnect, VS Tools for Dynamics... anything that comes along.