WCF --KnowType,ServiceKnownType,配置文件添加

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.在客户端和服务端上同时加相关配置


作用:实现继承,子类往父亲转换,从而实现多态



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值