WinFrm應用程式呼叫WebService服務
關于WebService的創建、發布與部署等相關操作不再贅述,傳送門如下:C# VS2019 WebService創建與發布,并部署到Windows Server 2012R
此篇記錄一下客戶端的呼叫,以便后續學習使用,不足之處請指出,
建立WinFrm應用程式
- 搭建前臺界面,如下圖 ;

- 添加服務參考(專案->添加服務參考->高級->添加Web參考->...,如圖);


- 創建公共類Global.cs,代碼如下;
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace WinFrmWebClient 7 { 8 class Global 9 { 10 // 實體化一個Web服務類 11 public static LocalHost.WebServiceOracleTest myWebService = new LocalHost.WebServiceOracleTest(); 12 } 13 }View Code
- 核心代碼,
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WinFrmWebClient { public partial class FrmMain : Form { public FrmMain() { InitializeComponent(); } private void FrmMain_Load(object sender, EventArgs e) { this.listBoxLogs.Items.Clear(); } /// <summary> /// 檢查資料庫連接是否正常 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnCheckConnect_Click(object sender, EventArgs e) { try { bool b = Global.myWebService.CheckOraConnect(); if (b) { this.listBoxLogs.Items.Add("Oracle 資料庫連接正常!"); } else { this.listBoxLogs.Items.Add("Oracle 資料庫連接失敗!"); } } catch (Exception ex) { this.listBoxLogs.Items.Add("呼叫WebService失敗,錯誤資訊[" + ex.Message + "]"); } } /// <summary> /// Say Hello /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSayHello_Click(object sender, EventArgs e) { try { this.listBoxLogs.Items.Add(Global.myWebService.HelloWorld()); } catch (Exception ex) { this.listBoxLogs.Items.Add("呼叫WebService失敗,錯誤資訊[" + ex.Message + "]"); } } /// <summary> /// 顯示當前時間對應的周別 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnShowWeek_Click(object sender, EventArgs e) { try { this.listBoxLogs.Items.Add("今天是2019年,第[" + Global.myWebService.GetWeek(DateTime.Now.ToString()) + "]周,請知悉!"); } catch (Exception ex) { this.listBoxLogs.Items.Add("呼叫WebService失敗,錯誤資訊[" + ex.Message + "]"); } } } }View Code
實作效果

作者:Jeremy.Wu
出處:https://www.cnblogs.com/jeremywucnblog/
本文著作權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段宣告,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利,
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/96165.html
標籤:C#
上一篇:C# copy source directory files with original folder to the destination path
