您的位置:首页 > 其它

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

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要和宿主一致。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: