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?
Like a class, Interface can have methods, properties, events, and indexers as its members. But interfaces will contain only the declaration of the members. The implementation of the interface's members will be given by class who implements the interface implicitly or explicitly.
A datacontract is a formal agreement between a client and service that abstractly describes the data to be exchanged. In WCF, the most common way of serialization is to make the type with the datacontract attribute and each member as datamember.
Like abstract classes, interfaces cannot be used to create objects (in the example above, it is not possible to create an "IAnimal" object in the Program class) Interface methods do not have a body - the body is provided by the "implement" class.
You must specify [DataMember] attribute on the property or the field of your Data Contract class to identify it as a Data Member. DataContractSerializer will serialize only those members, which are annotated by [DataMemeber] attribute.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With