解释一下这个博客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. Spring中Quartz动态设置cronExpression

    字段 允许值 允许的特殊字符 秒 0-59 , - * / 分 0-59 , - * / 小时 0-23 , - * / 日期 1-31 , - * ? / L W C 月份 1-12 或者 JAN- ...

  2. 从零开始学Xamarin.Forms(四) Android 准备步骤(添加第三方Xamarin.Forms.Labs库)

    原文:从零开始学Xamarin.Forms(四) Android 准备步骤(添加第三方Xamarin.Forms.Labs库)  1.安装对应dll     Update-Package Xama ...

  3. 从零开始学Xamarin.Forms(三) Android 制作启动画面

    原文:从零开始学Xamarin.Forms(三) Android 制作启动画面     Xamarin.Forms 在启动的时候相当慢,必须添加一个启动界面,步骤如下: 1.将启动画面的图片命名为:s ...

  4. HTML5 RPG游戏引擎 地图实现篇

    一,话说全国年夜事   前没有暂看到lufy的专客上,有一名伴侣念要一个RPG游戏引擎,出于兴趣筹办入手做一做.因为我研讨lufylegend有冶时间了,对它有必然的依赖性,因而便筹办将那个引擎基于 ...

  5. gopkg:一种方便的go pakcage管理方式

    在使用go的过程中,我们有时候会引入一些第三方库来使用,而通常的方式就是使用go get,可是这样的方式有一个非常严重的问题,假设第三方库更新了相关接口,非常有可能你就无法使用了,所以我们一套非常好地 ...

  6. Qt 3D研究(九):尝试第二边缘检测方法

    Qt 3D研究(九):尝试第二边缘检测方法 三维应用程序,通过FBO.将3D图像渲染成纹理,然后对渲染成的纹理进行图像处理,终于显示在屏幕上的.是风格化后的图案.上一次我使用了一种普通的图像处理方法: ...

  7. STL顺序容器【vector】【deque】【list】

    我们都知道,stl在集装箱船分为两类,订购集装箱和相关的容器. 顺序容器有三种即动态数组vector,双端队列deque,以及链表list (对csdn的文字排版严重吐槽.写好的版发表了就变了) 一: ...

  8. MySQL初步研究数据库

    我用的是环境Win7.开始学习PHP和MySQL,而买了这<Head First PHP & MySQL>,从能Head First Labs官网获得HeadFirst系列书籍的相 ...

  9. 超人学院Hadoop大数据资源共享

    超人学院Hadoop大数据资源共享-----数据结构与算法(java解密版) http://yunpan.cn/cw5avckz8fByJ   訪问password b0f8 很多其它精彩内容请关注: ...

  10. [C/C++]_[VS2010来源与UTF8中国字符串转码ANSI问题]

    现场: 1.思想vs设置源文件UTF8编码,代码中国串出现在它必须是utf8编码,不幸的是,,假定源代码将出现在中国字符串,在存储器中转码ANSI编码. Unicode(UTF8签名) 代码页(650 ...