0

Whats the best way to return a

List<object>

back to the server? The below solution requires that the Server already know the object. Im wondering if its possible to declare the object on the Client side and just get the Server to write the results when the List is returned.

  1. Declare the class in the Server with the necessary fields and [DataMember] and [DataContract] attributes. Use it in the Client via Add Service Reference.
Carlos Landeras
  • 11,025
  • 11
  • 56
  • 82
Hans Rudel
  • 3,433
  • 5
  • 39
  • 62
  • 1
    Option number 1, yes. – Grant Thomas May 23 '13 at 08:15
  • @GrantThomas is it possible for the Client code to only contain the class declaration? – Hans Rudel May 23 '13 at 08:17
  • 1
    Well, yes, but _why_? This kind of goes against the grain of strongly-typed, reusable, client-conforming web services. You could define a type in your client, make it serializable and then send the bytes over the wire, but again, to what end? You might well have an understandable reason, but without it this just seems a weird thing to do. – Grant Thomas May 23 '13 at 08:19
  • 1
    @HansRudel, here's my understanding. `[DataMember]`, `[DataContract]` attributes are designated to give server and client the clue how to (de)serialize the data. If you want to skip using these attributes - then you need to pass data not as usual(sending .net objects over the channel) but via xml or json for example and deserialize them on the server as you want. – alex.b May 23 '13 at 08:23
  • It only makes sense if the server knows what is being received... I'm not sure it can deserialize objects that it knows nothing about. The only way I could see you managing that is to serialize it to a string (json or xml) and returning that. I don't for the life of me see the point of doing that however. – Dave Lawrence May 23 '13 at 08:25
  • GrantThomas The client code will change often but the server side shouldnt require many/if any updates. So it would be nice to only have to change things in 1 solution rather than 2 and possibly making a mistake. I see ur point though. @aleksey.berezan yeah ok, i think i will stick with declaring it in the Server for now then but will look into (de)serialization of the list at a later date. If you could put this as an answer ill mark it as accepted. Thanks guys – Hans Rudel May 23 '13 at 08:27
  • thanks @GrantThomas, I've copied comment to an answer – alex.b May 23 '13 at 09:48

2 Answers2

1

Here's my understanding.
[DataMember], [DataContract] attributes are designated to give server and client the clue how to (de)serialize the data.
So if you want to skip using these attributes - then you need to pass data not as usual(sending doNet objects over the channel) but via xml or json for example and deserialize them on the server as you want.

alex.b
  • 4,547
  • 1
  • 31
  • 52
0

is it possible for the Client code to only contain the class declaration?

Yes, if it is known type on server side. So what you can do is send the input as Json or XML you can convert it back to a object list in the server side.

Community
  • 1
  • 1
Damith
  • 62,401
  • 13
  • 102
  • 153