You create a Windows Communication Foundation service by using Microsoft .NET Framework 3.5. The service contains the following code segment.
[DataContract]
public class Person
{
??
}
[DataContract]
public class Customer : Person
{
??
}
You need to create a service contract that meets the following requirements:
The service contract must have an operation contract named GetPerson that returns an object of type Person.
The GetPerson operation must be able to return an object of type Customer.
Which code segment should you use?
A.
[ServiceContract]
[ServiceKnownType(“GetPerson”)]
public interface IMyService
{
[OperationContract]
Person GetPerson();
}
B.
[ServiceContract]
public interface IMyService
{
[OperationContract]
[ServiceKnownType(“Customer”)]
Person GetPerson();}
C.
[ServiceContract]
[ServiceKnownType(typeof(Customer))]
public interface IMyService
{
[OperationContract]
Person GetPerson();
}
D.
[ServiceContract]
[ServiceKnownType(“GetPerson”,typeof(Customer))]
public interface IMyService
{
[OperationContract]
Person GetPerson();
}
Explanation:
Since Customer is a subclass of Person, in order for objects to be deserialized back to Customer objects rather than Person objects, the KnownType attribute must be used to decorate either the Person DataContract or the ServiceContract.