代码改变世界

简单计算器

2013-04-10 13:21  Keiven_LY  阅读(229)  评论(0编辑  收藏  举报

2.1 项目说明

该实例的服务宿主为控制台应用程序,客户端为Windows窗体应用程序

2.2 项目架构

2.3 项目开发过程

第一步:创建一个空的解决方案,命名为“WCFTest_5”,(可以任意取名);

第二步:在上述解决方案中,文件——>添加——>新建项目,选择控制台应用程序,取名Host,这个控制台程序用来作为服务的宿主程序;

第三步:在解决方案中,右键Host——>添加——>新建项,选择WCF服务,取名CalculatorService,系统自动生成ICalculatorService.cs、CalculatorService.cs、App.config。其中ICalculatorService.cs为接口,在此添加契约,相当于web service中的methord;CalculatorService.cs为契约的实现方法;

 

ICalculatorService.cs中的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace Host
{
    // 注意: 如果更改此处的接口名称 "ICalculatorService",也必须更新 App.config 中对 "ICalculatorService" 的引用。
    [ServiceContract]
    public interface ICalculatorService
    {
        //[OperationContract]
        //void DoWork();
        [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);
    }
}

CalculatorService.cs中的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace Host
{
    // 注意: 如果更改此处的类名 "CalculatorService",也必须更新 App.config 中对 "CalculatorService" 的引用。
    public class CalculatorService : ICalculatorService
    {
        //public void DoWork()
        //{
        //}
        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;
        }
   }
}

App.config文件原则上可以不用改,但是address太长了

(默认的为baseAddress=http://localhost:8731/Design_Time_Addresses/Host/CalculatorService/

缩短为baseAddress=http://localhost:8731/CalculatorService/

 

修改Program.cs中的代码:

using System;
using System.ServiceModel; //必须添加这个引用
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Host
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(typeof(Host.CalculatorService)))
//Host为宿主名,CalculatorService为服务名
            {
                host.Open();
                Console.ReadLine();
                host.Close();
            }
        }
    }
}

第四步:保存,启动项目,系统在项目保存文件夹下自动生成Host.exe ,路径为:项目保存文件夹——>Host——>bin——>Debug——>Host.exe,双击Host.exe文件,启动服务;

第五步:添加客户端应用程序,文件——>添加——>新建项目——>Windows,选择Windows窗体应用程序,取名CalculatorClient。接下来设计用户计算器界面如下:

接下来,添加服务引用,将服务端的地址填入地址(A)栏,点击前往,出现CalculatorService服务,修改命名空间为ServiceReference,如下图,点击“确定”即可。

接下来,双击“=”按钮,转到button的click事件,整个代码如下:

using System;
using System.ServiceModel;//必须添加这个引用
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CalculatorClient
{
    enum Operation : int   //声明一个枚举类型 Operation
    {
        Add = 1,
        Subtract = 2,
        Multiply = 3,
        Divide = 4
    }

    public partial class Form1 : Form
    {
        string tmpInputStr = string.Empty;
        string tmpResultStr = string.Empty;

        Operation CurOperation;  //定义一个枚举类型

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)   //数字1按钮
        {
            tmpInputStr += "1";
            this.textBox1.Text = tmpInputStr;
        }

        private void button2_Click(object sender, EventArgs e)   //数字2按钮
        {
            tmpInputStr += "2";
            this.textBox1.Text = tmpInputStr;
        }

        private void button3_Click(object sender, EventArgs e)   //数字3按钮
        {
            tmpInputStr += "3";
            this.textBox1.Text = tmpInputStr;
        }

        private void button4_Click(object sender, EventArgs e)   //数字4按钮
        {
            tmpInputStr += "4";
            this.textBox1.Text = tmpInputStr;
        }

        private void button5_Click(object sender, EventArgs e)   //数字5按钮
        {
            tmpInputStr += "5";
            this.textBox1.Text = tmpInputStr;
        }
        private void button6_Click(object sender, EventArgs e)    //数字6按钮
        {
            tmpInputStr += "6";
            this.textBox1.Text = tmpInputStr;
        }

        private void button7_Click(object sender, EventArgs e)    //数字7按钮
        {
            tmpInputStr += "7";
            this.textBox1.Text = tmpInputStr;
        }

        private void button8_Click(object sender, EventArgs e)    //数字8按钮
        {
            tmpInputStr += "8";
            this.textBox1.Text = tmpInputStr;
        }

        private void button9_Click(object sender, EventArgs e)    //数字9按钮
        {
            tmpInputStr += "9";
            this.textBox1.Text = tmpInputStr;
        }

        private void button10_Click(object sender, EventArgs e)   //数字0按钮
        {
            tmpInputStr += "0";
            this.textBox1.Text = tmpInputStr;
        }

        private void button12_Click(object sender, EventArgs e)   //等号按钮
        {
            using (ServiceReference.CalculatorServiceClient clientProxy = new ServiceReference.CalculatorServiceClient())  //创建代理,用于连接服务器
//ServiceReference为引用服务的命名空间,CalculatorServiceClient为服务端的服务,clientProxy为创建的代理
            {
                switch (this.CurOperation)
                {
                    case Operation.Add:
                        this.textBox1.Text = clientProxy.Add(double.Parse(this.tmpResultStr), double.Parse(this.tmpInputStr)).ToString();
                        this.tmpResultStr = this.textBox1.Text;
                        this.tmpInputStr = string.Empty;
                        break;
                    case Operation.Subtract:
                        this.textBox1.Text = clientProxy.Subtract(double.Parse(this.tmpResultStr), double.Parse(this.tmpInputStr)).ToString();
                        this.tmpResultStr = this.textBox1.Text;
                        this.tmpInputStr = string.Empty;
                        break;
                    case Operation.Multiply:
                        this.textBox1.Text = clientProxy.Multiply(double.Parse(this.tmpResultStr), double.Parse(this.tmpInputStr)).ToString();
                        this.tmpResultStr = this.textBox1.Text;
                        this.tmpInputStr = string.Empty;
                        break;
                    case Operation.Divide:
                        this.textBox1.Text = clientProxy.Divide(double.Parse(this.tmpResultStr), double.Parse(this.tmpInputStr)).ToString();
                        this.tmpResultStr = this.textBox1.Text;
                        this.tmpInputStr = string.Empty;
                        break;
                }
            }
        }

        private void button13_Click(object sender, EventArgs e)   //加号按钮
        {
            this.CurOperation = Operation.Add;
            if (string.IsNullOrEmpty(this.tmpResultStr))
            {
                this.tmpResultStr = this.tmpInputStr;
            }
            this.tmpInputStr = string.Empty;
        }

        private void button14_Click(object sender, EventArgs e)   //减号按钮
        {
            this.CurOperation = Operation.Subtract;
            if (string.IsNullOrEmpty(this.tmpResultStr))
            {
                this.tmpResultStr = this.tmpInputStr;
            }
            this.tmpInputStr = string.Empty;

        }

        private void button15_Click(object sender, EventArgs e)   //乘号按钮
        {
            this.CurOperation = Operation.Multiply;
            if (string.IsNullOrEmpty(this.tmpResultStr))
            {
                this.tmpResultStr = this.tmpInputStr;
            }
            this.tmpInputStr = string.Empty;
        }

        private void button16_Click(object sender, EventArgs e)   //除号按钮
        {
            this.CurOperation = Operation.Divide;
            if (string.IsNullOrEmpty(this.tmpResultStr))
            {
                this.tmpResultStr = this.tmpInputStr;
            }
            this.tmpInputStr = string.Empty;
        }

        private void button17_Click(object sender, EventArgs e)   //清空按钮
        {
            this.tmpResultStr = string.Empty;
            this.tmpInputStr = string.Empty;
            this.textBox1.Text = string.Empty;
        }

        private void button11_Click(object sender, EventArgs e)   //负号按钮
        {
            if (this.textBox1.Text.IndexOf("-") == 0)
            {
                this.textBox1.Text = this.textBox1.Text.Replace("-", string.Empty);
                this.tmpInputStr = tmpInputStr.Replace("-", string.Empty);
            }
            else
            {
                this.textBox1.Text = "-" + this.textBox1.Text;
                this.tmpInputStr = "-" + tmpInputStr;
            }
        }  
    }
}