C#調(diào)用WebService的實現(xiàn)方法
一、前言
在日常工作中,如果涉及到與第三方進行接口對接,有的會使用WebService的方式,這篇文章主要講解在.NET Framework中如何調(diào)用WebService。
1.創(chuàng)建WebService
(1)新建項目——模板選擇ASP.NET Web 應(yīng)用程序
(2)選擇空項目模板
(3)右擊項目-添加-Web服務(wù)(ASMX)
(4)新建后會自動生成一個測試服務(wù)HelloWorld并返回執(zhí)行字符串
(5)點擊運行,并調(diào)用返回
二、方法一:靜態(tài)引用
這種方式是通過添加靜態(tài)引用的方式調(diào)用WebService
1.首先創(chuàng)建一個Winform程序
右擊引用-添加服務(wù)引用。地址即為 運行的WebService地址,命名空間可自命名
2.設(shè)計Winform窗體
可選擇工具箱button調(diào)用,TextBox入?yún)?/p>
3.根據(jù)所需
調(diào)用WebService服務(wù)即可拿到返回參數(shù)
三、動態(tài)調(diào)用
上面使用靜態(tài)引用的方式調(diào)用WebService,但是這種方式有一個缺點:如果發(fā)布的WebService地址改變,那么就要重新添加WebService的引用。如果是現(xiàn)有的WebService發(fā)生了改變,也要更新現(xiàn)有的服務(wù)引用,這需要把代碼放到現(xiàn)場才可以。
使用動態(tài)調(diào)用WebService的方法可以解決該問題。
1.我們在配置文件里面添加配置
把WebService的地址、WebService提供的類名、要調(diào)用的方法名稱,都寫在配置文件里面
2.同樣設(shè)計Winform界面
添加按鈕,調(diào)用WebService服務(wù)。
添加幫助類
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)調(diào)用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"]; // 調(diào)用WebServiceHelper WebServiceHelper.CreateWebServiceDLL(url, className, methodName, filePath); // 讀取dll內(nèi)容 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)調(diào)用WebService" }; // 調(diào)用訪問,獲取方法返回值 string value = method.Invoke(o, args).ToString(); //輸出返回值 MessageBox.Show($"返回值:{value}"); } } }
程序運行結(jié)果
如果說類名沒有提供,可以根據(jù)url來自動獲取類名:
見幫助類(WebServiceHelper)中GetWsClassName 方法。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
C# 9 新特性——record的相關(guān)總結(jié)
這篇文章主要介紹了C# 9 新特性——record的相關(guān)總結(jié),幫助大家更好的理解和學(xué)習(xí)使用c# 9的新特性,感興趣的朋友可以了解下2021-02-02