C#調(diào)用WebService的方法步驟
前言
在日常工作中,如果涉及到與第三方進(jìn)行接口對接,有的會使用WebService的方式,這篇文章主要講解在.NET Framework中如何調(diào)用WebService。
創(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 方法。
到此這篇關(guān)于C#調(diào)用WebService的方法步驟的文章就介紹到這了,更多相關(guān)C#調(diào)用WebService內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C# 使用WPF 用MediaElement控件實現(xiàn)視頻循環(huán)播放
在WPF里用MediaElement控件,實現(xiàn)一個循環(huán)播放單一視頻的程序,同時可以控制視頻的播放、暫停、停止。這篇文章給大家介紹了C# 使用WPF 用MediaElement控件實現(xiàn)視頻循環(huán)播放,需要的朋友參考下吧2018-04-04
C#基于自定義事件EventArgs實現(xiàn)發(fā)布訂閱模式
這篇文章介紹了C#基于自定義事件EventArgs實現(xiàn)發(fā)布訂閱模式的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05
C#實現(xiàn)表格數(shù)據(jù)轉(zhuǎn)實體的示例代碼
在實際開發(fā)過程中,特別是接口對接之類的,對于這種需求是屢見不鮮,現(xiàn)在很多在線平臺也都提供了像json轉(zhuǎn)實體、sql轉(zhuǎn)實體等。本文將用C#實現(xiàn)這一功能,需要的可以參考一下2022-09-09
詳解如何在C#類型系統(tǒng)上實現(xiàn)一個SQL查詢引擎TypedSql
這篇文章主要為大家詳細(xì)介紹了如何在C#類型系統(tǒng)上實現(xiàn)一個SQL查詢引擎TypedSql,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-11-11

