WCF, Interface return type and KnownTypes

Ok, i managed to fix it
The problem, as I see it, was this

Since I’m returning an interface and not a concrete class, WCF doesn’t know what to expect on the other end. So, it can be anything. When he gets a List, he’s confused.
The correct way to do it was to add the KnownType attributes where needed.
Who needs to know those types? the service implementation, to serialize and deserialize them correctly. However, the client talks with the interface of the service, not with the implementation itself. That’s why adding theKnownType attribute in the service implementation didn’t work
The problem here is that, interfaces don’t allow KnownType attributes, but they do allow ServiceKnownType attributes. The solution to the problem was to add the expected type in the service interface contract, and voila, everything works ok and using interfaces

    [ServiceContract]
    [ServiceKnownType(typeof(Atm))]
    [ServiceKnownType(typeof(List<Atm>))]
    public interface IAtmFinderService
    {

        [OperationContract]
        ICollection<IAtm> GetAtms();

    }

Leave a Comment