原帖:http://bbs.csdn.net/topics/300075078 以下收藏,以備查閱
好像很多人做WebService的時候都是直接添加引用的方式,然後調用服務端的方法.這樣就個問題,就是每次我服務端添加了方法或者修改了方法後都要更新Web引用,這樣比較麻煩.下面給一個不用添加引用的方式調用服務端的方法.只是一個簡單的測試,不是很規範,用得著的人可以自己封裝一下,然後直接傳服務端的方法名進去,Type.GetMethod擷取方法,然後method.Invoke返回結果
高手些多多包函,主要是給用得著的人蔘考一下,互相學習.代碼主要是用了 System.Web.Services.Description裡的東西
建立一個WebService項目,添加以下代碼:
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Services;namespace TestWebService{ /// <summary> /// Service1 的摘要說明 /// </summary> [WebService(Namespace = "http://tempuri.org/",Description="我的Web服務")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // 若要允許使用 ASP.NET AJAX 從指令碼中調用此 Web 服務,請取消對下行的注釋。 // [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; } }}
下面是用戶端:
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();//建立用戶端代理代理類。 importer.ProtocolName = "Soap"; //指定訪問協議。 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是服務端的方法名稱,你想調用服務端的什麼方法都可以在這裡改,最好封裝一下 String[] item = (String[])method.Invoke(o, null); //註:method.Invoke(o, null)返回的是一個Object,如果你服務端返回的是DataSet,這裡也是用(DataSet)method.Invoke(o, null)轉一下就行了 foreach (string str in item) Console.WriteLine(str); //上面是根據WebService地址,模似產生一個代理類,如果你想看看產生的程式碼檔案是什麼樣子,可以用以下代碼儲存下來,預設是儲存在bin目錄下面 TextWriter writer = File.CreateText("MyTest.cs"); provider.GenerateCodeFromCompileUnit(unit, writer, null); writer.Flush(); writer.Close(); } }}