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站点,为后扩充项目用

步骤二:创建服务契约

ExpandedBlockStart.gif 契约:ICalculator
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);
    }
}

 

步骤三:创建服务

ExpandedBlockStart.gif 服务:CalculatorService
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;
        }
    }
}

 

步骤四:Host CalculatorService

ExpandedBlockStart.gif Host CalculatorService
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();
            }
        }
    }
}

 

步骤五:创建客户端:计算器

    WinForm UI

    

ExpandedBlockStart.gif Calculator Logic
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
    }
}
ExpandedBlockStart.gif WinForm Main
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());
        }
    }
}

 

步骤六:运行

    启动服务

   

    启动应用

   

 

下载演示源码:WCFStudySolution.rar

转载于:https://www.cnblogs.com/robertfang/articles/1692268.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值