WCF入门篇--实现简单的HelloWorld效果
2012-05-22 22:53
148 查看
WCF实际上是构建了一个基于SOA(Service Oriented Architecture)框架,这个框架实现了在互联系统中各个Application之间如何通信。使得开发者和设计者在构建分布式系统中,无需在考虑如何去实现通信相关的问题,更加关注与系统的业务逻辑本身。而在WCF 框架中,各个Application之间的通信是由Endpoint来实现的。
Endpoints是WCF实现通信的核心要素。一个WCF Service由一个Endpoints集合组成,每个Endpoint就是用于通信的入口,客户端和服务端通过Endpoint交换信息。
Endpoint由三部分组成:Address,Binding,Contract。便于记忆,我们往往将这三部分称为是Endpoint的WCF ABC。
Address:记录WCF服务唯一的地址
Binding:绑定实现了通信的所有细节,包括网络传输、消息编码,以及其他为实现某种功能(比如安全、可靠传输、事务等)对消息进行的相应处理。WCF中具有一系列的系统定义绑定,比如BasicHttpBinding、WsHttpBinding、NetTcpBinding等
Contract:契约是对服务操作的抽象,也是对消息交换模式以及消息结构的定义。
WCF的服务不能孤立地存在,需要寄宿于一个运行着的进程中,我们把承载WCF服务的进程称为宿主,为服务指定宿主的过程称为服务寄宿(Service Hosting)。
对于WCF,我个人把整个项目分为了三个部分:
1.建立WCF服务
2.建立宿主(承载WCF服务)
3.建立客户端
(记住:需要使用WCF服务的,都需要添加引用:System.ServiceModel)
先看效果:
我的项目列表:
第一步,建立一个WCF服务--WcfService
1.IService1.cs
2.Service1.svc
第二步,建立WCF宿主(承载WCF服务的进程)---WcfHost
在宿主里添加APP.config配置文件
第三步,建立客户端----WcfClient
添加服务端APP.config配置文件:
基本的流程就是这样,服务端的address,binding要和宿主一致。
Endpoints是WCF实现通信的核心要素。一个WCF Service由一个Endpoints集合组成,每个Endpoint就是用于通信的入口,客户端和服务端通过Endpoint交换信息。
Endpoint由三部分组成:Address,Binding,Contract。便于记忆,我们往往将这三部分称为是Endpoint的WCF ABC。
Address:记录WCF服务唯一的地址
Binding:绑定实现了通信的所有细节,包括网络传输、消息编码,以及其他为实现某种功能(比如安全、可靠传输、事务等)对消息进行的相应处理。WCF中具有一系列的系统定义绑定,比如BasicHttpBinding、WsHttpBinding、NetTcpBinding等
Contract:契约是对服务操作的抽象,也是对消息交换模式以及消息结构的定义。
WCF的服务不能孤立地存在,需要寄宿于一个运行着的进程中,我们把承载WCF服务的进程称为宿主,为服务指定宿主的过程称为服务寄宿(Service Hosting)。
对于WCF,我个人把整个项目分为了三个部分:
1.建立WCF服务
2.建立宿主(承载WCF服务)
3.建立客户端
(记住:需要使用WCF服务的,都需要添加引用:System.ServiceModel)
先看效果:
我的项目列表:
第一步,建立一个WCF服务--WcfService
1.IService1.cs
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace WcfService { // 注意: 如果更改此处的接口名称 "IService1",也必须更新 Web.config 中对 "IService1" 的引用。 [ServiceContract] public interface IService1 { [OperationContract] string SayHello(string name); // 任务: 在此处添加服务操作 } }
2.Service1.svc
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace WcfService { // 注意: 如果更改此处的类名“Service1”,也必须更新 Web.config 和关联的 .svc 文件中对“Service1”的引用。 public class Service1 : IService1 { public string SayHello(string name) { return string.Format("{0},你好!nice to meet you!",name); } } }
第二步,建立WCF宿主(承载WCF服务的进程)---WcfHost
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using WcfService; namespace WcfHost { class Program { static void Main(string[] args) { using (ServiceHost host = new ServiceHost(typeof(Service1))) { host.Opened += (sender, e) => { Console.WriteLine("Service is Started..."); }; host.Open(); Console.Read(); } } } }
在宿主里添加APP.config配置文件
<span style="font-size:12px;"><?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <services> <service name="WcfService.Service1"> <endpoint address="" binding="wsHttpBinding" contract="WcfService.IService1"></endpoint> <host> <baseAddresses> <add baseAddress="http://localhost:9876/helloworld"/> </baseAddresses> </host> </service> </services> </system.serviceModel> </configuration></span>
第三步,建立客户端----WcfClient
<span style="font-size:12px;">using System; using System.Collections.Generic; using System.Linq; using System.Text; using WcfService; using System.ServiceModel; namespace WcfClient { public class Client:ClientBase<IService1> { public string SayHello(string name) { return this.Channel.SayHello(name); } } class Program { static void Main(string[] args) { Client client = new Client(); Console.WriteLine("Client is Accepted!"+client.SayHello("WangLong")); Console.Read(); } } } </span>
添加服务端APP.config配置文件:
<span style="font-size:12px;"><?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <client> <endpoint address="http://localhost:9876/helloworld" binding="wsHttpBinding" contract="WcfService.IService1"></endpoint> </client> </system.serviceModel> </configuration></span>
基本的流程就是这样,服务端的address,binding要和宿主一致。
相关文章推荐
- 利用jQuery实现简单的拖曳效果实例代码
- 简单实现手机游戏雪花场景效果
- Jquery实现简单的轮播效果(代码管用)
- 文本框水印提示效果的简单实现代码
- jquery实现的简单二级菜单效果代码
- jQuery实现手机版页面翻页效果的简单实例
- Tab切换动画滑动效果的一个简单实现
- C# 无边框窗体边框阴影效果的简单实现
- Asp.net图片上传实现预览效果的简单代码
- 简单实现ListView顶部悬浮效果
- webService 使用CXF 实现简单的helloworld
- js基础练习---获取数组内容实现简单日历的效果
- 换个思路更简单:方便好用的文字描边效果实现方法
- JNI简单实现Java调用C++/C的HelloWorld
- 简单几行js实现tab选项切换效果
- Android滑动菜单特效实现,仿人人客户端侧滑效果,史上最简单的侧滑实现
- 分享超简单实用的用JS实现DIV、图片圆角效果,关键是很兼容
- Activity 透明 半透明效果的简单实现
- android.graphics.Camera 实现简单的3D效果
- Android 简单实现Activity界面变暗效果