C# - VS2019 WinFrm應用程序調用WebService服務

WinFrm應用程序調用WebService服務

關於WebService的建立、發佈與部署等相關操做再也不贅述,傳送門以下:C# VS2019 WebService建立與發佈,並部署到Windows Server 2012Rhtml

此篇記錄一下客戶端的調用,以便後續學習使用,不足之處請指出。數據庫

創建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/
  本文版權歸做者和博客園共有,歡迎轉載,但未經做者贊成必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接,不然保留追究法律責任的權利。 ide

相關文章
相關標籤/搜索