欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來(lái)到入門(mén)教程網(wǎng)!

C#教程

當(dāng)前位置:主頁(yè) > 軟件編程 > C#教程 >

WCF實(shí)現(xiàn)的計(jì)算器功能實(shí)例

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C#教程|點(diǎn)擊: 次

本文實(shí)例講述了WCF實(shí)現(xiàn)的計(jì)算器功能。分享給大家供大家參考,具體如下:

對(duì)于WCF,我們有了前面的理論基礎(chǔ),今天通過(guò)一個(gè)計(jì)算器的實(shí)例主要給大家講解怎么一步一步地創(chuàng)建一個(gè)完整的WCF應(yīng)用。

一、創(chuàng)建整個(gè)解決方案

Calculator.Service:一個(gè)類庫(kù)項(xiàng)目,定義服務(wù)契約(Service Contract),應(yīng)用System.ServiceModel程序集;提供對(duì)WCF服務(wù)的實(shí)現(xiàn)。
Calculator.Host:一個(gè)Windows窗體應(yīng)用程序,實(shí)現(xiàn)對(duì)定義在Calculator.Service項(xiàng)目中的服務(wù)的寄宿,該項(xiàng)目需要引用Calculator.Service項(xiàng)目和System.ServiceModel程序集。
Calculator.Client:一個(gè)Windows窗體應(yīng)用程序模擬服務(wù)的客戶端,該項(xiàng)目應(yīng)用System.ServiceModel程序集。

二、創(chuàng)建服務(wù)契約

一般,我們通過(guò)接口的形式定義服務(wù)契約。通過(guò)下面的代碼,將一個(gè)接口ICalculator定義成服務(wù)契約。我們通過(guò)在接口上應(yīng)用System.ServiceModel.ServiceContractAttribute特性將一個(gè)接口定義成服務(wù)契約。

將接口定義成服務(wù)契約后,接口的方法成員并不能自動(dòng)成為服務(wù)的操作。我們需要在相應(yīng)的操作方法上面顯式地應(yīng)用OperationContractAttribute特性。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace Calculator.Service
{
  [ServiceContract]
  public interface ICalculator
  {
    [OperationContract]
    double Add(double x, double y);
    [OperationContract]
    double Subtract(double x, double y);
    [OperationContract]
    double Multiply(double x, double y);
    [OperationContract]
    double Divide(double x, double y);
  }
}

三、創(chuàng)建服務(wù)

當(dāng)服務(wù)契約創(chuàng)建成功后,我們需要通過(guò)實(shí)現(xiàn)服務(wù)契約來(lái)創(chuàng)建具體的WCF服務(wù),WCF服務(wù)CalculatorService實(shí)現(xiàn)了服務(wù)契約的接口ICalculator,實(shí)現(xiàn)了所有的服務(wù)操作。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Calculator.Service
{
  public class CalculatorService:ICalculator
  {
    public double Add(double x, double y)
    {
      return x + y;
    }
    public double Subtract(double x, double y)
    {
      return x - y;
    }
    public double Multiply(double x, double y)
    {
      return x * y;
    }
    public double Divide(double x, double y)
    {
      return x / y;
    }
  }
}

四、通過(guò)自我寄宿的方式寄宿服務(wù)

服務(wù)寄宿的目的就是開(kāi)啟一個(gè)進(jìn)程,為WCF服務(wù)提供一個(gè)運(yùn)行的環(huán)境。通過(guò)為服務(wù)添加一個(gè)或多個(gè)中級(jí)誒單,使之暴露給潛在的服務(wù)消費(fèi)者。服務(wù)消費(fèi)者最終通過(guò)相匹配的終結(jié)點(diǎn)對(duì)該服務(wù)進(jìn)行調(diào)用。我們完全可以通過(guò)代碼的方式完成所有的服務(wù)寄宿工作。

