PermanentEmployee.cs
namespace CompanyService
{
public class PermanentEmployee:Employee
{
public int Salary { get; set; }
}
}
ContractEmployee.cs
namespace EmployeeService
{
public class ContractEmployee : Employee
{
public double HourlyPay { get; set; }
public string HoursWorked { get; set; }
}
}
namespace EmployeeService
{
public class ContractEmployee : Employee
{
public double HourlyPay { get; set; }
public string HoursWorked { get; set; }
}
}
Employee.cs
namespace CompanyService
{
[KnownType(typeof(ParmanentEmployee))]
[KnownType(typeof(ContractEmployee))]
namespace CompanyService
{
[KnownType(typeof(ParmanentEmployee))]
[KnownType(typeof(ContractEmployee))]
[DataContract]
public class Employee
{
private int _id;
private string _name;
private string _gender;
[DataMember(Order = 1)]
public int Id
{
get { return _id; }
set { _id = value; }
}
[DataMember(Order = 2)]
public string Name
{
get { return _name; }
set { _name = value; }
}
[DataMember(Order = 3)]
public string Gender
{
get { return _gender; }
set { _gender = value; }
}
[DataMember(Order = 5)]
public Type Type { get; set; }
}
public enum Type
{
ParmanentEmployee = 1,
ContractEmployee = 2
}
}
There are 4 different ways to associate KnownTypes:
1.Use KnownType attribute on the base type. like this
[KnownType(typeof(ParmanentEmployee))]
[KnownType(typeof(ContractEmployee))]
[KnownType(typeof(ContractEmployee))]
[DataContract]
public class Employee{
............
}
2. Apply ServiceKnownType attribute
on the service contract.
[ServiceKnownType(typeof(ParmanentEmployee))]
[ServiceKnownType(typeof(ContractEmployee))]
[ServiceContract]
public interface ICompanyService
{
[OperationContract]
string SaveEmployee(Employee Employee);
}
3. Apply ServiceKnownType attribute on specific operation contracts. With this option, only the operation contracts that are decorated with ServiceKnownType attribute respect known types.
[ServiceKnownType(typeof(ContractEmployee))]
[ServiceContract]
public interface ICompanyService
{
[OperationContract]
string SaveEmployee(Employee Employee);
}
3. Apply ServiceKnownType attribute on specific operation contracts. With this option, only the operation contracts that are decorated with ServiceKnownType attribute respect known types.
[ServiceContract]
public interface ICompanyService
public interface ICompanyService
{
[ServiceKnownType(typeof(ParmanentEmployee))]
[ServiceKnownType(typeof(ContractEmployee))]
[OperationContract]
string SaveEmployee(Employee Employee);
}
[ServiceKnownType(typeof(ContractEmployee))]
[OperationContract]
string SaveEmployee(Employee Employee);
}
4. You can also specify known
types in the configuration file like this ...
<add type="CompanyService.Employee,
CompanyService, Version=1.0.1.0,
Culture=Neutral, PublicKeyToken=null">
<knownType type="CompanyService.ParmanentEmployee, CompanyService,
Version=1.0.0.0, Culture=Neutral, PublicKeyToken=null"/>
<knownType type="CompanyService.ContractEmployee, CompanyService,,
Version=1.0.0.0, Culture=Neutral, PublicKeyToken=null"/>
</add>
Culture=Neutral, PublicKeyToken=null">
<knownType type="CompanyService.ParmanentEmployee, CompanyService,
Version=1.0.0.0, Culture=Neutral, PublicKeyToken=null"/>
<knownType type="CompanyService.ContractEmployee, CompanyService,,
Version=1.0.0.0, Culture=Neutral, PublicKeyToken=null"/>
</add>
No comments:
Post a Comment