解释一下这个博客Known Types此属性标签,假设翻译的单词,所谓已知类型它。在服务契约中实现多态的行为。有四种方式实现,以下来分别演示一下。

1.User KnownType attribute on the base type.

这样的方式是针对于全局的。假设採用这样的方式后,全局的服务契约和全部的方法契约将都会默认加上此种方式。

员工类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
namespace EmployeeService
{
[KnownType(typeof(FullTimeEmployee))]
[DataContract(Namespace="http://Employee/employee")]
public class Employee
{
private int _id;
private string _name;
private string _gender;
private DateTime _dateOfBirth; [DataMember(Name="id",Order=1)]
public int ID
{
get { return _id; }
set { _id = value; }
} [DataMember(Name = "Name", Order = 2)]
public string Name
{
get { return _name; }
set { _name = value; }
} [DataMember(Name = "Gender", Order = 2)]
public string Gender
{
get { return _gender; }
set { _gender = value; }
} public DateTime DateOfBirth
{
set { _dateOfBirth = value; }
get { return _dateOfBirth; }
} }
}

全职员工类。继承自基类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
namespace EmployeeService
{
[DataContract]
public class FullTimeEmployee:Employee
{
public int AnnualSalary { set; get; }
}
}

上述中FullTimeEmployee继承自Employee,此时假设在Employee上增加 [KnownType(typeof(FullTimeEmployee))]标签后,client也就会看到FullTimeEmployee

2.Apply ServiceKnownType attribute on the service contract.

另外一种方式是,直接把此标签加入到服务契约上,那么此时。仅仅有此服务契约共享该契约实体

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text; namespace EmployeeService
{
// 注意: 使用“重构”菜单上的“重命名”命令,能够同一时候更改代码和配置文件里的接口名“IEmployeeService”。
 [ServiceKnownType(typeof(FullTimeEmployee))]
[ServiceContract]
public interface IEmployeeService
{
[OperationContract]
Employee GetEmployee(int Id); [OperationContract]
void SaveEmployee(Employee Employee);
}
}

3. Apply ServiceKnownType attribute on specific operation contracts.

此种方式是把已知类型加入到服务契约的方法上,那么结果可想而知,仅仅有该服务契约的某个方法上才干够共享契约对象。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text; namespace EmployeeService
{
// 注意: 使用“重构”菜单上的“重命名”命令。能够同一时候更改代码和配置文件里的接口名“IEmployeeService”。 [ServiceContract]
public interface IEmployeeService
{
 [ServiceKnownType(typeof(FullTimeEmployee))]
[OperationContract]
Employee GetEmployee(int Id); [OperationContract]
void SaveEmployee(Employee Employee);
}
}

4.Specifiy known types in the configuration file

最后一种方式是直接在配置文件里进行配置,因为是全局的与第一种一样。所以就不在此讲述了。

版权声明:本文博客原创文章,博客,未经同意,不得转载。

Different ways of associating known types in wcf的更多相关文章

  1. WCF入门(8)

    前言 昨天买的usb无线路由到了,笔记本又可以愉快的上网了. 下午去办市民卡,被告知说“本人医保现在停保,要等继续缴才行”,白公交坐了那么远的路. 需要视频的进群,378190436. 第八集 Dif ...

  2. Three ways to do WCF instance management

    Three ways to do WCF instance management (Per call, Per session, and Single). IntroductionVery often ...

  3. WCF Concurrency (Single, Multiple, and Reentrant) and Throttling

    http://www.codeproject.com/Articles/89858/WCF-Concurrency-Single-Multiple-and-Reentrant-and Introduc ...

  4. WCF学习系列四--【WCF Interview Questions – Part 4 翻译系列】

    WCF Interview Questions – Part 4   This WCF service tutorial is part-4 in series of WCF Interview Qu ...

  5. (WCF) 多线程 (Multi-threading) 和 并发性 (Concurency)

    问题:WCF 有个Server端,还有个Client端,他们之间是如何进行并发,多线程通信的呢?多个Client端同时访问Server,如何保证Server端的操作线程安全呢? 在理解WCF Conc ...

  6. how to do error handing with WCF by using attributes to log your errors z

    There are multiple ways to do error handling in WCF as listed by Pedram Rezaei Blog. The default way ...

  7. Java Interview Reference Guide--reference

    Part 1 http://techmytalk.com/2014/01/24/java-interview-reference-guide-part-1/ Posted on January 24, ...

  8. 栈帧 2.6. Frames 虚拟机内存模型

    https://docs.oracle.com/javase/specs/jvms/se11/html/jvms-2.html#jvms-2.6 小结: 1. https://docs.oracle. ...

  9. 可扩展的Web系统和分布式系统(Scalable Web Architecture and Distributed Systems)

    Open source software has become a fundamental building block for some of the biggest websites. And a ...

随机推荐

  1. WPF与混淆器

    原文:WPF与混淆器 时至今日,混淆依然是.Net程序的一道重要保护手段,而混淆器对WPF应用程序的支持是怎样的呢?我们今天就通过实例讲解一下. 首先建立如下图所示的简单的用户界面: 在界面代码中设置 ...

  2. Exec l 中分列的作用

  3. poj1236 有向图加边变成强连通图

    给我们一个有向图,有两个问题 1.最少要给多少个点发消息,才能使得所有的点都收到消息(消息可以随边传递) 2.最少需要多少条边才能使得图变成强连通图 对于一个强连通分量,可以当做一个点来考虑,所以我们 ...

  4. 状态压缩dp(hdu2167,poj2411)

    hdu2167 http://acm.hdu.edu.cn/showproblem.php?pid=2167 给定一个N*N的板子,里面有N*N个数字,选中一些数字,使得和最大 要求任意两个选中的数字 ...

  5. Ubuntu 12.04开启3D桌面特效

    1.设定软件源,更新软件 点击左边栏Dash主页(Ubuntu图标),输入更新管理器,会出现更新管理器,打开后点设置,弹出软件源对话框,为确保能够正常更新,选主服务器 点击检查,更新完后,点重启 2. ...

  6. C# Windows Phone 8 WP8 开发,将WebClient的DownloadStringCompleted事件改成非同步的awiat方法。

    原文:C# Windows Phone 8 WP8 开发,将WebClient的DownloadStringCompleted事件改成非同步的awiat方法. 一般我们在撰写Windows Phone ...

  7. cocos2d-x3.0rc 版 设置模拟器窗体大小

    由于刚接触这职业时间不是非常长.也是第一次写博客,假设有错误的地方还请大神们指出,开通这博客目的非常easy相互学习和讨论(更重要的是记录工作中学到的东西以方便以后自己查阅) 先后參与过两个项目.只是 ...

  8. HTML+CSS样式设置——CSS一学就会

    HTML+CSS样式设置 CSS:(Cascading Style Sheets)层叠样式设置表. 网页的展示效果跟其排版有非常大的关系.排版则主要依靠CSS来设置.调节. 以下说CSS与HTML的联 ...

  9. 玩转Web之servlet(四)---B/S是如何使用http协议完成通信过程的

    在上一篇文章中,我简单的说了一下B/S架构的流程图,关于浏览器和服务器之间的通信过程知识含糊的说了一下,在这篇文章中我再总结一下B/S架构里是如何利用http协议去完成通信的. (一)通讯过程 1:浏 ...

  10. ps设计资料整理

    零基础学会建立一个简单化妆品网站—前台设计篇1[PS画草图] http://xiebiji.com/2008/09/huazhuang4/?wptheme=Plainscape&ie=1 PS ...