本文实例讲述了wcf实现的计算器功能。分享给大家供大家参考,具体如下:
对于wcf,我们有了前面的理论基础,今天通过一个计算器的实例主要给大家讲解怎么一步一步地创建一个完整的wcf应用。
一、创建整个解决方案
calculator.service:一个类库项目,定义服务契约(service contract),应用system.servicemodel程序集;提供对wcf服务的实现。
calculator.host:一个windows窗体应用程序,实现对定义在calculator.service项目中的服务的寄宿,该项目需要引用calculator.service项目和system.servicemodel程序集。
calculator.client:一个windows窗体应用程序模拟服务的客户端,该项目应用system.servicemodel程序集。
二、创建服务契约
一般,我们通过接口的形式定义服务契约。通过下面的代码,将一个接口icalculator定义成服务契约。我们通过在接口上应用system.servicemodel.servicecontractattribute特性将一个接口定义成服务契约。
将接口定义成服务契约后,接口的方法成员并不能自动成为服务的操作。我们需要在相应的操作方法上面显式地应用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);
}
}
三、创建服务
当服务契约创建成功后,我们需要通过实现服务契约来创建具体的wcf服务,wcf服务calculatorservice实现了服务契约的接口icalculator,实现了所有的服务操作。
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;
}
}
}
四、通过自我寄宿的方式寄宿服务
服务寄宿的目的就是开启一个进程,为wcf服务提供一个运行的环境。通过为服务添加一个或多个中级诶单,使之暴露给潜在的服务消费者。服务消费者最终通过相匹配的终结点对该服务进行调用。我们完全可以通过代码的方式完成所有的服务寄宿工作。
using calculator.service;
using system;
using system.collections.generic;
using system测试数据ponentmodel;
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 = "服务已经启动!" ; };
host.open();
}
private void btnclose_click( object sender, eventargs e)
{
if (host.state != communicationstate.closed)
{
host.closed += delegate { label1.text = "服务已经停止!" ; };
host.close();
}
}
}
}
五、创建客户端调用服务
服务被成功寄宿后,服务端便开始了服务调用请求的监听工作。此外,服务寄宿将服务描述通过元数据的形式发布出来,相应的客户端就可以获取这些元数据,创建爱你客户端程序进行服务的消费。在vs下,当我们添加服务引用的时候,vs在内部帮我们实现元数据的获取,并借组这些元数据通过代码生成工具自动生成用于服务调用的服务代理相关代码和相应的配置。
我们可以创建calculatorclient对象,执行相应方法调用服务操作。
using system;
using system.collections.generic;
using system测试数据ponentmodel;
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( "除数不能为0!" );
return ;
}
result = client.divide(x, y);
break ;
}
label1.text = textbox1.text + combobox1.text + textbox2.text + " = " + convert.tostring(result);
}
}
}
在这个计算器实例中,我们实现了一个简单的计算服务(calculatorservice),提供基本的加、减、乘、除的运算。客户端和服务通过运行在不同进程模拟,体现了客户端和服务端进程互相调用的关系。
希望本文所述对大家c#程序设计有所帮助。
dy("nrwz");