当前位置: 首页 > news >正文

合肥市建设网站西安市十大it培训机构

合肥市建设网站,西安市十大it培训机构,做网站费是多少,网站设计预算准备技术#xff1a; 1.C#基础知识 2.了解WCF基础知识 在正常的c#开发中我们是允许用子类去替换基类的#xff0c;这也是所谓的替换原则。但是我们在WCF中确不能用数据契约的子类来替换父类的#xff0c;因为这中间存在一个序列化的问题。举个例子#xff1a; 我们有数据契…准备技术      1.C#基础知识      2.了解WCF基础知识          在正常的c#开发中我们是允许用子类去替换基类的这也是所谓的替换原则。但是我们在WCF中确不能用数据契约的子类来替换父类的因为这中间存在一个序列化的问题。举个例子      我们有数据契约           [DataContract]           class Employee{...}      服务契约中           [ServiceContract]           interface IEmployeeManager           {                [OperationContract]                void AddEmployee(Employee employee);           }      然后我们在客户端的代理中就算有类Intern继承于           [DataContract]           class Intern:Employee{...}      然后再客户端调用时           proxy.AddEmployee(new Intern())是会出错的。因为在服务器端无法识别Intern对象因为他无法去反序列化Intern成Employee对象(WCF序列化)。                  WCF提供给我们了一个解决的办法就是使用KnownTypeAttribute特性在基类上标识对应的子类就可以了。KnownTypeAttribute特性可以使用在Struct跟Class上。示例           [DataContract]           [KnownType(typeof(Customer))]           class Employee{...}                      [DataContract]           class Intern:Employee{...} 这样我们就在所有的契约跟操作上能跨越所有的服务和终结点允许服务接受子类。但是这样会遇到一个问题我们不能去特定的指定某一个服务操作所以KnownType的缺陷就是范围过于广泛。WCF提供了另外一个Attribute--ServiceKnownType.   ServiceKnownType 特性      KnownType只能应用在数据契约的基类上而ServiceKnownType可以在Interface、Method、Class上标识。看一个示例Employee.cs   Codeusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ServiceModel;using System.Runtime.Serialization;namespace HenllyeeDataContract {     [DataContract]     public class Employee     {         #region Fields         private string _name;         private int _age;         #endregion         #region Properties         /// summary         /// The employees name         /// /summary        [DataMember(Order0)]         public string Name         {             get              {                 return this._name;             }             set             {                 this._name  value;             }         }         /// summary         /// The employees age         /// /summary        [DataMember(Order1)]         public int Age         {             get             {                 return this._age;             }             set             {                 this._age  value;             }         }         #endregion     }     [DataContract]     public class Intern : Employee     {         private int _internship;         /// summary         /// The interns working days         /// /summary        [DataMember]         public int Internship         {             get             {                 return this._internship;             }             set             {                 this._internship  value;             }         }     } }   在数据契约中我们并没有去指定KnownType我们在服务契约的操作上去标识ServiceKnownType特性EmployeeManage.cs:     Codeusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ServiceModel;using System.Runtime.Serialization;namespace HenllyeeServiceContract {     [ServiceContract]     public interface IEmployeeManage     {         [OperationContract]         [ServiceKnownType(typeof(HenllyeeDataContract.Intern))]         void AddEmployee(HenllyeeDataContract.Employee emlpoyee);         [OperationContract]         [ServiceKnownType(typeof(HenllyeeDataContract.Intern))]         HenllyeeDataContract.Employee GetEmployee();     }     [ServiceBehavior(InstanceContextModeInstanceContextMode.Single)]     public class EmployeeManage : IEmployeeManage     {         private HenllyeeDataContract.Employee _employee;         /// summary         /// Set employee         /// /summary         /// param nameemlpoyeethe employees object/param        public void AddEmployee(HenllyeeDataContract.Employee emlpoyee)         {             this._employee  emlpoyee;         }         /// summary         /// Get a employee         /// /summary         /// returns/returns        public HenllyeeDataContract.Employee GetEmployee()         {             return this._employee;         }     } }   在客户端我们调用     Codeusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Client {     class Program     {         static void Main(string[] args)         {             EmployeeService.EmployeeManageClient proxy  new Client.EmployeeService.EmployeeManageClient();             EmployeeService.Intern intern  new Client.EmployeeService.Intern();             intern.Age  22;             intern.Name  Henllyee Cui;             intern.Internship  120;             proxy.AddEmployee(intern);             EmployeeService.Employee internOut  proxy.GetEmployee();             Console.Write(The Employee Name:{0}\nAge:{1}\n,                 internOut.Name,                 internOut.Age                 );             Console.Read();         }     } } 运行后       ServiceKnownType特性也可以表示在数据契约的类上那么就会应用到整个数据契约中操作上如           [ServiceContract]           [ServiceDataContract(typeof(HenllyeeDataContract.Intern))]           public interface IEmployeeManage{...}      那么IEmployeeManage服务契约跟其所有的操作都可以接受Intern这个子类. 已知类型与接口      数据契约DataContract只能标识在class 或者struct上但是数据契约的基类可以是接口,但是我们在服务契约的时候要去用ServiceKnownType特性去指定确切的数据类型。如:      interface IEmployee{...}      [DataContract]      class Intern:IEmployee{...} 服务契约中      [ServiceContract]      [ServiceKnownType(typeof(Intern))]      interface IEmployeeManage      {           [OperationContract]           void AdddEmployee(IEmployee employee);      } 要注意的一点就是我们不能把KnownType特性应用到基接口上因为客服端导出的元数据是不能包含接口本身的。 [代码下载] 作者Henllyee Cui出处 http://henllyee.cnblogs.com/ 本文版权归作者和博客园共有欢迎转载但未经作者同意必须保留此段声明。 转载于:https://www.cnblogs.com/Henllyee/archive/2008/08/26/1276995.html
http://www.pierceye.com/news/963918/