using Calculator.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;
using System.Windows.Forms;
namespace Calculator.Host
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    ServiceHost host = null;
    private void btnOpen_Click(object sender, EventArgs e)
    {
      host = new ServiceHost(typeof(CalculatorService));
      host.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "http://localhost:8008/Calculator");
      if (host.Description.Behaviors.Find<ServiceMetadataBehavior>()==null)
      {
        ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
        behavior.HttpGetEnabled = true;
        behavior.HttpGetUrl = new Uri("http://localhost:8008/Calculator/metadata");
        host.Description.Behaviors.Add(behavior);
      }
      host.Opened += delegate { label1.Text = "服務(wù)已經(jīng)啟動(dòng)!"; };
      host.Open();
    }
    private void btnClose_Click(object sender, EventArgs e)
    {
      if (host.State != CommunicationState.Closed)
      {
        host.Closed += delegate { label1.Text = "服務(wù)已經(jīng)停止!"; };
        host.Close();
      }
    }
  }
}

五、創(chuàng)建客戶端調(diào)用服務(wù)

服務(wù)被成功寄宿后,服務(wù)端便開(kāi)始了服務(wù)調(diào)用請(qǐng)求的監(jiān)聽(tīng)工作。此外,服務(wù)寄宿將服務(wù)描述通過(guò)元數(shù)據(jù)的形式發(fā)布出來(lái),相應(yīng)的客戶端就可以獲取這些元數(shù)據(jù),創(chuàng)建愛(ài)你客戶端程序進(jìn)行服務(wù)的消費(fèi)。在VS下,當(dāng)我們添加服務(wù)引用的時(shí)候,VS在內(nèi)部幫我們實(shí)現(xiàn)元數(shù)據(jù)的獲取,并借組這些元數(shù)據(jù)通過(guò)代碼生成工具自動(dòng)生成用于服務(wù)調(diào)用的服務(wù)代理相關(guān)代碼和相應(yīng)的配置。

我們可以創(chuàng)建CalculatorClient對(duì)象,執(zhí)行相應(yīng)方法調(diào)用服務(wù)操作。

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 Calculator.Client
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
      comboBox1.Text = "+";
    }
    private void button1_Click(object sender, EventArgs e)
    {
      CalculatorService.CalculatorClient client = new CalculatorService.CalculatorClient();
      double x = Convert.ToDouble (textBox1.Text);
      double y = Convert.ToDouble(textBox2.Text);
      double result=0;
      string operater = comboBox1.Text;
      switch (operater )
      {
        case "+":
          result = client.Add(x, y);
          break;
        case "-":
          result = client.Subtract(x, y);
          break;
        case "*":
          result = client.Multiply(x, y);
          break;
        case "/":
          if (y==0)
          {
            MessageBox.Show("除數(shù)不能為0!");
            return;
          }
          result = client.Divide(x, y);
          break;
      }
      label1.Text = textBox1.Text + comboBox1.Text + textBox2.Text + " = " + Convert.ToString(result);
    }
  }
}

在這個(gè)計(jì)算器實(shí)例中,我們實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的計(jì)算服務(wù)(CalculatorService),提供基本的加、減、乘、除的運(yùn)算??蛻舳撕头?wù)通過(guò)運(yùn)行在不同進(jìn)程模擬,體現(xiàn)了客戶端和服務(wù)端進(jìn)程互相調(diào)用的關(guān)系。

PS:這里再為大家推薦幾款在線計(jì)算工具供大家參考使用:

在線一元函數(shù)(方程)求解計(jì)算工具:
http://tools.jb51.net/jisuanqi/equ_jisuanqi

科學(xué)計(jì)算器在線使用_高級(jí)計(jì)算器在線計(jì)算:
http://tools.jb51.net/jisuanqi/jsqkexue

在線計(jì)算器_標(biāo)準(zhǔn)計(jì)算器:
http://tools.jb51.net/jisuanqi/jsq

更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#常見(jiàn)控件用法教程》、《C#窗體操作技巧匯總》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》及《C#程序設(shè)計(jì)之線程使用技巧總結(jié)》

希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。

上一篇:C# networkcomms 3.0實(shí)現(xiàn)模擬登陸總結(jié)

欄    目:C#教程

下一篇:C#中各種計(jì)時(shí)器用法小結(jié)

本文標(biāo)題:WCF實(shí)現(xiàn)的計(jì)算器功能實(shí)例

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5672.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有