WCF 学习笔记
Kagula
2012-2-13
正文
本文记录学习中碰到的最常用WCF使用方式。
使用WCF目的:
解决Silverlight程序同服务器程序高负载下的网络通讯问题。
之所以使用WCF方式是出于以下两个原因
[1]原来是想使用Active MQ产品,但是,支持Silverlight的Active MQ客户端SDK还不够成熟。
[2]直接自己在TCP上作两层协议(一层是数据完整性协议、剩下一层做数据安全性协议),或则自己在Silverlight上实现JMS客户端框架,但是工作量太大。
学习环境:
Win7(64位) 、 VS2010SP1 with C#
WCF简介:
WCF(Windows Communication Foundation)是Microsoft提出的网络通讯框架。承载WCF服务的进程称为宿主,宿主可以是Win32 Console程序,也可以是IIS。
我的第一个WCF服务程序
在VS2010SP1内,新建C#的Win32 Console项目。第一个项目由ICalculator.cs,calculatorService.cs,App.config,program.cs四个文件组成。
第一步:定义服务契约(Service Contract)
在项目里引入.NET的System.ServiceMode程序集,WCF框架的绝大部分实现和API定义在该程序集中。
定义服务契约的源码(ICalculator.cs)如下:
using System;
using System.ServiceModel;
namespace testWCF
{
[ServiceContract(Name="CalculatorService", Namespace="WCFDEMO")]
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);
}
}
第二步:实现服务
CalculatorService.cs源码清单如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace testWCF
{
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;
}
}
}
第三步:添加App.config文件
新建模板,可以右键单击项目名称,添加[Windows C# Items]->[General]->[Application Configuration File]添加App.config文件。
建立App.config目的,是定义哪些服务接口可供外部调用,接口绑定方式,源码如下
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="metadataBehavior">
<serviceMetadata httpGetEnabled="true" httpGetUrl="http://127.0.0.1:8085/calculatorservice/metadata" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="metadataBehavior" name="testWCF.CalculatorService">
<endpoint address="http://127.0.0.1:8085/calculatorservice" binding="wsHttpBinding" contract="testWCF.ICalculator" />
</service>
</services>
</system.serviceModel>
</configuration>
第四步:在项目的主函数里填入Hosting代码
启动服务
program.cs源码清单如下
using System;
using System.ServiceModel;
using System.ServiceModel.Description;
namespace testWCF
{
class Program
{
//在Win7下必须使用下面的命令授权
//netsh http add urlacl url=http://+:8085/calculatorservice user=kagula-pc\kagula
//kagula-pc:是我机器的名字
//kagula:是我机器中的缺省用户名
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(CalculatorService)))
{
host.Opened += delegate
{
Console.WriteLine("CalculaorService已经启动,按任意键终止服务!");
};
host.Open();
Console.Read();
}
}
}
}
在Win7下你需要参考下面的控制台命令,给WCF服务程序的endpoint授权。
“netsh http add urlacl url=http://+:8085/calculatorservice user=kagula-pc\kagula”
其中“kagula-pc”是我机器的名称,“kagula”是我在OS中的默认用户名。
我的第一个WCF客户端程序
新建Win32 Console应用程序
第一步:引入上文中的“ICalculator.cs”文件
第二步:为引用WCF服务新建App.config文件源码如下
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<client>
<endpoint address="http://127.0.0.1:8085/calculatorservice" binding="wsHttpBinding" contract="testWCF.ICalculator" name="calculatorservice" />
</client>
</system.serviceModel>
</configuration>
第三步:修改program.cs文件,调用WCF服务,源码如下
using System;
using System.Collections.Generic;
using System.ServiceModel;
using testWCF;
namespace testWCFClient2
{
class Program
{
static void Main(string[] args)
{
using (ChannelFactory<ICalculator> channelFactory = new ChannelFactory<ICalculator>( "calculatorservice"))
{
ICalculator proxy = channelFactory.CreateChannel();
using (proxy as IDisposable)
{
Console.WriteLine("x + y = {2} when x = {0} and y = {1}", 1, 2, proxy.Add(1, 2));
Console.WriteLine("x - y = {2} when x = {0} and y = {1}", 1, 2, proxy.Subtract(1, 2));
Console.WriteLine("x * y = {2} when x = {0} and y = {1}", 1, 2, proxy.Multiply(1, 2));
Console.WriteLine("x / y = {2} when x = {0} and y = {1}", 1, 2, proxy.Divide(1, 2));
}
}
}
}
}
现在第一对WCF服务端、客户端程序可以运行了。
参考资料
[1]《我的WCF之旅(1):创建一个简单的WCF程序》
http://www.cnblogs.com/artech/archive/2007/02/26/656901.html
[2]《使用WCF的相关问题》
http://www.iteye.com/blogs/tag/WCF
[3]《silverlight 连接wcf服务使用》
http://hi.baidu.com/mldark/blog/item/99d0fccec7266d2af9dc618c.html
[4]《如何使用 WCF 服務透過 TCP 傳輸在 Microsoft Silverlight 4》
http://support.microsoft.com/kb/2425652/zh-tw