已知类型(Known types)允许在服务契约中使用多态的行为,在服务操作中暴露基本类型。
将已知类型(known types)相关到基本类型(基类类型)自身;特定操作;整个服务契约
采用属性声明或者配置的方式来实现

1、[KnownType]: 相关到基本类型(基类类型)自身

[DataContract(Namespace ="htttp://www.cnblogs.com/Charlesliu")]
[KnownType(typeof(GigInfo))]
[KnownType(typeof(PhotoLink))]
[KnownType(typeof(MP3Link))]
public class LinkItem
[DataContract(Namespace ="htttp://www.cnblogs.com/Charlesliu")]
public class GigInfo: LinkItem
[DataContract(Namespace ="htttp://www.cnblogs.com/Charlesliu")]
public class PhotoLink: LinkItem
[DataContract(Namespace ="htttp://www.cnblogs.com/Charlesliu")]
public class MP3Link: LinkItem

2、[ServiceKnownType]: 相关到整个服务契约

[ServiceContract(Name = "GigManagerContract", Namespace ="http://www.cnblogs.com/Charlesliu")]
[ServiceKnownType(typeof(GigInfo))]
[ServiceKnownType(typeof(PhotoLink))]
[ServiceKnownType(typeof(MP3Link))]
public interface IGigManagerService
{
[OperationContract]
void SaveLink(LinkItem item);
[OperationContract]
LinkItem GetLink(string id);
}

3、[ServiceKnownType]: 相关到特定操作

