C#調用WebService的方法步驟
前言
在日常工作中,如果涉及到與第三方進行接口對接,有的會使用WebService的方式,這篇文章主要講解在.NET Framework中如何調用WebService。
創(chuàng)建WebService
(1)新建項目——模板選擇ASP.NET Web 應用程序
(2)選擇空項目模板
(3)右擊項目-添加-Web服務(ASMX)
(4)新建后會自動生成一個測試服務HelloWorld并返回執(zhí)行字符串
(5)點擊運行,并調用返回
方法一:靜態(tài)引用
這種方式是通過添加靜態(tài)引用的方式調用WebService
1.首先創(chuàng)建一個Winform程序,右擊引用-添加服務引用。地址即為 運行的WebService地址,命名空間可自命名
2.設計Winform窗體,可選擇工具箱button調用,TextBox入?yún)?/p>
3.根據(jù)所需,調用WebService服務即可拿到返回參數(shù)
方法二:動態(tài)調用
上面使用靜態(tài)引用的方式調用WebService,但是這種方式有一個缺點:如果發(fā)布的WebService地址改變,那么就要重新添加WebService的引用。如果是現(xiàn)有的WebService發(fā)生了改變,也要更新現(xiàn)有的服務引用,這需要把代碼放到現(xiàn)場才可以。
使用動態(tài)調用WebService的方法可以解決該問題。
1.我們在配置文件里面添加配置,把WebService的地址、WebService提供的類名、要調用的方法名稱,都寫在配置文件里面
2.同樣設計Winform界面,添加按鈕,調用WebService服務
添加幫助類
using System; using System.CodeDom.Compiler; using System.CodeDom; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using System.Xml.Serialization; using System.Web; using System.Xml.Serialization; using System.Web.Caching; using System.Web.Services.Description; namespace ApiTest1 { internal class WebServiceHelper { /// <summary> /// 生成dll文件保存到本地 /// </summary> /// <param name="url">WebService地址</param> /// <param name="className">類名</param> /// <param name="methodName">方法名</param> /// <param name="filePath">保存dll文件的路徑</param> public static void CreateWebServiceDLL(string url, string className, string methodName, string filePath) { // 1. 使用 WebClient 下載 WSDL 信息。 WebClient web = new WebClient(); Stream stream = web.OpenRead(url + "?WSDL"); // 2. 創(chuàng)建和格式化 WSDL 文檔。 ServiceDescription description = ServiceDescription.Read(stream); //如果不存在就創(chuàng)建file文件夾 if (Directory.Exists(filePath) == false) { Directory.CreateDirectory(filePath); } if (File.Exists(filePath + className + "_" + methodName + ".dll")) { //判斷緩存是否過期 var cachevalue = HttpRuntime.Cache.Get(className + "_" + methodName); if (cachevalue == null) { //緩存過期刪除dll File.Delete(filePath + className + "_" + methodName + ".dll"); } else { // 如果緩存沒有過期直接返回 return; } } // 3. 創(chuàng)建客戶端代理代理類。 ServiceDescriptionImporter importer = new ServiceDescriptionImporter(); // 指定訪問協(xié)議。 importer.ProtocolName = "Soap"; // 生成客戶端代理。 importer.Style = ServiceDescriptionImportStyle.Client; importer.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties | CodeGenerationOptions.GenerateNewAsync; // 添加 WSDL 文檔。 importer.AddServiceDescription(description, null, null); // 4. 使用 CodeDom 編譯客戶端代理類。 // 為代理類添加命名空間,缺省為全局空間。 CodeNamespace nmspace = new CodeNamespace(); 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 = filePath + className + "_" + methodName + ".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"); // 生成dll文件,并會把WebService信息寫入到dll里面 CompilerResults result = provider.CompileAssemblyFromDom(parameter, unit); if (result.Errors.HasErrors) { // 顯示編譯錯誤信息 System.Text.StringBuilder sb = new StringBuilder(); foreach (CompilerError ce in result.Errors) { sb.Append(ce.ToString()); sb.Append(System.Environment.NewLine); } throw new Exception(sb.ToString()); } //記錄緩存 var objCache = HttpRuntime.Cache; // 緩存信息寫入dll文件 objCache.Insert(className + "_" + methodName, "1", null, DateTime.Now.AddMinutes(5), TimeSpan.Zero, CacheItemPriority.High, null); } /// <summary> /// 根據(jù)WebService的url地址獲取className /// </summary> /// <param name="wsUrl">WebService的url地址</param> /// <returns></returns> //private string GetWsClassName(string wsUrl) //{ // string[] parts = wsUrl.Split('/'); // string[] pps = parts[parts.Length - 1].Split('.'); // return pps[0]; //} } }
3.動態(tài)調用WebService代碼:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Configuration; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace ApiTest1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { // 讀取配置文件,獲取配置信息 string url = ConfigurationManager.AppSettings["WebServiceAddress"]; string className = ConfigurationManager.AppSettings["ClassName"]; string methodName = ConfigurationManager.AppSettings["MethodName"]; string filePath = ConfigurationManager.AppSettings["FilePath"]; // 調用WebServiceHelper WebServiceHelper.CreateWebServiceDLL(url, className, methodName, filePath); // 讀取dll內容 byte[] filedata = File.ReadAllBytes(filePath + className + "_" + methodName + ".dll"); // 加載程序集信息 Assembly asm = Assembly.Load(filedata); Type t = asm.GetType(className); // 創(chuàng)建實例 object o = Activator.CreateInstance(t); MethodInfo method = t.GetMethod(methodName); // 參數(shù) string MsgCode = textBox1.Text; string SendXml = textBox2.Text; //string UserCode = textBox3.Text; object[] args = { MsgCode, SendXml}; //object[] args = { "動態(tài)調用WebService" }; // 調用訪問,獲取方法返回值 string value = method.Invoke(o, args).ToString(); //輸出返回值 MessageBox.Show($"返回值:{value}"); } } }
程序運行結果
如果說類名沒有提供,可以根據(jù)url來自動獲取類名:
見幫助類(WebServiceHelper)中GetWsClassName 方法。
到此這篇關于C#調用WebService的方法步驟的文章就介紹到這了,更多相關C#調用WebService內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
c#編寫的高并發(fā)數(shù)據(jù)庫控制訪問代碼
往往大數(shù)據(jù)量,高并發(fā)時, 瓶頸都在數(shù)據(jù)庫上, 好多人都說用數(shù)據(jù)庫的復制,發(fā)布, 讀寫分離等技術, 但主從數(shù)據(jù)庫之間同步時間有延遲.2015-03-03C#調用??倒I(yè)相機SDK采集圖像并在Halcon窗口中顯示方式
這篇文章主要介紹了C#調用??倒I(yè)相機SDK采集圖像并在Halcon窗口中顯示方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02WindowsForm實現(xiàn)TextBox占位符Placeholder提示功能
這篇文章主要介紹了WindowsForm實現(xiàn)TextBox占位符Placeholder提示,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07C#判斷頁面中的多個文本框輸入值是否有重復的實現(xiàn)方法
這篇文章主要介紹了C#判斷頁面中的多個文本框輸入值是否有重復的實現(xiàn)方法,是一個非常簡單實用的技巧,需要的朋友可以參考下2014-10-10.NET WinForm實現(xiàn)在listview中添加progressbar的方法
這篇文章主要介紹了.NET WinForm實現(xiàn)在listview中添加progressbar的方法,結合實例形式簡單分析了進度條控件的添加與使用方法,需要的朋友可以參考下2017-05-05