WCF --KnowType,ServiceKnownType,配置文件添加
来源:互联网 发布:anaconda2 安装 linux 编辑:程序博客网 时间:2024/05/17 06:38
KnowType
指定 DataContractSerializer 在序列化和反序列化给定类型时应识别的类型
ServiceKnownType
指定序列化或反序列化时服务要使用的已知类型
父类:
namespace Contracts.Model{ //[KnownType(typeof(ChildModel))] public class ParentModel { public string PName { get; set; } public int Age { get; set; } }}派生类:
namespace Contracts.Model{ public class ChildModel:ParentModel { public string CName { get; set; } }}服务契约:
using System.Collections.Generic;using System.ServiceModel;using Contracts.Model;namespace Contracts{ [ServiceContract] //[ServiceKnownType(typeof(ChildModel))] public interface IContract { [OperationContract] //[ServiceKnownType(typeof(ChildModel))] ParentModel ChildToParent(); }
服务端实现:
using System.Collections.Generic;using Contracts;using Contracts.Model;namespace Serices{ public class ServiceImpl : IContract { public ParentModel ChildToParent() { ChildModel cinfo = new ChildModel(); cinfo.Age = 123; cinfo.CName = "子类"; cinfo.PName = "P1"; return cinfo; } }}
服务端config
<?xml version="1.0" encoding="utf-8" ?><configuration> <system.serviceModel> <bindings> <netTcpBinding> <binding name="CatorNetTcpBing" receiveTimeout ="00:00:40" > <!--<reliableSession ordered="true" inactivityTimeout="00:00:10" enabled="false" />--> </binding> </netTcpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior name="metadataBehavior"> <serviceDebug includeExceptionDetailInFaults ="false"/> </behavior> </serviceBehaviors> </behaviors> <services> <service behaviorConfiguration="metadataBehavior" name="Serices.ServiceImpl"> <endpoint address="net.tcp://192.168.1.204:9300/IContract" binding="netTcpBinding" contract="Contracts.IContract" bindingConfiguration ="CatorNetTcpBing" /> </service> </services> </system.serviceModel> <system.diagnostics> <trace autoflush="true" /> <sources> <source name="System.ServiceModel" switchValue="Error" propagateActivity="true"> <listeners> <add name="xwt" type="System.Diagnostics.XmlWriterTraceListener" initializeData="MyListener.log" /> </listeners> </source> </sources> </system.diagnostics> <!--<system.runtime.serialization> <dataContractSerializer> <declaredTypes> <add type="Contracts.Model.ParentModel, Contracts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"> <knownType type="Contracts.Model.ChildModel, Contracts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /> </add> </declaredTypes> </dataContractSerializer> </system.runtime.serialization>--></configuration>
客户端调用:
using System.Collections.Generic;using System.ServiceModel;using Contracts.Model;namespace Contracts.Client{ public class ClientImpl:ClientBase<IContract>,IContract { public ParentModel ChildToParent() { return Channel.ChildToParent(); } }}
客户端CONFIG
<?xml version="1.0" encoding="utf-8"?><configuration> <system.serviceModel> <bindings> <netTcpBinding> <binding name="CatorNetTcpBing" receiveTimeout="00:00:40" /> </netTcpBinding> </bindings> <client> <endpoint address="net.tcp://192.168.1.204:9300/IContract" binding="netTcpBinding" bindingConfiguration="CatorNetTcpBing" contract="Contracts.IContract" name="" kind="" endpointConfiguration=""> </endpoint> </client> <services /> </system.serviceModel> <!--<system.runtime.serialization> <dataContractSerializer> <declaredTypes> <add type="Contracts.Model.ParentModel, Contracts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"> <knownType type="Contracts.Model.ChildModel, Contracts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /> </add> </declaredTypes> </dataContractSerializer> </system.runtime.serialization>--></configuration>
1.在父类CLASS上写[KnownType(typeof(ChildModel))]
2.在服务接口(interface)或定义的某个规则上加[ServiceKnownType(typeof(ChildModel))]
3.在客户端和服务端上同时加相关配置
作用:实现继承,子类往父亲转换,从而实现多态
0 0
- WCF --KnowType,ServiceKnownType,配置文件添加
- WCF 动态实现数据契约 ServiceKnownType
- WCF配置文件
- wcf 配置文件
- WCF 配置文件
- WCF配置文件
- wcf 配置文件
- WCF配置文件
- WCF 配置文件
- WCF配置文件
- wcf配置文件
- WCF 配置文件
- WCF配置文件
- WCF配置文件
- WCF配置文件
- 化零为整WCF(2) - 契约Contract(ServiceContract、OperationContract、DataContract、ServiceKnownType和DataMember)
- 化零为整WCF(2) - 契约Contract(ServiceContract、OperationContract、DataContract、ServiceKnownType和DataMember)
- WCF配置文件设置
- sleep和wait的区别
- 页面有的参数,在action中没有报错
- hibernate学习记录5
- OC语言学习——继承和多态的一些随笔记
- 各方都说过她的托管人
- WCF --KnowType,ServiceKnownType,配置文件添加
- Java奇葩笔试题
- Cocos2d-x win7 + vs2010 配置图文详解
- 琵琶行·并序--白居易
- C++11中值得关注的几大变化
- std::function与std::bind 函数指针
- Objective-C 总结
- Git创建repository,以及clone,提交一个repository
- Linux进程的睡眠和唤醒