wcf 实现多契约 z
我们知道,WCF服务端是先定义服务协定,其实就是一个接口,然后通过实现接口来定义服务类。那么,有一个问题,如果一个服务类同时实现N个接口(也就是有N个协定)呢?结果会如何?
不必猜,我们还是通过实验来说明吧。
首先,参照下面代码写好服务器端,代码中,我写了三个协定,然后一个服务类同时实现它们。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description; namespace Server
{
[ServiceContract]
public interface IService1
{
[OperationContract]
string SayHelloA();
} [ServiceContract]
public interface IService2
{
[OperationContract]
string SayHelloB();
} [ServiceContract]
public interface IService3
{
[OperationContract]
string SayHelloC();
} /// <summary>
/// 实现服务协定接口的服务类
/// </summary>
class MyService:IService1,IService2,IService3
{
static void Main(string[] args)
{
using (ServiceHost host=new ServiceHost(typeof(MyService),new Uri("http://localhost:8001/service")))
{
WSHttpBinding binding = new WSHttpBinding();
binding.Name = "MyBinding";
binding.Security.Mode = SecurityMode.None;
host.AddServiceEndpoint(typeof(IService1), binding, "mysv1");
ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
behavior.HttpGetEnabled = true;
host.Description.Behaviors.Add(behavior); host.Opened += (s, a) => Console.WriteLine("服务已启动。"); try
{
host.Open();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadKey();
host.Close();
}
} string IService1.SayHelloA()
{
return "你好,这是第一个服务协定。";
} string IService2.SayHelloB()
{
return "你好,这是第二个服务协定。";
} string IService3.SayHelloC()
{
return "你好,这是第三个服务协定。";
}
}
}
接着你试一下,在客户端引用服务,看看是什么样的?

神了,这是怎么回事呢?为什么客户端引用的服务中只有第一个协定?
还记得刚才的服务器端代码吗?请注意看我框起来的代码。

如果你上图中能找到原因,表明你悟性不错,呵呵。
注意我们添加终结点的时候,AddServiceEndpoint方法的第一个参数我们传了啥玩意儿进去了?是不是只有第一个服务协定的接口?
是啊,这上好理解了,
我们仅仅让服务类实现多个协定的接口是不够的,还要把希望对客户端公开的协定添加为终结点,对,一个协定一个终结点,不添加终结点的协定就不公开。
好了,现在找到原因了,代码知道怎么改了。
using (ServiceHost host=new ServiceHost(typeof(MyService),new Uri("http://localhost:8001/service")))
{
WSHttpBinding binding = new WSHttpBinding();
binding.Name = "MyBinding";
binding.Security.Mode = SecurityMode.None;
host.AddServiceEndpoint(typeof(IService1), binding, "mysv1");
host.AddServiceEndpoint(typeof(IService2), binding, "mysv2");
host.AddServiceEndpoint(typeof(IService3), binding, "mysv3");
ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
behavior.HttpGetEnabled = true;
host.Description.Behaviors.Add(behavior);
host.Opened += (s, a) => Console.WriteLine("服务已启动。");
try
{
host.Open();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadKey();
host.Close();
}
看看这回达到目的了没?

嘿,这回全有了。
不过,注意啊,在生成的客户端代理类中,是按协定来划分的,也就是说,我有三个协定,在客户端代码中就生成了三个客户端类。

所以,在客户端,我们应该这样调用。
class Program
{
static void Main(string[] args)
{
WS.Service1Client client1 = new WS.Service1Client();
Console.WriteLine(client1.SayHelloA()); WS.Service2Client client2 = new WS.Service2Client();
Console.WriteLine(client2.SayHelloB()); WS.Service3Client client3 = new WS.Service3Client();
Console.WriteLine(client3.SayHelloC()); Console.ReadKey();
}

wcf 实现多契约 z的更多相关文章
- WCF如何通过契约加编码方式调用
WCF采用基于契约的服务调用方法,通过System.ServiceModel.ChannelFactory<TChannel>直接创建服务代理对象. 创建服务代理 public stati ...
- [WCF编程]4.契约概述
一.契约的基本概念 契约是消息参与者之间的约定.在SOA架构中,契约提供了服务通信所必需的元数据.契约用来定义数据类型,操作,消息交换模式和消息交换使用的传输协议.契约通常是在标准化平台中使用与编程语 ...
- 重温WCF之数据契约中使用枚举(转载)(十一)
转载地址:http://www.zhuli8.com/wcf/EnumMember.html 枚举类型的定义总是支持序列化的.当我们定义一个新的枚举时,不必应用DataContract特性,就可以在数 ...
- 重温WCF之数据契约和序列化(四)
一.数据契约 1.使用数据协定可以灵活控制哪些成员应该被客户端识别. [DataContract] public class Employee { [DataMember] public string ...
- WCF之数据契约
从抽象层面看,WCF能够托管CLR类型(接口和类)并将它们公开为服务,也能够以本地CLR接口和类的方式使用服务.然而,CLR类型却属于.NET的特定技术.由于面向服务的一个核心原则就是在跨越服务边界时 ...
- WCF中数据契约之已知类型的几种公开方式
WCF中传输的数据不想传统的面向对象编程,它只传递了一些对象的属性,但是自身并不知道自己属于什么对象,所以,他没有子类和父类的概念,因而也就没有Is-a的关系,所以在WCF中,如果想维持这种继承关系, ...
- 通过 WCF 实现点对点文件共享 z
下载免费的项目源代码 下载项目的数据库 目录 简介 背景 为什么是WCF? WCF历史简述 WCF基础 点对点概念 代码分析(它是怎么工作的) 核心转化引擎层 下载管理层 服务层 代码的使用(如何运行 ...
- WCF 之 数据契约
前面几篇讲的都只能传递string类型的简单参数,数据契约就是用来解决如传递一个带有多个属性的Class类型的对象的. WCF推荐使用数据契约的方式实现数据的序列化.这部分的内容很好理解但是很重要,先 ...
- WCF学习笔记——契约不能少了set
我定义的WCF契约里,有一个类,里面的属性,有一个因为只读,所以只写了个get.结果客户端就报错. [DataContract] public class UserItem { public User ...
随机推荐
- [转] linux下shell中使用上下键翻出历史命名时出现^[[A^[[A^[[A^[[B^[[B的问题解决,Linux使用退格键时出现^H解决方法
[From] https://www.zmrbk.com/post-2030.html https://blog.csdn.net/suifengshiyu/article/details/40952 ...
- C++ GUI Qt4编程(11)-5.1hexSpinbox
1. hexspinbox.cpp /* * The spin box supports integer values but can be extended to use different str ...
- poj3176
一.题意:给定一些数,成三角形排列.从上往下走,每个数只能往它相邻的两个数走,一直走到底端得到一条线路.这条线路上的数的和最大是多少 二.思路:简单的动态规划.dp[i+1][j+1]:=以第i+1行 ...
- How to Setup a Private Proxy Server on EC2 in Under 10 Minutes
How to Setup a Private Proxy Server on EC2 in Under 10 Minutes I’ve been slacking a bit with regular ...
- (转)增加定时检测linux占用内存,及时清理功能
增加定时检测linux占用内存,及时清理功能 原文:http://www.voidcn.com/article/p-wnmannom-boa.html free -m 查看,发现内存跑满了. 再 to ...
- TOJ 1856 Is It A Tree?
Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a ...
- adb调试安卓
http://blog.csdn.net/liushida00/article/details/49797239
- Android触摸事件传递机制
简单梳理一下Android触摸事件传递机制的知识点. 一.View与ViewGroup的关系 View和ViewGroup二者的继承关系如下图所示: View是Android中最基本的一种UI组件,它 ...
- linux下统计文本行数的各种方法(二)
上一篇讲的都是统计单个文件的方法,直接在命令行执行就可以.现在试试脚本的方式,统计多个文件的行数 一.统计目录下所有文件的文件数及所有行数 脚本暂时命名为count.sh,代码如下: #!/bin/b ...
- C++ 编译器
C++编译器 当我们定义了一个类的时候, C++编译器在默认的情况下会为我们添加默认的构造方法, 拷贝构造方法, 析构函数和=运算符 在第一次创建对象的语句中如: MyString myString ...