相关文章:

  • 南宁建站程序成都新线加网站建设
  • 用微软雅黑做网站可以吗wordpress游客发帖插件
  • 网站备案怎样提交管局网页电商设计
  • 郑州华恩科技做网站怎么样网络竞价推广托管公司
  • 都江堰住房和城乡建设厅网站哈尔滨网站建设方案维护
  • 九江网站网站建设原始传奇经典复古
  • 宽屏营销型网站源码安微省住房和城乡建设厅网站
  • 做暖视频网站免费搜索引擎营销的模式有
  • 网站建设需要的条件榆林北京网站建设
  • 分类信息网站推广的意义wordpress安装教程wamp
  • 免费自助建站全系统建设银行永泰支行网站
  • 建网络商城网站如何开公司做网站
  • 长春网站制作色块网站设计
  • 通明建设网站网站怎么黑
  • 学校网站怎么查询录取html5浏览器
  • 网站开发 技术问题页面模版 公众号
  • 宜阳县网站建设网络运营者应当为()
  • 做网站的人能看到浏览的人的信息吗青岛市最大的网络公司是哪里
  • 网站建设 千助黄冈网站推广软件ios
  • 网站制作视频教程全报价单模板表格
  • 包头市做网站哪个wordpress nginx伪静态规则
  • 深圳建网站哪家好专业网站建设服务包括
  • 做静态头像网站网站做百度竞价利于百度优化
  • 网站建设属于税收建立网站后怎样收费
  • 婚礼礼网站如何做的云南推广公司
  • 模板建站流程seo优化推广
  • 龙岗网络推广深圳网站建设我的世界的头怎么做视频网站
  • 高明网站建设首选公司深圳市建设安监站网站
  • 宁波网站建设科技有限公司注册开发公司
  • 什么网站有女人跟狗做的和平东路网站建设