动态引用Webservice

 

Imports System.Collections.Generic
Imports System.Text
Imports System.IO
Imports System.Configuration
Imports System.CodeDom
Imports System.CodeDom.Compiler
Imports System.Net
Imports System.Web.Services
Imports System.Web.Services.Description
Imports Microsoft.CSharp

	Public NotInheritable Class WebServiceHelper
		Private Sub New()
		End Sub

		''' <summary>  
		''' 动态调用WebService  
		''' </summary>  
		''' <param name="url">WebService地址</param>  
		''' <param name="methodname">方法名(模块名)</param>  
		''' <param name="args">参数列表</param>  
		''' <returns>object</returns>  
		Public Shared Function InvokeWebService(url As String, methodname As String, args As Object()) As Object
			Return InvokeWebService(url, Nothing, methodname, args)
		End Function

		''' <summary>  
		''' 动态调用WebService  
		''' </summary>  
		''' <param name="url">WebService地址</param>  
		''' <param name="classname">类名</param>  
		''' <param name="methodname">方法名(模块名)</param>  
		''' <param name="args">参数列表</param>  
		''' <returns>object</returns>  
		Public Shared Function InvokeWebService(url As String, classname As String, methodname As String, args As Object()) As Object

			Dim [namespace] As String = "ServiceBase.WebService.DynamicWebLoad"

			If classname Is Nothing OrElse classname = "" Then

				classname = WebServiceHelper.GetClassName(url)
			End If

			'获取服务描述语言(WSDL)  
			Dim wc As New WebClient()
			Dim stream As Stream = wc.OpenRead(url & "?WSDL")
			Dim sd As ServiceDescription = ServiceDescription.Read(stream)
			Dim sdi As New ServiceDescriptionImporter()
			sdi.AddServiceDescription(sd, "", "")
			Dim cn As New CodeNamespace([namespace])
			'生成客户端代理类代码  
			Dim ccu As New CodeCompileUnit()
			ccu.Namespaces.Add(cn)
			sdi.Import(cn, ccu)
			Dim csc As New CSharpCodeProvider()
			Dim icc As ICodeCompiler = csc.CreateCompiler()
			'设定编译器的参数  
			Dim cplist As New CompilerParameters()
			cplist.GenerateExecutable = False
			cplist.GenerateInMemory = True
			cplist.ReferencedAssemblies.Add("System.dll")
			cplist.ReferencedAssemblies.Add("System.XML.dll")
			cplist.ReferencedAssemblies.Add("System.Web.Services.dll")
			cplist.ReferencedAssemblies.Add("System.Data.dll")

			'编译代理类  
			Dim cr As CompilerResults = icc.CompileAssemblyFromDom(cplist, ccu)
			If True = cr.Errors.HasErrors Then
				Dim sb As System.Text.StringBuilder = New StringBuilder()
				For Each ce As CompilerError In cr.Errors
					sb.Append(ce.ToString())
					sb.Append(System.Environment.NewLine)
				Next
				Throw New Exception(sb.ToString())
			End If

			'生成代理实例,并调用方法  
			Dim assembly As System.Reflection.Assembly = cr.CompiledAssembly
			Dim t As Type = assembly.[GetType]([namespace] & "." & classname, True, True)
			Dim obj As Object = Activator.CreateInstance(t)
			Dim mi As System.Reflection.MethodInfo = t.GetMethod(methodname)
			Return mi.Invoke(obj, args)
		End Function

		Private Shared Function GetClassName(url As String) As String
			Dim parts As String() = url.Split("/"C)
			Dim pps As String() = parts(parts.Length - 1).Split("."C)
			Return pps(0)
		End Function
	End Class

 

 

你可能感兴趣的:(webservice)