5

Can you do this?

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

Notice the member Endpoint is an interface (IEndpoint), not a class. Will WCF allow this?

Paul Fryer
  • 9,268
  • 14
  • 61
  • 93
  • I *think* it will, as long as whatever concrete `IEndpoint` implementation you have for your `Endpoint` property is also decorated with a `DataContractAttribute` and appropriate `DataMemberAttribute`s on its members. **EDIT**: Plus what [Aasmund said](http://stackoverflow.com/a/11800139/74757) about the `KnownTypeAttribute`s. – Cᴏʀʏ Aug 03 '12 at 16:59

3 Answers3

8

I think you can (but I haven't tested it), but you will then need to declare all implementations of that interface with [KnownType]:

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

Each implementing class must have a [DataContract] attribute.

Aasmund Eldhuset
  • 37,289
  • 4
  • 68
  • 81
3

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

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

Community
  • 1
  • 1
chridam
  • 100,957
  • 23
  • 236
  • 235
1

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

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