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


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/
  本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM