`
tubaluer
  • 浏览: 1447279 次
文章分类
社区版块
存档分类
最新评论
  • sblig: c / c++ 是不一样的都会输出 100
    j = j++

如何动态调用WebService?

 
阅读更多

今天在CSDN看到一个问题:如何动态调用WebService? (原贴见:http://community.csdn.net/Expert/topic/3330/3330006.xml?temp=.8463404

方法一: 手动的添加一个Web引用,然后修改下本地的代理类。最后实现Web Service的URI部署到配置文件里。 具体做法如下:

以下代码是显示如何配置动态的Web Service,以服务单元C(类名为Web_SVSGC)为例:

(1) 首先在Web引用中的本地代理类中添加一个构造函数,这个构造函数是以Web ServiceURL为参数的重载方法。

Namespace Web_SVSGC

'<remarks/>

<System.Diagnostics.DebuggerStepThroughAttribute(), _ System.ComponentModel.DesignerCategoryAttribute("code"), _ System.Web.Services.WebServiceBindingAttribute(Name:="SVSGCSoap", [Namespace]:="http://tempuri.org/QYJSERVICE/SVSGC"), _ System.Xml.Serialization.XmlIncludeAttribute(GetType(Attribute))> _

Public Class SVSGC

Inherits System.Web.Services.Protocols.SoapHttpClientProtocol

'<remarks/>

Public Sub New()

MyBase.New

Me.Url = "http://localhost/QYJSERVICE/WEBSERVICE/SERVICE/SVSGC.asmx"

End Sub

'添加一个带参数的构造函数。

Public Sub New(ByVal strUrl As String)

MyBase.New()

Me.Url = strUrl

End Sub

(2) Web Serviceurl配置在调用Web Service的应用程序的配置文件中。(其中的value可以随时修改。)

<configuration>

<appSettings>

<add key="SVSGA_URL" value="http://192.168.108.188/QDN/SERVICE/SVSGA.asmx"QDN/SERVICE/SVSGA.asmx" />

</appSettings>

</configuration>

(3) 调用时,根据配置文件的Url动态的生成Web Service

'用的Web ServiceURL

Dim strWebSvsUrl As String

'声明一个要用的Web Service

Dim objSVSGC As WebSvs_GC. SVSGC

'Web Service远程方法的返回

Dim strReturnValue As String

Try

'从配置文件中取得Web ServiceURL

strWebSvsUrl = _

System.Configuration.ConfigurationSettings.AppSettings("SVSGC_URL")

'生成一个Web Service

objSVSGC = New WebSvs_GC.SVSGC (strWebSvsUrl)

'Web Service里的程方法

strReturnValue = objSVSGC.HelloWorld()

Catch ex As Exception

End Try

方法二:利用了CodeDem动态的编译出Web Service的代理类来,方法还是很不错的哦。缺点是因为类型定义都在内存里动态生成,所以返回值复杂的调用无法实现。

<style type="text/css"> .cf { font-family: SimSun; font-size: 10pt; color: black; background: silver; border-top: windowtext 1pt solid; padding-top: 0pt; border-left: windowtext 1pt solid; padding-left: 0pt; border-right: windowtext 1pt solid; padding-right: 0pt; border-bottom: windowtext 1pt solid; padding-bottom: 0pt; } .cl { margin: 0px; } .cb1 { color: gray; } .cb2 { color: green; } .cb3 { color: blue; }</style>

/// <summary>

/// 根据指定的信息,调用远程WebService方法

/// </summary>

/// <param name="url">WebService的http形式的地址</param>

/// <param name="namespace">欲调用的WebService的命名空间</param>

/// <param name="classname">欲调用的WebService的类名(不包括命名空间前缀)</param>

/// <param name="methodname">欲调用的WebService的方法名</param>

/// <param name="args">参数列表</param>

/// <returns>WebService的执行结果</returns>

/// <remarks>

/// 如果调用失败,将会抛出Exception。请调用的时候,适当截获异常。

/// 异常信息可能会发生在两个地方:

/// 1、动态构造WebService的时候,CompileAssembly失败。

/// 2、WebService本身执行失败。

/// </remarks>

/// <example>

/// <code>

/// object obj = InvokeWebservice("http://localhost/GSP_WorkflowWebservice/common.asmx",

/// "Genersoft.Platform.Service.Workflow",

/// "Common",

/// "GetToolType",

/// new object[]{"1"});

/// </code>

/// </example>

public static object InvokeWebservice(string url, string @namespace, string classname,

string methodname, object[] args)

{

try

{

System.Net.WebClient wc = new System.Net.WebClient();

System.IO.Stream stream = wc.OpenRead(url+"?WSDL");

System.Web.Services.Description.ServiceDescription sd

= System.Web.Services.Description.ServiceDescription.Read(stream);

System.Web.Services.Description.ServiceDescriptionImporter sdi

= new System.Web.Services.Description.ServiceDescriptionImporter();

sdi.AddServiceDescription(sd,"","");

System.CodeDom.CodeNamespace cn = new System.CodeDom.CodeNamespace(@namespace);

System.CodeDom.CodeCompileUnit ccu = new System.CodeDom.CodeCompileUnit();

ccu.Namespaces.Add(cn);

sdi.Import(cn,ccu);

Microsoft.CSharp.CSharpCodeProvider csc = new Microsoft.CSharp.CSharpCodeProvider();

System.CodeDom.Compiler.ICodeCompiler icc = csc.CreateCompiler();

System.CodeDom.Compiler.CompilerParameters cplist

= new System.CodeDom.Compiler.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");

System.CodeDom.Compiler.CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);

if(true == cr.Errors.HasErrors)

{

System.Text.StringBuilder sb = new System.Text.StringBuilder();

foreach(System.CodeDom.Compiler.CompilerError ce in cr.Errors)

{

sb.Append(ce.ToString());

sb.Append(System.Environment.NewLine);

}

throw new Exception(sb.ToString());

}

System.Reflection.Assembly assembly = cr.CompiledAssembly;

Type t = assembly.GetType(@namespace+"."+classname,true,true);

object obj = Activator.CreateInstance(t);

System.Reflection.MethodInfo mi = t.GetMethod(methodname);

return mi.Invoke(obj,args);

}

catch(Exception ex)

{

throw new Exception(ex.InnerException.Message,new Exception(ex.InnerException.StackTrace));

}

}

方法三:URL Behavior 属性

项目中的每一 Web 引用都与一个代理类进行交互,该代理类用于访问 Web 服务的内容。可以使用 UrlBehavior 属性来控制代理类获取特定 Web 引用的 URL 的方式。该属性具有两项设置:

设置 效果
Static 如果已经创建代理类的实例,该代理类将使用硬编码的 URL 来设置 URL。
Dynamic 应用程序在运行时从应用程序配置文件的 <link keywords="gngrfAppSettingsElement">元素中获取 URL。

将Web引用改为Dynamic的,本地的代理会被修改:

Public Sub New()

MyBase.New

Dim urlSetting As String = System.Configuration.ConfigurationSettings.AppSettings("CallWebServiceDemo.WebServiceDemo.Service1")

If (Not (urlSetting) Is Nothing) Then

Me.Url = String.Concat(urlSetting, "")

Else

Me.Url = "http://localhost/WebAppTest/WebServiceDemo/Service1.asmx"

End If

End Sub

urlSetting 被追加了。你只要按照Key(CallWebServiceDemo.WebServiceDemo.Service1),在相应的.config里追加此Key/Value就可以了。<!--EndFragment-->

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics