KnownTypes of Data Contracts in WCF

A WCF Data Contract describes the structure or type of data being exchanged between the client and server. In some scenarios the type of data is not known between the client and server. For example the data the service is sending back to the client is not the actual data of the contract but a derived type of it. Then how can we expect the DataContractserializer to determine this relationship? Until we say this relationship to the DataContractSerializer, this deserialization issue will not be resolved. Is my explanation still not good enough to explain the problem? Let me explain it with the following source code.

  1. public interface IOnlineShoppingService  
  2. {  
  3.     [OperationContract(Name = "StockAvailability")]  
  4.     bool IsStockAvailable(string sModelNumber);  
  5.   
  6.     [OperationContract(Name = "CalculatePrice", ProtectionLevel = System.Net.Security.ProtectionLevel.None)]  
  7.     double CalculatePrice(string sModelNumber, int iQuantity, string sDeliveryLocation,int iDiscountCouponNum);  
  8.   
  9.     [OperationContract(Name = "GetWholeSaleDealerInfo")]  
  10.     WholeSaleDealersInfo GetwholeSaleDealerInfo(string sFilterByCity);   
  11. }           
  12. [DataContract]  
  13. public class WholeSaleDealersInfoDetailed:WholeSaleDealersInfo  
  14. {  
  15.   //Member declaration  
  16. }  
  17. public WholeSaleDealersInfo GetwholeSaleDealerInfo(string sFilterByCity) 
    {  
  18.     switch (sFilterByCity)  
  19.     {  
  20.          case "Chennai":  
  21.          if (sFilterByCity.Equals("Chennai"))  
  22.          {  
  23.                WholeSaleDealersInfoDetailed wsDealerDetailed = new  WholeSaleDealersInfoDetailed();  
  24.                wsDealerDetailed.DealerName = "Rameshkartik";  
  25.                wsDealerDetailed.DealerLocation = "Chennai";  
  26.                wsDealerDetailed.DealerSince = DateTime.Now;  
  27.                wsDealerDetailed.DealerEmail = "[email protected]";  
  28.                wsDealerDetailed.DealerAddress = "North Street,Chennai";  
  29.                return wsDealerDetailed;  
  30.           }  
  31.           break;  
  32.               return null;  
  33.      }  
  34.      return null;  

In the preceding service interface you can find a method called GetWholeSaleDealerInfo that returns the type WholeSaleDealersInfo, but the DataContractSerializer will expect only the type WholeSaleDealersInfo, not the WholeSaleDealersInfoDetailed. This is the problem, during the deserialization the actual contract will not be coming, but it is a derived one. So what is the solution for this? As discussed earlier, Known Types in a Data Contract is the right way to solve this. You can use Known Types at the various levels based on your needs.

  • Global Level
  • Service Contract Level
  • Operation Contract Level

Global Level

  1. [KnownType(typeof(WholeSaleDealersInfoDetailed))]  
  2. [DataContract]  
  3. public class WholeSaleDealersInfo  
  4. {  

In the preceding example, you can see that the KnownType attribute is used at the Data Contract level. It means that the type is globally known to all the service contracts and operation contracts wherever the WholeSaleDealersInfo type is used.

Service Contract Level

  1. [ServiceKnownType(typeof(WholeSaleDealersInfoDetailed))]  
  2. public interface IOnlineShoppingService  
  3. {  
  4.    [OperationContract(Name = "GetWholeSaleDealerInfo")]  
  5.    WholeSaleDealersInfo GetwholeSaleDealerInfo(string sFilterByCity);  

If you want to use this derived type as a known type for a specific service, please use the preceding syntax Service Known Type.

Operation Contract Level

  1. public interface IOnlineShoppingService  
  2. {  
  3.     [ServiceKnownType(typeof(WholeSaleDealersInfoDetailed))]  
  4.     [OperationContract(Name = "GetWholeSaleDealerInfo")]  
  5.     WholeSaleDealersInfo GetwholeSaleDealerInfo(string sFilterByCity);  
  6. }  

If you want tp use the derived type as known for a specific method, please place the serviceknown type attribute on the service method as above.

We can do the same in the web.config as well.

Attachment

Refer to the code attachment for further details.

Summary

Known Types can be used at various levels, there are Global Level, Service Contract Level and Data Contract Level.


Similar Articles