Different ways of associating known types in wcf

  

  本篇博客讲解一下Known Types这个属性标签,如果翻译过来的话,就叫做已知类型吧!这个标签允许在服务契约中实现多态的行为。有四种方式实现,下面来分别演示一下。


  1.User KnownType attribute on the base type.

      这种方式是针对于全局的,如果采用这种方式后,全局的服务契约和所有的方法契约将都会默认加上此种方式。

      员工类

    

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
namespace EmployeeService
{
    [KnownType(typeof(FullTimeEmployee))]
   [DataContract(Namespace="http://Employee/employee")]
    public class Employee
    {
        private int _id;
        private string _name;
        private string _gender;
        private DateTime _dateOfBirth;

        [DataMember(Name="id",Order=1)]
        public int ID
        {
            get { return _id; }
            set { _id = value; }
        }


        [DataMember(Name = "Name", Order = 2)]
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }


        [DataMember(Name = "Gender", Order = 2)]
        public string Gender
        {
            get { return _gender; }
            set { _gender = value; }
        }

        public DateTime DateOfBirth
        {
            set { _dateOfBirth = value; }
            get { return _dateOfBirth; }
        }

    }
}


  全职员工类,继承自基类

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
namespace EmployeeService
{
    [DataContract]
    public class FullTimeEmployee:Employee
    {
        public int AnnualSalary { set; get; }
    }
}

  上述中FullTimeEmployee继承自Employee,此时如果在Employee上加入 [KnownType(typeof(FullTimeEmployee))]标签后,客户端也就会看到FullTimeEmployee


  2.Apply ServiceKnownType attribute on the service contract.

     第二种方式是,直接把此标签添加到服务契约上,那么此时,只有此服务契约共享该契约实体

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace EmployeeService
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IEmployeeService”。
    [ServiceKnownType(typeof(FullTimeEmployee))]
    [ServiceContract]
    public interface IEmployeeService
    {
       [OperationContract]
        Employee GetEmployee(int Id);

        [OperationContract]
        void SaveEmployee(Employee Employee);
    }
}


  3. Apply ServiceKnownType attribute on specific operation contracts.

    此种方式是把已知类型添加到服务契约的方法上,那么结果可想而知,只有该服务契约的某个方法上才可以共享契约对象。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace EmployeeService
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IEmployeeService”。
   
    [ServiceContract]
    public interface IEmployeeService
    {
        [ServiceKnownType(typeof(FullTimeEmployee))]
       [OperationContract]
        Employee GetEmployee(int Id);

        [OperationContract]
        void SaveEmployee(Employee Employee);
    }
}


  4.Specifiy known types in the configuration file

      最后一种方式是直接在配置文件中进行配置,由于是全局的与第一种一样,所以就不在此讲述了。



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值