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 ...
随机推荐
- iOS 9音频应用播放音频之音量设置与声道设置
iOS 9音频应用播放音频之音量设置与声道设置 iOS 9音频应用音量设置 音量又称响度.音强,是指人耳对所听到的声音大小强弱的主观感受,其客观评价尺度是声音的振幅大小.在iOS 9音频应用的应用中, ...
- python 什么是全局解释器锁GIL
什么是全局解释器锁GIL Python代码的执行由Python 虚拟机(也叫解释器主循环,CPython版本)来控制,Python 在设计之初就考虑到要在解释器的主循环中,同时只有一个线程在执行,即在 ...
- Dalvik 虚拟机 jvm 区别
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha dalvik 基于 寄存器, jvm基于 栈. 寄存器,编译时间会更短. dalvik ...
- [SDOI2014]数数 --- AC自动机 + 数位DP
[SDOI2014]数数 题目描述: 我们称一个正整数N是幸运数,当且仅当它的十进制表示中不包含数字串集合S中任意一个元素作为其子串. 例如当S=(22,333,0233)时,233是幸运数,2333 ...
- 斐波那契数列(python实现)
描述 一个斐波那契序列,F(0) = 0, F(1) = 1, F(n) = F(n-1) + F(n-2) (n>=2),根据n的值,计算斐波那契数F(n),其中0≤n≤1000. 输入 输入 ...
- [转] Spring MVC 4.1.3 + MyBatis 零基础搭建Web开发框架
首先感谢一下润和软件,指引我走上了Spring MVC Web开发的道路. 下面进入正题 搭建开发环境: Netbeans8.0.2 + MySql5.6 + JDK1.7 + tomcat8.0.1 ...
- iOS Masonry的使用需要注意的地方
自动布局最重要的是约束:UI元素间关系的数学表达式.约束包括尺寸.由优先级和阈值管理的相对位置.它们是添加剂,可能导致约束冲突 .约束不足造成布局无法确定 .这两种情况都会产生异常. 使用前:Auto ...
- Scrapy入门教程(转)
关键字:scrapy 入门教程 爬虫 Spider作者:http://www.cnblogs.com/txw1958/出处:http://www.cnblogs.com/txw1958/archive ...
- One switched-capacitor IC simultaneously inverts, doubles, and halves the input voltage.
- Inverted bipolar transistor doubles as a signal clamp
A number of circuits, such as level detectors and AM demodulators, benefit from a rectifier with a l ...