WebService引用动态配置
http://blog.csdn.net/rainylin/article/details/3556902 有时候需要动态的设置?WebService?的址,这样发布到不同的服务器时就要重新生成,为此我们需要在web.config中动态配置WebService的地址,在网上查了很多资料,其中这种方法感觉很好用也很好实现,原文VB.NET实现。本人已改为C#版 首先手动的添加一个Web引用(这个就不用说了吧) 然后修改本地的代理类添加一个新类,继承你的?WebService代理类实例: namespace?Web_Service { ??? [System.Diagnostics.DebuggerStepThrough(),System.ComponentModel.DesignerCategory("code"), ?? System.Web.Services.WebServiceBinding(Name =?"",Namespace =?"")] ????public?class?DynWebService?:?SelfWebService ??? { ????????public?DynWebService() :?base() ??????? { ????????????//设置默认webService的地址 ????????????this.Url =?"http://localhost/WebService.asmx"; ??????? } ????????public?DynWebService(string?webUrl) :?base() ????????????this.Url = webUrl; ??? } } 说明:SelfWebService?你引用的?WebService Web Service的URI部署到配置文件里 <add?key="WebServiceKey"value="http://xxxx/WebService.asmx"/> 最后实现 ????private?void?WebServiceTest() ????????string?webServiceUrl =?ConfigurationManager.AppSettings["WebServiceKey "]; ??????? Web_Service.DynWebService?dws =?new?Web_Service.DynWebService(webServiceUrl); ????????string?result = dws.HelloWorld(); } 原文内容如下:
方法一: 手动的添加一个Web引用,然后修改下本地的代理类。最后实现Web Service的URI部署到配置文件里。 具体做法如下:
以下代码是显示如何配置动态的Web Service,以服务单元C(类名为Web_SVSGC)为例:
(1)?????首先在Web引用中的本地代理类中添加一个构造函数,这个构造函数是以Web Service的URL为参数的重载方法。
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 Service的url配置在调用Web Service的应用程序的配置文件中。(其中的value可以随时修改。)
<
con
f
iguration
>
????
<
appSettings
>
?????????????
<
add
key
="
SVSGA_
U
RL
"
value
="http://192.168.108.188/?QDN/SERVICE/SVSG
A
.asmx"
?QDN/SERVICE/SVSG
A
.asmx"
/>
????
</
appSettings
>
</
con
f
iguration
>
?(3)?????调用时,根据配置文件的Url动态的生成Web Service。
????????
'
要
调
用的
Web Service
的
URL
????????
Dim?strWebSvsUrl?
As?
String
????????
'
声明一个要
调
用的
Web Service
????????
Dim?objSVSGC?
As?WebSvs_GC.?SVSGC
????????
'
调
用
Web Service
的
远程方法的
返回
值
????????
Dim?strReturnValue?
As?
String
????????
Try
????????????
'
从配置文件中取得
Web Service
的
URL
??????????? 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的代理类来,方法还是很不错的哦。缺点是因为类型定义都在内存里动态生成,所以返回值复杂的调用无法实现。
/// <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 的方式。该属性具有两项设置:
将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就可以了。
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |