Find Code:
All Words
Any of the Words
Exact Phrase
Home
:
Code
:
Forums
:
Submit
:
Mailing List
:
About
:
Contact
Code
All
VB.NET
ASP.NET
C#
VB Classic
ASP Classic
Snippets
Popular
Resources
Submit Code
Forums
Articles
Tips
Links
Books
Contest
Link to us
Execute Web Service
Author:
Michael Clark
E-mail:
Click to e-mail author
Website:
http://www.salcentral.com
Submitted:
12/10/2004
Version:
ASP.NET 1.0
Compatibility:
ASP.NET 1.0
Category:
ASP.NET
Views:
21434
This vb.net script allows you to run a web service programmatically without having to use 'Add Web Reference' you simply instantiate a class and tell it what method you want to run along with passing it the required arguments. Great for generic web service calling.
Declarations:
'none
Code:
'WWW.SALCENTRAL.COM 'Web Services search engine and aggregator of search information. '==================================================================== 'COPYRIGHT 'This code is copyright © 2004 of SalCentral. 'All Rights Reserved. This code must not be altered in any way and this 'copyright notice must not be removed. You can however use this code for 'unlimited non-commercial use. '==================================================================== 'SUPPORT 'Email support@salcentral.com for technical support on this product. 'To receive an email about new releases and enhancements then register 'your email at mailto:support@salcentral.com?subject=Register '==================================================================== Imports System.Web.Services.Description Imports System.Net Imports System.Reflection Imports System.CodeDom.Compiler Imports System.CodeDom Imports System.IO Public Class WebService Private Const DEFAULT_TIMEOUT As Integer = 30000 Dim m_iTimeOut As Integer Dim m_oAssemblyType As System.Type Dim m_oWebProxy As System.Net.WebProxy Dim m_oNetworkCredential As System.Net.NetworkCredential Dim m_sWsdlLocation As String Dim m_sClassName As String Public Function Methods() As System.Reflection.MemberInfo() Return m_oAssemblyType.GetMembers(BindingFlags.Public) End Function Public Function Invoke(ByVal Method As SAL.Common.Method) As Object If IsNothing(m_oAssemblyType) Then Dim oSD As ServiceDescription = GetServiceDescription(m_sWsdlLocation) m_oAssemblyType = GenerateAssembly(m_sClassName, oSD) End If Dim oInstance As Object = Activator.CreateInstance(m_oAssemblyType) If Not IsNothing(WebProxy) Then oInstance.Proxy = WebProxy If Not IsNothing(NetworkCredential) Then oInstance.Credentials = NetworkCredential Dim oMethod As System.Reflection.MethodInfo = m_oAssemblyType.GetMethod(Method.MethodName) If IsNothing(oMethod) Then Throw New Exception("No such method found") Dim oParameters() As System.Reflection.ParameterInfo = oMethod.GetParameters() Return m_oAssemblyType.InvokeMember(Method.MethodName, BindingFlags.Public + BindingFlags.InvokeMethod + BindingFlags.Instance + BindingFlags.Static, Nothing, oInstance, GetArguments(oParameters, Method.Arguments)) End Function Public Sub New(ByVal WsdlLocation As String, ByVal ClassName As String) m_iTimeOut = DEFAULT_TIMEOUT m_sWsdlLocation = WsdlLocation m_sClassName = ClassName End Sub #Region "Public Properties" Public Property WebProxy() As System.Net.WebProxy Get Return m_oWebProxy End Get Set(ByVal Value As System.Net.WebProxy) m_oWebProxy = Value End Set End Property Public Property NetworkCredential() As System.Net.NetworkCredential Get Return m_oNetworkCredential End Get Set(ByVal Value As System.Net.NetworkCredential) m_oNetworkCredential = Value End Set End Property Public Property Timeout() As Integer Get Return m_iTimeOut End Get Set(ByVal Value As Integer) m_iTimeOut = Value End Set End Property #End Region #Region "Preparation" Private Function GetArguments(ByVal poParameters() As System.Reflection.ParameterInfo, ByVal poArguments As Hashtable) As Object() Dim iIndex As Integer = (poParameters.Length - 1) Dim oArguments(iIndex) As Object Dim i As Integer For i = 0 To iIndex oArguments(i) = poArguments.Item(poParameters(i).Name) Next Return oArguments End Function Private Function GetURLStream(ByVal sURL As String) As Stream Dim oWebRequest As WebRequest oWebRequest = WebRequest.Create(sURL) oWebRequest.Timeout = Timeout If Not IsNothing(WebProxy) Then oWebRequest.Proxy = WebProxy If Not IsNothing(NetworkCredential) Then oWebRequest.Credentials = NetworkCredential Return oWebRequest.GetResponse.GetResponseStream End Function #End Region #Region "Generation" Private Function GetServiceDescription(ByVal psWsdlLocation As String) As ServiceDescription Return ServiceDescription.Read(GetURLStream(psWsdlLocation)) End Function Private Function GenerateAssembly(ByVal psClassName As String, ByVal poServiceDescription As ServiceDescription) As Type Dim oSDImp As New ServiceDescriptionImporter oSDImp.ProtocolName = "soap" oSDImp.AddServiceDescription(poServiceDescription, Nothing, "") Dim oCodeNameSpace As CodeNamespace = New CodeNamespace Dim oCodeCompUnit As New CodeCompileUnit Dim oWarning As ServiceDescriptionImportWarnings = oSDImp.Import(oCodeNameSpace, oCodeCompUnit) Dim oCodeProv As New VBCodeProvider Dim oCodeGen As ICodeGenerator = oCodeProv.CreateGenerator Dim oCodeComp As ICodeCompiler = oCodeProv.CreateCompiler Dim oTextWriter As System.IO.StringWriter = New System.IO.StringWriter oCodeGen.GenerateCodeFromNamespace(oCodeNameSpace, oTextWriter, Nothing) Dim sSource As String = oTextWriter.ToString Dim oCompParam As CompilerParameters = New CompilerParameters(New String() {"System.dll", "System.Xml.dll", "System.Web.Services.dll", "System.Data.dll"}) With oCompParam .GenerateExecutable = False .GenerateInMemory = True .MainClass = System.Guid.NewGuid.ToString .IncludeDebugInformation = False .TreatWarningsAsErrors = False .WarningLevel = 4 End With Dim oCompilerResults As CompilerResults = oCodeComp.CompileAssemblyFromSource(oCompParam, sSource) Dim oAssembly As [Assembly] = oCompilerResults.CompiledAssembly Dim oDynamicType As Type = oAssembly.GetType(psClassName) Return oDynamicType End Function #End Region End Class Public Class Method Dim m_oArguments As New System.Collections.Hashtable Dim m_sMethodName As String Public ReadOnly Property MethodName() As String Get Return m_sMethodName End Get End Property Public Property Arguments() As Hashtable Get Arguments = m_oArguments End Get Set(ByVal Value As Hashtable) m_oArguments = Value End Set End Property Public Sub New(ByVal MethodName As String) m_oArguments.Clear() m_sMethodName = MethodName End Sub End Class
Home
|
Forums
|
Submit
|
Books
|
Mailing List
|
Advertising
|
About
|
Contact
© 2024 A1VBCode. All rights reserved.
Legal disclaimer & terms of use
Privacy statement