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.在客户端和服务端上同时加相关配置
作用:实现继承,子类往父亲转换,从而实现多态