Can you use interfaces like DataMembers in WCF?

Can you do it?

[DataContract] public class RegisterEndpointRequest : NotificationRegistrationServiceRequest { [DataMember] public IEndpoint Endpoint { get; set; } } 

Note that the endpoint is an interface ( IEndpoint ), not a class. Will WCF allow this?

+4
source share
3 answers

WCF DataContractAttribute is not intended for use on an interface: See the documentation here

The answer in this question may give you a better idea of ​​why.

+3
source

I think you can (but I have not tested it), but you will need to declare all implementations of this interface using [KnownType] :

 [DataContract] [KnownType(typeof(EndpointImplA))] [KnownType(typeof(EndpointImplB))] public class RegisterEndpointRequest : NotificationRegistrationServiceRequest { [DataMember] public IEndpoint Endpoint { get; set; } } 

Each implementing class must have the [DataContract] attribute.

+5
source

Yes, of course, you can have a DataMember interface inside a DataContract .. Then you must specify all implementations of the interface in KnownType attributes ...

  [DataContract] [KnownType(typeof(ActivityDC))] [KnownType(typeof(StepDC))] [KnownType(typeof(WaveDC))] public class CampaignDC : AuditedEntityBaseDC { [DataMember] public IList<IActivityDC> Activities { get; set; } 
0
source

All Articles