WCF客户端和服务

?服务器端:

– 定义和实现服务契约

– 为服务类型构建ServiceHost实例,暴露endpoints

– 打开通讯通道

?客户端:

– 需要服务契约的一个副本和关于endpoints的信息

– 为特定的endpoint构建通信通道并且调用操作

客户端的客户进程中,有一个代理,该代理主要功能是完成客户进程和主机进程之间的通信。Proxy并不直接和主机的Endpoint通信,而是由客户端自己提供一个Endpoint和主机的Endpoint通信。

服务端有一个主机Host,提供服务,服务由Endpoint向外发布接口。

消息的通信由两端的Endpoint通信。

代码示例:

服务端代码

using System;

using System.ServiceModel;

namespace HelloIndigo

{

[ServiceContract(Namespace="http://www.monkeyfu.net")]

public interface IHelloIndigoService

{

[OperationContract]

string HelloIndigo(string message);

}

public class HelloIndigoService : IHelloIndigoService

{

#region IHelloIndigoService Members

public string HelloIndigo(string message)

{

return string.Format("Receivied message at{0}:{1}", DateTime.Now, message);

}

#endregion

}

}

此处主要定义了服务契约。在WCF中,定义一个契约使用的是ServiceContract。只有定义了OperationContract的方法才会被放入服务中。

宿主程序

通常情况,服务宿主程序需要使用ServiceHost类,当使用IIS或者WAS作为宿主程序的时候IIS和WAS会自动创建ServiceHost类型。当自定义宿主服务的时候,需要手动创建ServiceHost对象。

namespace Host

{

class Program

{

static void Main(string[] args)

{

using (ServiceHost host = new ServiceHost(typeof(HelloIndigo.HelloIndigoService)))//手动创建ServiceHost

{

host.AddServiceEndpoint(typeof(HelloIndigo.IHelloIndigoService), new NetTcpBinding(), "net.tcp://localhost:9009/HelloIndigo");

host.Open();

Console.ReadLine();

}

}

}

}

客户端

客户端的契约要和服务端的保持一致

namespace Client

{

//和服务端的契约定义要一致。

[ServiceContract(Namespace = "http://www.monkeyfu.net")]

public interface IHelloIndigoService

{

[OperationContract]

string HelloIndigo(string message);

}

class Program

{

static void Main(string[] args)

{

IHelloIndigoService proxy = ChannelFactory<IHelloIndigoService>.CreateChannel(new NetTcpBinding(), new EndpointAddress("net.tcp://localhost:9009/HelloIndigo"));

string s = proxy.HelloIndigo("Hello from client...");

Console.WriteLine(s);

Console.ReadLine();

}

}

}

Endpoint

Endpoint实际上由ABC三个要素组成Address,Binding,Contract

使用配置文件实现服务和Endpoint

前面的方法是通过手工编程实现的,事实上,可以不用写代码而通过配置文件实现服务调用。

只使用代码而不用配置文件的情况不适合IIS为宿主的情况,IIS宿主必须使用配置文件配置WCF的ServiceHost。

主要步骤如下:

配置Host的配置文件如下:

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<system.serviceModel>

<services>

<service name="HelloIndigo.HelloIndigoService" behaviorConfiguration="serviceBehavior">

<endpoint binding="basicHttpBinding" contract="HelloIndigo.IHelloIndigoService" address="HelloIndigo" />

<endpoint binding="mexHttpBinding" contract="IMetadataExchange" address="mex" />

<host>

<baseAddresses>

<add baseAddress="http://localhost:8008"/>

</baseAddresses>

</host>

</service>

</services>

<behaviors>

<serviceBehaviors>

<behavior name="serviceBehavior">

<serviceMetadata httpGetEnabled="true" />

</behavior>

</serviceBehaviors>

</behaviors>

</system.serviceModel>

</configuration>

配置完毕后,宿主代码可以如下写法, 不再需要添加endpoint

static void Main(string[] args)

{

using (ServiceHost host = new ServiceHost(typeof(HelloIndigo.HelloIndigoService)))//手动创建ServiceHost

{

//host.AddServiceEndpoint(typeof(HelloIndigo.IHelloIndigoService), new NetTcpBinding(), "net.tcp://localhost:9009/HelloIndigo");

host.Open();

Console.ReadLine();

}

}

为客户端添加Service Reference。对于客户端代码,可以不必要再写契约定义,因为添加完Service Reference之后,系统自动生成很多相关代码。按如下方法写即可。

添加完毕后可以适当修改app.config

namespace Client

{

////和服务端的契约定义要一致。

//[ServiceContract(Namespace = "http://www.monkeyfu.net")]

//public interface IHelloIndigoService

//{

// [OperationContract]

// string HelloIndigo(string message);

//}

class Program

{

static void Main(string[] args)

{

//IHelloIndigoService proxy = ChannelFactory<IHelloIndigoService>.CreateChannel(new NetTcpBinding(), new EndpointAddress("net.tcp://localhost:9009/HelloIndigo"));

Client.ServiceReference1.HelloIndigoServiceClient proxy = new Client.ServiceReference1.HelloIndigoServiceClient();

string s = proxy.HelloIndigo("Hello from client...");

Console.WriteLine(s);

Console.ReadLine();

}

}

}

本文转自cnn23711151CTO博客,原文链接: http://blog.51cto.com/cnn237111/572975,如需转载请自行联系原作者

最新文章

  1. 爬虫, 获取登录者的外网IP
  2. 安装R语言扩展包diveRsity-1
  3. 无责任Windows Azure SDK .NET开发入门篇三[使用Azure AD 管理用户信息--3.5 Delete删除用户]
  4. 基于ASP.Net +easyUI框架上传图片,判断格式+实现即时浏览
  5. 理解java的三大特性之多态(三)
  6. Ant构建与部署Java项目---入门
  7. 百度推送-sitemap-使用playframework框架实现-java
  8. phpcms v9栏目列表调用每一篇文章内容方法
  9. 705. New Distinct Substrings spoj(后缀数组求所有不同子串)
  10. Electron的代码调试
  11. 南邮攻防训练平台逆向第四题WxyVM
  12. hive批量删除表
  13. Web API统一异常处理 【转载】
  14. python的__get__、__set__、__delete__(1)
  15. Linux笔记:vi常用命令
  16. (动态)代理于HOOK的区别于关系
  17. MySQL和时间戳有关的函数
  18. spring3: AOP 之 6.2 AOP的HelloWorld
  19. Java ScheduledExecutorService源码分析
  20. sudo出现unable to resolve host

热门文章

  1. 云计算介绍、TCP/IP协议及配置
  2. python基本知识点if、while、等等
  3. 关于android中数据库的创建以及基础的增删改查的相应操作
  4. TP3快速入门
  5. Spring--开篇 (spring优缺点、模块组件、各个jar包详解)
  6. 【DataBase】更改root根用户密码 和 SQLyog安装
  7. ios 中使用 animation-play-state: paused 属性失效的问题
  8. 分屏神器PoweToys
  9. 记一次错误 POST http://127.0.0.1:8000/auth/signup/ 500 (Internal Server Error)
  10. pandas basic cheatsheet