小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

【分享】動態(tài)調(diào)用WebService方法【散分】

 thy 2009-02-24
好像很多人做WebService的時候都是直接添加引用的方式,然后調(diào)用服務(wù)端的方法.這樣就個問題,就是每次我服務(wù)端添加了方法或者修改了方法后都要更新Web引用,這樣比較麻煩.下面給一個不用添加引用的方式調(diào)用服務(wù)端的方法.只是一個簡單的測試,不是很規(guī)范,用得著的人可以自己封裝一下,然后直接傳服務(wù)端的方法名進(jìn)去,Type.GetMethod獲取方法,然后method.Invoke返回結(jié)果
高手些多多包函,主要是給用得著的人參考一下,互相學(xué)習(xí).代碼主要是用了 System.Web.Services.Description里的東西

新建一個WebService項目,添加以下代碼:

C# code
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; namespace TestWebService { /// <summary> /// Service1 的摘要說明 /// </summary> [WebService(Namespace = "http:///",Description="我的Web服務(wù)")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // 若要允許使用 ASP.NET AJAX 從腳本中調(diào)用此 Web 服務(wù),請取消對下行的注釋。 // [System.Web.Script.Services.ScriptService] public class TestWebService : System.Web.Services.WebService { [WebMethod] public string HelloWorld() { return "測試Hello World"; } [WebMethod] public string Test() { return "測試Test"; } [WebMethod(CacheDuration = 60,Description = "測試")] public List<String> GetPersons() { List<String> list = new List<string>(); list.Add("測試一"); list.Add("測試二"); list.Add("測試三"); return list; } } }

下面是客戶端:
C# code
using System; using System.IO; using System.Collections.Generic; using System.Linq; using System.Collections; using System.Web; using System.Net; using System.Reflection; using System.CodeDom; using System.CodeDom.Compiler; using System.Web.Services; using System.Text; using System.Web.Services.Description; using System.Web.Services.Protocols; using System.Xml.Serialization; using System.Windows.Forms; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { WebClient client = new WebClient(); String url = "http://localhost:3182/Service1.asmx?WSDL";//這個地址可以寫在Config文件里面,這里取出來就行了.在原地址后面加上: ?WSDL Stream stream = client.OpenRead(url); ServiceDescription description = ServiceDescription.Read(stream); ServiceDescriptionImporter importer = new ServiceDescriptionImporter();//創(chuàng)建客戶端代理代理類。 importer.ProtocolName = "Soap"; //指定訪問協(xié)議。 importer.Style = ServiceDescriptionImportStyle.Client; //生成客戶端代理。 importer.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties | CodeGenerationOptions.GenerateNewAsync; importer.AddServiceDescription(description, null, null); //添加WSDL文檔。 CodeNamespace nmspace = new CodeNamespace(); //命名空間 nmspace.Name = "TestWebService"; CodeCompileUnit unit = new CodeCompileUnit(); unit.Namespaces.Add(nmspace); ServiceDescriptionImportWarnings warning = importer.Import(nmspace, unit); CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp"); CompilerParameters parameter = new CompilerParameters(); parameter.GenerateExecutable = false; parameter.OutputAssembly = "MyTest.dll";//輸出程序集的名稱 parameter.ReferencedAssemblies.Add("System.dll"); parameter.ReferencedAssemblies.Add("System.XML.dll"); parameter.ReferencedAssemblies.Add("System.Web.Services.dll"); parameter.ReferencedAssemblies.Add("System.Data.dll"); CompilerResults result = provider.CompileAssemblyFromDom(parameter, unit); if (result.Errors.HasErrors) { // 顯示編譯錯誤信息 } Assembly asm = Assembly.LoadFrom("MyTest.dll");//加載前面生成的程序集 Type t = asm.GetType("TestWebService.TestWebService"); object o = Activator.CreateInstance(t); MethodInfo method = t.GetMethod("GetPersons");//GetPersons是服務(wù)端的方法名稱,你想調(diào)用服務(wù)端的什么方法都可以在這里改,最好封裝一下 String[] item = (String[])method.Invoke(o, null); //注:method.Invoke(o, null)返回的是一個Object,如果你服務(wù)端返回的是DataSet,這里也是用(DataSet)method.Invoke(o, null)轉(zhuǎn)一下就行了 foreach (string str in item) Console.WriteLine(str); //上面是根據(jù)WebService地址,模似生成一個代理類,如果你想看看生成的代碼文件是什么樣子,可以用以下代碼保存下來,默認(rèn)是保存在bin目錄下面 TextWriter writer = File.CreateText("MyTest.cs"); provider.GenerateCodeFromCompileUnit(unit, writer, null); writer.Flush(); writer.Close(); } } }

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多