I have a WCF service that has methods to return IEnumerable<T> collections of objects, and also a complex type, OrganizationCollection, which has a few properties, which are each an IEnumerable<T> of different types. I believe I’ve set up my Service Contract correctly, and defined my types DataContract / DataMember correctly. The method that returns an OrganizationCollection fails with an exception. I know that the method functions since I have unit and integration tests that test it. It’s only when running with a live and deployed service that it fails. I’ve even specified the type as a ServiceKnownType, but to no avail. What do I need to do to configure the service to be able to return complex types like OrganizationCollection?
Note: The WCF service is running with basicHttpBinding, and is housed in a ServiceHost in a Windows Service.
[System.ServiceModel.CommunicationException] {“An error occurred while receiving the HTTP response to http://localhost:8799/MyService. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.”} System.ServiceModel.CommunicationException
[ServiceBehavior(IncludeExceptionDetailInFaults = true, AutomaticSessionShutdown = false, InstanceContextMode = InstanceContextMode.Single)]
[ServiceKnownType(typeof(OrganizationCollection))]
public class MyService: IClientService
{
// ...
}
[ServiceContract]
public interface IClientService
{
// This works:
[OperationContract]
IEnumerable<BookSvc> GetBooks(DateTime sinceDate);
// This fails, with the above exception:
[OperationContract]
OrganizationCollection GetOrganizations(DateTime sinceDate);
}
BookSvc is defined with [DataContract] and each property has [DataMember]. This is also true with Teacher and Student classes. The properties are all primitive types. OrganizationCollection is defined as:
[DataContract]
public class OrganizationCollection
{
[DataMember]
public IEnumerable<Teacher> Teachers { get; set; }
[DataMember]
public IEnumerable<Student> Students { get; set; }
}
There are two options:
Concrete collection type (List, etc.) should be used as return value to provide serialization mechanism for WCF (Collection Types in Data Contracts, MSDN).
Using known data types (Data Contract Known Types, MSDN).