本文共 6511 字,大约阅读时间需要 21 分钟。
WCF --- Windows Communiction Foundation,是一个推出已久的技术。已久有很多人写了不少关于WCF的文章,比如等,都是很好的文章。我只是想说一些基础的东西,然后写一个例子,让目前刚开始学习WCF的人有一个感性的了解,希望能给他们的学习带来一点用处,同时是自己对WCF认识加深的一个过程。
WCF是一个统一的,可用于建立安全,可靠的面向服务的应用高效的开发平台。WCF是构建安全可靠的事务性服务的统一框架。它是一种构建分布式面向服务系统的非常丰富的技术基础,它统一了消息风格和RPC[Remote Procedure Call]风格,并且通过二进制和基于开放标准的通信达到了平台最优化。
它整合了.Net平台下所有的和分布式系统有关的技术,例如ASP.NET Web服务(ASMX)、增强Web服务扩展(WSE)、.Net Remoting、企业服务(Enterprise Service)和微软消息队列(MSMQ)。
而要理解WCF,你首先要理解WCF基本的组成部分,分别为:
下面我做一个简单实例来说明一下,一个WCF的开发,调用过程。
一,首先我们创建一个Console Application,叫做WCFDemo
二,我们既然要开发WCF,当然要引入一个WCF Service Application了,叫做Service,如果你不想创建单独的项目,你可以在别的项目中添加WCF Service的Item。在创建WCF Project的时候会自动生成IService和Service.svc,通常IService这个Service Contract我们是不会要的,因为调用Service的Client也需要用,所以我们会把他们放到统一的地方,本例中是叫Interface的项目。单纯的Service.svc是很简单的一个实现了IService接口的一个类,没有什么特别之处,
1: namespace Service
2: {
3: // NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in Web.config and in the associated .svc file.
4: public class Service : IService
5: {
6: public Computer GetComputer()
7: {
8: Computer computer = new Computer();
9: computer.ComputerName = "Ralax - PC";
10: return computer;
11: }
12: }
13: }
而一个值得我们注意的地方就是,如果我们的Service名称变换了,一定要记得在Service.svc的Markup的CodeBehind修改引用,
1: <%@ ServiceHost Language="C#" Debug="true" Service="Service.Service" CodeBehind="Service.svc.cs" %>
而作为Service的Config,应该是很重要的一个地方了,Config是配置Service如何运行,如何访问等的很重要的地方。如下:
1:
2:
3:
4:
5:
6:
7: Upon deployment, the following identity element should be removed or replaced to reflect the
8: identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
9: automatically.
10: -->
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
这里面很重要的部分,就是endpoint的配置ABC,address,binding和contract,另外还有bindingConfiguration,behavirorConfiguration等。
三,我刚才提到IService被提到Interface这个项目中,共Client和Service共同使用,其实IService更加简单,就是一个加了ServiceContract的interface,和一些加了OperationContract的Methods。
1: namespace Interface
2: {
3: [ServiceContract]
4: public interface IService
5: {
6: [OperationContract]
7: Computer GetComputer();
8: }
9: }
四,另外一个项目就是Data,这个项目是我们的Model类,加上了DataContract就可以就可以用于WCF传输,同时对于公开的Property,需要加上DataMember。
1: namespace Data
2: {
3: [DataContract]
4: public class Computer
5: {
6: private string computerName;
7: [DataMember]
8: public string ComputerName
9: {
10: get { return computerName; }
11: set { computerName = value; }
12: }
13: }
14: }
完成了上面的部分,也可以说一个Service也就完成了,剩下的就是客户端的调用了。
五,在Console Application中创建一个Service的客户端代理类,如下:
1: namespace WCFDemo
2: {
3: public class ServiceClient : ClientBase,IService
4: {
5: public ServiceClient(System.ServiceModel.Channels.Binding binding,EndpointAddress remoteAddress) : base(binding,remoteAddress)
6: {
7:
8: }
9:
10: public Computer GetComputer()
11: {
12: return base.Channel.GetComputer();
13: }
14: }
15: }
需要注意的是,ServiceClient继承关系,以及构造函数
ServiceClient完成后,就是Client的调用了,如下:
1: namespace WCFDemo
2: {
3: class Program
4: {
5: static void Main(string[] args)
6: {
7: using(ServiceClient sClient = GetService())
8: {
9: Console.WriteLine(sClient.GetComputer().ComputerName);
10: Console.Read();
11: }
12: }
13:
14: public ServiceClient GetService()
15: {
16: WSHttpBinding binding = new WSHttpBinding();
17: EndpointAddress address = new EndpointAddress(new Uri("http://localhost/WCFDemo/Service.svc"));
18: ServiceClient client = new ServiceClient(binding, address);
19: return client;
20: }
21: }
22: }转: