Introduction
A client requested that for some reason he cannot add a Web Reference and wanted to specify the web service URL in web.config or hardcode it. Hence, my first article:)
Background
I did create this source more than a year ago. I used the internet to get information/ideas regarding this.
I am copying from my blog.
Using the Code
- Create a class as with "
DynamicWebService
" and new method "CallWebService
". - Create an object of this class and call method "
CallWebService
" with web service URL and other parameters:
Imports System.CodeDom
Imports System.CodeDom.Compiler
Imports System.Security.Permissions
Imports System.Web.Services.Description
Imports System.Reflection
Public Function CallWebService(ByVal webServiceAsmxUrl As String, _
ByVal serviceName As String, ByVal methodName As String, _
ByVal args() As Object) As Object
Try
Dim client As System.Net.WebClient = New System.Net.WebClient()
Dim stream As System.IO.Stream = _
client.OpenRead(webServiceAsmxUrl + "?wsdl")
Dim description As ServiceDescription = ServiceDescription.Read(stream)
Dim importer As ServiceDescriptionImporter = New ServiceDescriptionImporter()
importer.ProtocolName = "Soap12"
importer.AddServiceDescription(description, Nothing, Nothing)
importer.Style = ServiceDescriptionImportStyle.Client
importer.CodeGenerationOptions = _
System.Xml.Serialization.CodeGenerationOptions.GenerateProperties
Dim nmspace As CodeNamespace = New CodeNamespace()
Dim unit1 As CodeCompileUnit = New CodeCompileUnit()
unit1.Namespaces.Add(nmspace)
Dim warning As ServiceDescriptionImportWarnings = _
importer.Import(nmspace, unit1)
If warning = 0 Then
Dim provider1 As CodeDomProvider = _
CodeDomProvider.CreateProvider("VB")
Dim assemblyReferences() As String
assemblyReferences = New String() {"System.dll", _
"System.Web.Services.dll", "System.Web.dll", _
"System.Xml.dll", "System.Data.dll"}
Dim parms As CompilerParameters = New CompilerParameters(assemblyReferences)
parms.GenerateInMemory = True
Dim results As CompilerResults = provider1.CompileAssemblyFromDom(parms, unit1)
If results.Errors.Count > 0 Then
Dim oops As CompilerError
For Each oops In results.Errors
System.Diagnostics.Debug.WriteLine("========Compiler error============")
System.Diagnostics.Debug.WriteLine(oops.ErrorText)
Next
Throw New System.Exception("Compile Error Occurred calling webservice.")
End If
Dim wsvcClass As Object = results.CompiledAssembly.CreateInstance(serviceName)
Dim mi As MethodInfo = wsvcClass.GetType().GetMethod(methodName)
Return mi.Invoke(wsvcClass, args)
Else
Return Nothing
End If
Catch ex As Exception
Throw ex
End Try
End Function
Calling
Call this web service in a Webform or windows form or method, etc... as shown below:
Dim WebserviceUrl As String = _
"http://www.abc.com/lgl/test/webservice/v1_00/security.asmx"
Dim serviceName As String = "SecurityAndSessionManagement"
Dim methodName As String = "Session_Start"
Dim arArguments(1) As String
arArguments(0) = "abc"
arArguments(1) = "xxxx"
Dim objCallWS As New DynamicWebService
sSessionID = objCallWS.CallWebService(WebserviceUrl, serviceName, _
methodName, arArguments)
MsgBox("new SessionID: " & sSessionID)
History
- 11th September, 2009: Initial post