How to * use interface types with WCF?

In particular, the question arises: How to serialize a class with WCF that has (must have) interface types in property signatures, since it implements the interface?

This is a design issue. I'm not looking for a hack. The goal is to have separate interface definitions from class implementations and therefore not allow interfaces to refer to implementing classes.

I know that we should not transfer interfaces with WCF and also that there are still (incompatible) ways to do this (for example, using the NetDataContractSerializer or the ServiceKnownType attribute), but this leaves me with the question: is there a suitable way to use classes based on interfaces with WCF, or if it won't be an attempt at all?

+3
source share
2 answers

I found a solution to my specific problem.

The problem was that I marked the properties (such as an interface) as a DataMember. Instead of marking the corresponding private members as a DataMember (and making sure their types are concrete classes), the problem is solved.

Example:

[DataContract()]
public class Company : ICompany
{
    [DataMember(Name = "Employees")]
    private EmployeeList _employees;

    public IEmployeeList Employees { get { return _employees; }}
}
+3
source

Serialization is mainly related to data transport. Interfaces describe behavior. Essentially, these two things are completely orthogonal, IMO.

You can get it to work with [KnownType(...)]- and this will certainly help if you use the exact same types at each end (sharing assemblies), but it is very not portable.

WCF , , , , , WSDL.

, WCF , , p

+4

All Articles