开发者

Wcf inhereted models

[DataContract]
Base
{ 
     [DataMember]
     public int Id {get;set;}
}

[DataContract]
A : Base 
{
    开发者_如何学编程 [DataMember]
     public string Value {get;set;}
}

[ServiceContract]
interface IService
{
    [OperationContract]
    void SetValue (Base base);
}

is there a way to use the service like the following style:

new Service ().SetValue (new A ());


You tagged this WCF so I assume you want to use it.

You need to connect to the endpoint using the ChannelFactory and then open the channel.

This will not work:

new Service ().SetValue (new A ());

You need to do smth. like this:

using (var scf = new ChannelFactory< IService >(<Binding>,<EndpointAddress>)
 {
   IService proxy = scf.CreateChannel();
   proxy.SetValue(new (A));
 }

This will return you a proxy object that implements the IService interface. You can call the SetValue on this object.


As well as changing the way you're calling the service as indicated by @Flo, you'll also need to make a small change to prepare the Data Contract Serializer to deal with the inheritance hierarchy.

The easiest way of doing this is decorating Base with the KnownTypeAttribute. Like this,

[DataContract]
[KnownType(typeof(A))]
Base
{ 
     [DataMember]
     public int Id {get;set;}
}

[DataContract]
A : Base 
{
     [DataMember]
     public string Value {get;set;}
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