WCF 之 已知类型(KnownType)
已知类型(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)的更多相关文章
- WCF数据契约代理和已知类型的使用
using Bll; using System; using System.CodeDom; using System.Collections.Generic; using System.Collec ...
- WCF 已知类型和泛型解析程序 KnownType
数据协定继承 已知类型和泛型解析程序 Juval Lowy 下载代码示例 自首次发布以来,Windows Communication Foundation (WCF) 开发人员便必须处理数据协定继承方 ...
- WCF技术剖析之十三:序列化过程中的已知类型(Known Type)
原文:WCF技术剖析之十三:序列化过程中的已知类型(Known Type) [爱心链接:拯救一个25岁身患急性白血病的女孩[内有苏州电视台经济频道<天天山海经>为此录制的节目视频(苏州话) ...
- C# 序列化过程中的已知类型(Known Type)
WCF下的序列化与反序列化解决的是数据在两种状态之间的相互转化:托管类型对象和XML.由于类型定义了对象的数据结构,所以无论对于序列化还是反序列化,都必须事先确定对象的类型.如果被序列化对象或者被反序 ...
- java基础 File与递归练习 使用文件过滤器筛选将指定文件夹下的小于200K的小文件获取并打印按层次打印(包括所有子文件夹的文件) 多层文件夹情况统计文件和文件夹的数量 统计已知类型的数量 未知类型的数量
package com.swift.kuozhan; import java.io.File; import java.io.FileFilter; /*使用文件过滤器筛选将指定文件夹下的小于200K ...
- WCF中数据契约之已知类型的几种公开方式
WCF中传输的数据不想传统的面向对象编程,它只传递了一些对象的属性,但是自身并不知道自己属于什么对象,所以,他没有子类和父类的概念,因而也就没有Is-a的关系,所以在WCF中,如果想维持这种继承关系, ...
- wcf已知类型 known type
.服务契约的定义 /* Copyright (c) 2014 HaiHui Software Co., Ltd. All rights reserved * * Create by huanglc@h ...
- wcf可以返回的类型有哪些
Windows Communication Foundation (WCF) 使用 DataContractSerializer 作为其默认的序列化引擎以将数据转换到 XML 并将 XML 转换回数据 ...
- 已知json类型根据类型封装集合
1编写帮助类根绝url得到json public static string Post(string url) { string strURL = url; //创建一个HTTP请求 HttpWebR ...
随机推荐
- Redis学习篇(八)之连接相关
PING 测试客户端和服务器之间的连接是否有效,有效返回PONG ECHO 打印特定的信息, 如: ECHO 'HELLO WORLD' QUIT/EXIT 断开当前客户端与服务器之间的连接,可以重连 ...
- HDU 5514 Frogs(容斥原理)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5514 [题目大意] m个石子围成一圈,标号为0~m-1,现在有n只青蛙,每只每次跳a[i]个石子, ...
- Centos7 下mysql大小写敏感问题
在Centos7 下mysql大小写敏感问题,会导致程序运行时找不到对应的表. 解决办法: 第一步:编辑/etc/my.cnf文件,在[mysqld]节下 添加 lower_case_table_na ...
- ACM -- 算法小结(十)素数的两种打表法
素数的两种打表法 下面介绍两种素数打表法,由于是两年前留下的笔记,所以没有原创链接~~ @_@!! 第一种疯狂打表法: #include<stdio.h> #include<math ...
- linux基础命令学习(四)用户与群组
一.linux用户账号的管理 linux用户账号的管理主要包括用户添加.用户删除.用户修改. 添加用户账号就是在系统创建一个新账号,然后为新账号分为用户号.用户组.主目录和登录Shell等资源. 刚添 ...
- jquery 常用获取值得方法汇总
jquery取radio单选按钮的值$("input[name='items']:checked").val();jquery radio取值,checkbox取值,select取 ...
- POJ 1286 Necklace of Beads(Polya简单应用)
Necklace of Beads 大意:3种颜色的珠子,n个串在一起,旋转变换跟反转变换假设同样就算是同一种,问会有多少种不同的组合. 思路:正规学Polya的第一道题,在楠神的带领下,理解的还算挺 ...
- Dual-polarity supply provides ±12V from one IC
LT1961 升压型稳压器造就了兼具升压和降压能力的扁平状SEPIC Transformerless dc/dc converter produces bipolar outputs Well-reg ...
- Linux/drivers/usb/serial/ftdi_sio.c
Linux/drivers/usb/serial/ftdi_sio.h /* 2 * Driver definitions for the FTDI USB Single Port Serial Co ...
- jquery开发之第一个程序
前一段时间学习了js和css.可是发现好多的程序里面都用到了jquery当时本来想着先吧js弄熟了 再搞这个.后来发现不行,好多的程序好像是有益和自己为难似的,所以我决定接下来认认真真的把jquery ...