tags:

views:

20

answers:

2
[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 ());
+1  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.

Flo
A: 

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;}
}
Samuel Jack
Yes but by this knowntype way; for all the new class i added i also add a knowntype in base too.
jack london
That's true - but have a look at Davy Brion's Known Type Provider: http://davybrion.com/blog/2008/07/the-known-type-provider/
Samuel Jack