this is my first WCF service. I defined a response message that derives from a Dictionary like this:
[CollectionDataContract(ItemName = "Product", KeyName = "ProductNumber", ValueName = "ProductName")]
public class GetAvailableProductsResponse : Dictionary<string, string> { }
When I try to run the following code in a service operation it throws an exception for not being able to cast:
Dictionary<string, string> result = new Dictionary<string, string>();
GetAvailableProductsResponse responseMsg = (GetAvailableProductsResponse)result;
In reality I don’t instantiate a new Dictionary but I’m calling a business object which returns a Dictionary so I need to cast this to the response message somehow.
This might be an issue of casting a Dictionary in general rather than a WCF specific question, isn’t it?
Many thanks in advance!
You can not cast an instance of the base class object to derived type, if it is not an instance of the derived. You can put the dictionary on the response as a property:
and
EDIT:
if you prefer to keep the inheritance you have to specify a constructor for the class: