I am using a DI pattern with a set of WCF services, and Unity is acting as my IoC container. I have completed unit and functional testing from the service down and everything is working as I would expect. I am now testing the endpoints themselves and I have run into an issue with serializing my results.
My service looks something like this:
public class ProcessingService : IProcessingService
{
private IOrderService orderService;
public ProcessingService(IOrderService orderService)
{
this.orderService = orderService;
}
[WebInvoke(Method = "Get", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public IOrder CreateNewOrder(string venNum, OrderType orderType, string orderDescription, string createdBy)
{
return orderService.CreateNewOrder(venNum, orderType, orderDescription, createdBy);
}
}
orderService.CreateNewOrder
ultimately returns a complex object that looks similar to this
internal partial class Order : IOrder
{
[DataMember]
public int VenNum { get; set; }
[DataMember]
public string Description { get; set; }
[DataMember]
public IList<string> RuntimeErrorMessages { get; set; }
[DataMember]
public IList<IOrderDetail> OrderDetails { get; set; }
}
This generates the exception:
"
<type>
is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer."
This error makes sense since you can't possibly serialize or deserialize an interface. Further I can get this error to go away by changing the return type of my service method to
public Order CreateNewOrder(...)
I don't want to expose my concrete objects though, so I am at a bit of an impasse here. In the past I can recall using Json.Net's JsonConverter class to serialize and deserialize objects like this although I wasn't using DI at that time and it was a little bit easier. So how do I go about serializing my complex types without exposing my concrete types? The exception mentions using a DataContractSerializer but it appears that I would have to write one Serializer for every complex type I want to return and that isn't practical.
Suggestions are greatly appreciated!