4.0 WCF 之旅 - 简单的计算器程序
一直以来都想把WCF系统的学习一下,可总是没有坚持下来。再加上平时工作中没有用到WCF去开发项目。所以对WCF的认识基本停留在初级阶段。4.0 马上都要正式发布了,再不学就跟不上潮流了。。
参考Artech的一些列关于WCF的文章开始我的4.0 WCF 学习之旅,目的不在于成文成书,权当自己的学习笔记。
开篇上我用WCF做的一个简单的计算器程序吧
步骤一:构建整个解决方案
- BY.WCF.Client : 一个WinForm应用的客户端
- BY.WCF.Contracts: 一个类库项目,定义服务契约
- BY.WCF.Entities: 一个类库项目,为后扩充项目用
- BY.WCF.Hosting:一个控制台(Console)应用,实现对定义在Services项目中的服务的寄宿
- BY.WCF.Service:一个类库项目,提供对WCF服务的实现。
- Web: 一个webform站点,为后扩充项目用
步骤二:创建服务契约
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace BY.WCF.Contracts
{
[ServiceContract(Name="CalculatorService",Namespace="http://www.biyinginfo.cn/")]
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);
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace BY.WCF.Contracts
{
[ServiceContract(Name="CalculatorService",Namespace="http://www.biyinginfo.cn/")]
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);
}
}
步骤三:创建服务
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BY.WCF.Contracts;
namespace BY.WCF.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;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BY.WCF.Contracts;
namespace BY.WCF.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;
}
}
}
步骤四:Host CalculatorService
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
namespace BY.WCF.Hosting
{
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(BY.WCF.Service.CalculatorService)))
{
host.AddServiceEndpoint(typeof(BY.WCF.Contracts.ICalculator), new WSHttpBinding(), "http://localhost:9000/calculatorservice");
if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)
{
ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
behavior.HttpGetEnabled = true;
behavior.HttpGetUrl = new Uri("http://localhost:9000/calculatorservice/metadata");
host.Description.Behaviors.Add(behavior);
}
host.Opened += delegate { Console.WriteLine("Calculaor Service Start, Press any key to stop the service!"); };
host.Open();
Console.Read();
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
namespace BY.WCF.Hosting
{
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(BY.WCF.Service.CalculatorService)))
{
host.AddServiceEndpoint(typeof(BY.WCF.Contracts.ICalculator), new WSHttpBinding(), "http://localhost:9000/calculatorservice");
if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)
{
ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
behavior.HttpGetEnabled = true;
behavior.HttpGetUrl = new Uri("http://localhost:9000/calculatorservice/metadata");
host.Description.Behaviors.Add(behavior);
}
host.Opened += delegate { Console.WriteLine("Calculaor Service Start, Press any key to stop the service!"); };
host.Open();
Console.Read();
}
}
}
}
步骤五:创建客户端:计算器
WinForm UI
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 BY.WCF.Client
{
public partial class Calculator : Form
{
string tmpInputStr = string.Empty;
string tmpResultStr = string.Empty;
Operation CurOperation;
public Calculator()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
tmpInputStr += "1";
this.textBox1.Text = tmpInputStr;
}
private void button2_Click(object sender, EventArgs e)
{
tmpInputStr += "2";
this.textBox1.Text = tmpInputStr;
}
private void button3_Click(object sender, EventArgs e)
{
tmpInputStr += "3";
this.textBox1.Text = tmpInputStr;
}
private void button4_Click(object sender, EventArgs e)
{
tmpInputStr += "4";
this.textBox1.Text = tmpInputStr;
}
private void button5_Click(object sender, EventArgs e)
{
tmpInputStr += "5";
this.textBox1.Text = tmpInputStr;
}
private void button6_Click(object sender, EventArgs e)
{
tmpInputStr += "6";
this.textBox1.Text = tmpInputStr;
}
private void button7_Click(object sender, EventArgs e)
{
tmpInputStr += "7";
this.textBox1.Text = tmpInputStr;
}
private void button8_Click(object sender, EventArgs e)
{
tmpInputStr += "8";
this.textBox1.Text = tmpInputStr;
}
private void button9_Click(object sender, EventArgs e)
{
tmpInputStr += "9";
this.textBox1.Text = tmpInputStr;
}
private void button10_Click(object sender, EventArgs e)
{
tmpInputStr += "0";
this.textBox1.Text = tmpInputStr;
}
private void button11_Click(object sender, EventArgs e)
{
using (ServiceReference.CalculatorServiceClient clientProxy = new ServiceReference.CalculatorServiceClient())
{
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 button12_Click(object sender, EventArgs e)
{
this.CurOperation = Operation.Add;
if (string.IsNullOrEmpty(this.tmpResultStr))
{
this.tmpResultStr = this.tmpInputStr;
}
this.tmpInputStr = string.Empty;
}
private void button13_Click(object sender, EventArgs e)
{
this.CurOperation = Operation.Subtract;
if (string.IsNullOrEmpty(this.tmpResultStr))
{
this.tmpResultStr = this.tmpInputStr;
}
this.tmpInputStr = string.Empty;
}
private void button14_Click(object sender, EventArgs e)
{
this.CurOperation = Operation.Multiply;
if (string.IsNullOrEmpty(this.tmpResultStr))
{
this.tmpResultStr = this.tmpInputStr;
}
this.tmpInputStr = string.Empty;
}
private void button15_Click(object sender, EventArgs e)
{
this.CurOperation = Operation.Divide;
if (string.IsNullOrEmpty(this.tmpResultStr))
{
this.tmpResultStr = this.tmpInputStr;
}
this.tmpInputStr = string.Empty;
}
private void button16_Click(object sender, EventArgs e)
{
this.tmpResultStr = string.Empty;
this.tmpInputStr = string.Empty;
this.textBox1.Text = string.Empty;
}
private void button17_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;
}
}
}
enum Operation : int
{
Add = 1,
Subtract = 2,
Multiply = 3,
Divide = 4
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace BY.WCF.Client
{
public partial class Calculator : Form
{
string tmpInputStr = string.Empty;
string tmpResultStr = string.Empty;
Operation CurOperation;
public Calculator()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
tmpInputStr += "1";
this.textBox1.Text = tmpInputStr;
}
private void button2_Click(object sender, EventArgs e)
{
tmpInputStr += "2";
this.textBox1.Text = tmpInputStr;
}
private void button3_Click(object sender, EventArgs e)
{
tmpInputStr += "3";
this.textBox1.Text = tmpInputStr;
}
private void button4_Click(object sender, EventArgs e)
{
tmpInputStr += "4";
this.textBox1.Text = tmpInputStr;
}
private void button5_Click(object sender, EventArgs e)
{
tmpInputStr += "5";
this.textBox1.Text = tmpInputStr;
}
private void button6_Click(object sender, EventArgs e)
{
tmpInputStr += "6";
this.textBox1.Text = tmpInputStr;
}
private void button7_Click(object sender, EventArgs e)
{
tmpInputStr += "7";
this.textBox1.Text = tmpInputStr;
}
private void button8_Click(object sender, EventArgs e)
{
tmpInputStr += "8";
this.textBox1.Text = tmpInputStr;
}
private void button9_Click(object sender, EventArgs e)
{
tmpInputStr += "9";
this.textBox1.Text = tmpInputStr;
}
private void button10_Click(object sender, EventArgs e)
{
tmpInputStr += "0";
this.textBox1.Text = tmpInputStr;
}
private void button11_Click(object sender, EventArgs e)
{
using (ServiceReference.CalculatorServiceClient clientProxy = new ServiceReference.CalculatorServiceClient())
{
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 button12_Click(object sender, EventArgs e)
{
this.CurOperation = Operation.Add;
if (string.IsNullOrEmpty(this.tmpResultStr))
{
this.tmpResultStr = this.tmpInputStr;
}
this.tmpInputStr = string.Empty;
}
private void button13_Click(object sender, EventArgs e)
{
this.CurOperation = Operation.Subtract;
if (string.IsNullOrEmpty(this.tmpResultStr))
{
this.tmpResultStr = this.tmpInputStr;
}
this.tmpInputStr = string.Empty;
}
private void button14_Click(object sender, EventArgs e)
{
this.CurOperation = Operation.Multiply;
if (string.IsNullOrEmpty(this.tmpResultStr))
{
this.tmpResultStr = this.tmpInputStr;
}
this.tmpInputStr = string.Empty;
}
private void button15_Click(object sender, EventArgs e)
{
this.CurOperation = Operation.Divide;
if (string.IsNullOrEmpty(this.tmpResultStr))
{
this.tmpResultStr = this.tmpInputStr;
}
this.tmpInputStr = string.Empty;
}
private void button16_Click(object sender, EventArgs e)
{
this.tmpResultStr = string.Empty;
this.tmpInputStr = string.Empty;
this.textBox1.Text = string.Empty;
}
private void button17_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;
}
}
}
enum Operation : int
{
Add = 1,
Subtract = 2,
Multiply = 3,
Divide = 4
}
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace BY.WCF.Client
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Calculator());
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace BY.WCF.Client
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Calculator());
}
}
}
步骤六:运行
启动服务
启动应用
下载演示源码:WCFStudySolution.rar