[ServiceContract(Name = "GigManagerContract", Namespace ="http://www.cnblogs.com/Charlesliu")]
public interface IGigManagerService
{
[OperationContract]
[ServiceKnownType(typeof(GigInfo))]
void SaveGig(LinkItem item);
[OperationContract]
[ServiceKnownType(typeof(GigInfo))]
LinkItem GetGig(string id);
}

  下面看一个具体的Demo,然后根据代码讲解:

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization; namespace ContentTypes
{
[DataContract(Namespace = "http://www.cnblogs.com/Charlesliu")]
public enum LinkItemCategories
{
[EnumMember]
Gig,
[EnumMember]
MP3,
[EnumMember]
Photo
} [DataContract(Name = "LinkItem_Contract", Namespace = "http://www.cnblogs.com/Charlesliu")]
[KnownType(typeof(GigInfo))]
[KnownType(typeof(PhotoLink))]
[KnownType(typeof(MP3Link))]
public class LinkItem
{
private long m_id;
private string m_title;
private string m_description;
private DateTime m_dateStart;
private DateTime m_dateEnd;
private string m_url;
private LinkItemCategories m_category; [DataMember(Name = "Id_Contract", IsRequired = false, Order = )]
public long Id
{
get { return m_id; }
set { m_id = value; }
} [DataMember(Name = "Title_Contract", IsRequired = true, Order = )]
public string Title
{
get { return m_title; }
set { m_title = value; }
} [DataMember(Name = "Description_Contract", IsRequired = true, Order = )]
public string Description
{
get { return m_description; }
set { m_description = value; }
} [DataMember(Name = "DateStart_Contract", IsRequired = true, Order = )]
public DateTime DateStart
{
get { return m_dateStart; }
set { m_dateStart = value; }
} [DataMember(Name = "DateEnd_Contract", IsRequired = false, Order = )]
public DateTime DateEnd
{
get { return m_dateEnd; }
set { m_dateEnd = value; }
} [DataMember(Name = "Url_Contract", IsRequired = false, Order = )]
public string Url
{
get { return m_url; }
set { m_url = value; }
} [DataMember(Name = "Category_Contract", IsRequired = false, Order = )]
public LinkItemCategories Category
{
get { return m_category; }
set { m_category = value; }
}
} [DataContract(Namespace = "http://www.cnblogs.com/Charlesliu")]
public class GigInfo : LinkItem
{
public GigInfo()
{
this.Category = LinkItemCategories.Gig;
}
} [DataContract(Namespace = "http://www.cnblogs.com/Charlesliu")]
public class MP3Link : LinkItem
{
public MP3Link()
{
this.Category = LinkItemCategories.MP3;
}
} [DataContract(Namespace = "http://www.cnblogs.com/Charlesliu")]
public class PhotoLink : LinkItem
{
public PhotoLink()
{
this.Category = LinkItemCategories.Photo;
}
}
}

  enum类型可以用[EnumMember]标记,本例中如果不加[KnownType]的相关标记,那么在客户端是看不到子类型的。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using ContentTypes; namespace WcfServiceLibraryDemo
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class GigManagerService : IGigManagerService
{
private LinkItem m_linkItem; #region IGigManager Members public void SaveGig(LinkItem item)
{
m_linkItem = item;
} public LinkItem GetGig()
{
return m_linkItem;
} #endregion
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using ContentTypes; namespace WcfServiceLibraryDemo
{
[ServiceContract(Name = "GigManagerServiceContract", Namespace = "http://www.cnblogs.com/Charlesliu", SessionMode = SessionMode.Required)]
public interface IGigManagerService
{
[OperationContract(Name = "SaveGig", Action = "http://www.cnblogs.com/Charlesliu/GigManagerServiceContract/SaveGig",
      ReplyAction = "http://www.cnblogs.com/Charlesliu/GigManagerServiceContract/SaveGigResponse")]
void SaveGig(LinkItem item); [OperationContract(Name = "GetGig", Action = "http://www.cnblogs.com/Charlesliu/GigManagerServiceContract/GetGig",
      ReplyAction = "http://www.cnblogs.com/Charlesliu/GigManagerServiceContract/GetGigResponse")]
LinkItem GetGig();
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WinTest.MyServiceReference; namespace WinTest
{
public partial class Form1 : Form
{
MyServiceReference.GigManagerServiceContractClient m_proxy = new WinTest.MyServiceReference.GigManagerServiceContractClient(); public Form1()
{
InitializeComponent();
} private void cmdSave_Click(object sender, EventArgs e)
{
MP3Link item = new MP3Link(); item.Id_Contract = int.Parse(this.txtId.Text);
item.Title_Contract = this.txtTitle.Text;
item.Description_Contract = this.txtDescription.Text;
item.DateStart_Contract = this.dtpStart.Value;
item.DateEnd_Contract = this.dtpEnd.Value;
item.Url_Contract = this.txtUrl.Text; m_proxy.SaveGig(item);
} private void cmdGet_Click(object sender, EventArgs e)
{
MP3Link item = (MP3Link)m_proxy.GetGig();
if (item != null)
{
this.txtId.Text = item.Id_Contract.ToString();
this.txtTitle.Text = item.Title_Contract;
this.txtDescription.Text = item.Description_Contract; if (item.DateStart_Contract != DateTime.MinValue)
this.dtpStart.Value = item.DateStart_Contract;
if (item.DateEnd_Contract != DateTime.MinValue)
this.dtpEnd.Value = item.DateEnd_Contract; this.txtUrl.Text = item.Url_Contract;
}
}
}
}

  客户端用的是MP3Link这个类,通过[KnownType]实现了WCF的多态。

WCF 之 已知类型(KnownType)的更多相关文章

  1. WCF数据契约代理和已知类型的使用

    using Bll; using System; using System.CodeDom; using System.Collections.Generic; using System.Collec ...

  2. WCF 已知类型和泛型解析程序 KnownType

    数据协定继承 已知类型和泛型解析程序 Juval Lowy 下载代码示例 自首次发布以来,Windows Communication Foundation (WCF) 开发人员便必须处理数据协定继承方 ...

  3. WCF技术剖析之十三:序列化过程中的已知类型(Known Type)

    原文:WCF技术剖析之十三:序列化过程中的已知类型(Known Type) [爱心链接:拯救一个25岁身患急性白血病的女孩[内有苏州电视台经济频道<天天山海经>为此录制的节目视频(苏州话) ...

  4. C# 序列化过程中的已知类型(Known Type)

    WCF下的序列化与反序列化解决的是数据在两种状态之间的相互转化:托管类型对象和XML.由于类型定义了对象的数据结构,所以无论对于序列化还是反序列化,都必须事先确定对象的类型.如果被序列化对象或者被反序 ...

  5. java基础 File与递归练习 使用文件过滤器筛选将指定文件夹下的小于200K的小文件获取并打印按层次打印(包括所有子文件夹的文件) 多层文件夹情况统计文件和文件夹的数量 统计已知类型的数量 未知类型的数量

    package com.swift.kuozhan; import java.io.File; import java.io.FileFilter; /*使用文件过滤器筛选将指定文件夹下的小于200K ...

  6. WCF中数据契约之已知类型的几种公开方式

    WCF中传输的数据不想传统的面向对象编程,它只传递了一些对象的属性,但是自身并不知道自己属于什么对象,所以,他没有子类和父类的概念,因而也就没有Is-a的关系,所以在WCF中,如果想维持这种继承关系, ...

  7. wcf已知类型 known type

    .服务契约的定义 /* Copyright (c) 2014 HaiHui Software Co., Ltd. All rights reserved * * Create by huanglc@h ...

  8. wcf可以返回的类型有哪些

    Windows Communication Foundation (WCF) 使用 DataContractSerializer 作为其默认的序列化引擎以将数据转换到 XML 并将 XML 转换回数据 ...

  9. 已知json类型根据类型封装集合

    1编写帮助类根绝url得到json public static string Post(string url) { string strURL = url; //创建一个HTTP请求 HttpWebR ...

随机推荐

  1. 在学习HTML——form表单中的label标签时的一点小体会

    在我啃了一遍书本之后,开始了在慕课看视频的过程,从最开始的HTML+CSS的基础课程看起,在第5-9小节讲到了form表单的label标签, 首先看一下慕课的讲解:  label 标签不会向用户呈现任 ...

  2. 【BZOJ 4031】 4031: [HEOI2015]小Z的房间 (Matrix-Tree Theorem)

    4031: [HEOI2015]小Z的房间 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 1089  Solved: 533 Description ...

  3. [BZOJ 3720][JZYZOJ 2016]gty的妹子树 强制在线 树分块/树套树

    jzyzoj的p2016 先码着,强制在线的树分块或者树套树?关键是我树分块还在入门阶段树套树完全不会啊摔   http://blog.csdn.net/jiangyuze831/article/de ...

  4. BZOJ3619 [Zjoi2014]璀灿光华 构造+dfs

    题意:有一个\(a^3\)个小正方体组成的大正方体,其中有n个正方体会向上下左右前后六个方向中的一个发出光,正方体是透光的,被照亮的正方体有个美丽值\(g_{i}\),给出正方体的相邻关系,问美丽值之 ...

  5. 【tarjan+拓扑】BZOJ3887-[Usaco2015 Jan]Grass Cownoisseur

    [题目大意] 给一个有向图,然后选一条路径起点终点都为1的路径出来,有一次机会可以沿某条边逆方向走,问最多有多少个点可以被经过?(一个点在路径中无论出现多少正整数次对答案的贡献均为1) [思路] 首先 ...

  6. Java Queue的测试

    上传图片没上去,提交的时候已经结束 代码链接

  7. django之cookie、session和ajax

    1 Cookie cookie是什么? 保存在客户端浏览器上的键值对   {k:v} cookie依附在请求头或响应头中出现 发送请求时,会自动携带自己网站的cookie 应用: 实现登录 投票 1. ...

  8. 在活动之间切换(隐式Intent)

    实验名称:在活动之间切换 实验现象:在主活动中点击button1可以进入下一个活动 使用技术:隐式Intent 步骤: 1.创建一个项目,加载布局并在布局中添加一个button 部分截图未截,直接Ne ...

  9. Android中的数据存储(二):文件存储 2017-05-25 08:16 35人阅读 评论(0) 收藏

    文件存储 这是本人(菜鸟)学习android数据存储时接触的有关文件存储的知识以及本人自己写的简单地demo,为初学者学习和使用文件存储提供一些帮助.. 如果有需要查看SharedPreference ...

  10. mui 页面滚动解决方案

    默认情况下mui 页面不能滚动,以下为解决方案: 1. mui('.mui-scroll-wrapper').scroll({  deceleration: 0.0005 //flick 减速系数,系 ...