In WCF if we have classes related by inheritence, WCF service generally accept and returns the basse type.if you expect the service to accept and return inherited types than use knownType attribute.
[KnownType(typeof(PartTimeEmployee))]
[KnownType(typeof(FullTimeEmployee))]
[DataContract]
public class Employee
{
[DataMember]
public int empID { get; set; }
[DataMember]
public string empName { get; set; }
[DataMember]
public string empAddress { get; set; }
}
public class PartTimeEmployee : Employee
{
public int Salary { get; set; }
}
public class FullTimeEmployee : Employee
{
public int HourWorked { get; set; }
public int HourPaid { get; set; }
}
If you remove [KnownType] from above code, in wsdl file metadata for classes FullTimeEmployee and PartTimeEmployee will not exist.
You can't use drived classes PartTimeEmployee and FullTimeEmployee in client side untill you don't use [KnownType] attribute.
Client Code :
protected void btn_getEmp_Click(object sender, EventArgs e)
{
ServiceReference1.Service1Client ocjClient = new ServiceReference1.Service1Client();
ServiceReference1.Employee obj = new ServiceReference1.Employee();
obj = ocjClient.getEmoloyee(1);
int hourPaid = ((ServiceReference1.FullTimeEmployee)obj).HourPaid;
int hourworked = ((ServiceReference1.FullTimeEmployee)obj).HourWorked;
ServiceReference1.Employee obj2 = new ServiceReference1.Employee();
obj2 = ocjClient.getEmoloyee(2);
int salary = ((ServiceReference1.PartTimeEmployee)obj).Salary;
}
Above client code will give error if you remove [KnownType] from employee class.
Related Post :
Different-option-of-hosting-wcf-service
Binding-in-wcf-choosing-right-wcf
Exchanging-metadata-in-wcf
Some-interesting-facts-about-data-contract
Knowntype-attribute-in-wcf
Associating-knowntype-in-wcf
Message-contract-in-wcf
Exception-handling-in-wcf
Exception Handling in WCF - SOAP Fault in WCF
Exception Handling in WCF - Unhandled Exception in WCF
Exception Handling in WCF - Creating and Throwing Strongly Typed SOAP Fault
[KnownType(typeof(PartTimeEmployee))]
[KnownType(typeof(FullTimeEmployee))]
[DataContract]
public class Employee
{
[DataMember]
public int empID { get; set; }
[DataMember]
public string empName { get; set; }
[DataMember]
public string empAddress { get; set; }
}
public class PartTimeEmployee : Employee
{
public int Salary { get; set; }
}
public class FullTimeEmployee : Employee
{
public int HourWorked { get; set; }
public int HourPaid { get; set; }
}
If you remove [KnownType] from above code, in wsdl file metadata for classes FullTimeEmployee and PartTimeEmployee will not exist.
You can't use drived classes PartTimeEmployee and FullTimeEmployee in client side untill you don't use [KnownType] attribute.
Client Code :
protected void btn_getEmp_Click(object sender, EventArgs e)
{
ServiceReference1.Service1Client ocjClient = new ServiceReference1.Service1Client();
ServiceReference1.Employee obj = new ServiceReference1.Employee();
obj = ocjClient.getEmoloyee(1);
int hourPaid = ((ServiceReference1.FullTimeEmployee)obj).HourPaid;
int hourworked = ((ServiceReference1.FullTimeEmployee)obj).HourWorked;
ServiceReference1.Employee obj2 = new ServiceReference1.Employee();
obj2 = ocjClient.getEmoloyee(2);
int salary = ((ServiceReference1.PartTimeEmployee)obj).Salary;
}
Above client code will give error if you remove [KnownType] from employee class.
Related Post :
Different-option-of-hosting-wcf-service
Binding-in-wcf-choosing-right-wcf
Exchanging-metadata-in-wcf
Some-interesting-facts-about-data-contract
Knowntype-attribute-in-wcf
Associating-knowntype-in-wcf
Message-contract-in-wcf
Exception-handling-in-wcf
Exception Handling in WCF - SOAP Fault in WCF
Exception Handling in WCF - Unhandled Exception in WCF
Exception Handling in WCF - Creating and Throwing Strongly Typed SOAP Fault
No comments:
Post a Comment