views:

2791

answers:

4

I have a need to pass a custom object to a remote web service. I have read that it may be necessary to implement ISerializable, but I've done that and I'm encountering difficulties. What is the proper way in C# to pass a custom object to a web service method?

A: 

Edit: removed the part about [Serializable]

Are you creating the service, or consuming it?

To create an object which can be passed as a parameter of a webmethod, you don't have to do anything special. That is if you're creating an asmx webservice.

OTOH, If you're creating a WCF service then you have to mark the class with [DataContract] and all the members you want to be serialized with [DataMember].

If you're consuming the webservice, then the proxy classes for the object to pass should be generated when you added the service reference. You just have to use them.

fretje
Actually I'm both making and consuming. What you've mentioned here is what I've been doing, but I keep getting complaints from VS about needing a parameterless constructor in the custom object (which I've added, but to no avail.).
byte
can you then provide the relevant code and exact *complaints* ;-)
fretje
+2  A: 

Looks like a duplicate to this question

Anyway, all objects involved in WS interactions should be XML-serializable, not ISerializable (which is binary serialization). Moreover, they should be described in the service contract (WSDL), otherwise clients won't be able to consume them. This article should be useful to understand XML-serialization with XML Web Services.

However, if you are talking about really custom objects (i.e. any type). You should consider passing them in binary form: either as base64-encoded, or as attachments. The question I linked to has an answer how to do that.

DreamSonic
A: 

Look at this question to see how to implement XML serialization for your custom objects

Michael Kniskern
+1  A: 

The objects you provide as arguments as part of the service request must be marked with [Serializable] and based on some of the answers posted before mine you also need to make sure your custom object does not contain any parameters in the constructor.

Also keep in mind any logic you have inside your class will not get created in the proxy class that gets created on the client side. All you will see on the client side is a default constructor and properties. So if you add methods to your custom objects keep in mind that the client will not see them or be able to use them.

Same thing goes for any logic you might put into any of the properties.

Example

[Serializable]
public class Customer
{
 public int Id { get; set; }
 public string Name { get; set; }
}
Jim Scott
Jim, sorry, but [Serializable] has nothing to do with web services.
John Saunders
@John: Why not? Doesn't the XmlSerializer also rely on the [Serializable] attribute?
fretje
Added example. I use the [Serializable] attribute all the time when needing to have a custom object be serialized.
Jim Scott
@fretje: No, it does not. See http://msdn.microsoft.com/en-us/library/182eeyhh(VS.85).aspx.
John Saunders
@Jim: Try removing [Serializable] and see if it still works.
John Saunders
@John: Ok, I stand corrected (and changed my answer ;-)
fretje
[Serializable] works for binary serialization, not XML-serialization.
DreamSonic