From: Raj on
I have written a web service that receive an object and return an object.
Though the object is serialized, data sent to the service is always null and
hence object with null values returned . Any help would be appreciated.

I have serialized the object using [Serializable] attribute

Thank you

Regards
Raj


From: Jeroen Mostert on
On 2010-01-08 5:27, Raj wrote:
> I have written a web service that receive an object and return an object.
> Though the object is serialized, data sent to the service is always null and
> hence object with null values returned . Any help would be appreciated.
>
> I have serialized the object using [Serializable] attribute
>
[Serializable] affects binary serialization only (remoting) and has no
effect at all on XML or DataContract serialization.

Without seeing more detailed code with what you're doing it's hard to tell
what's going wrong. If you're using XML serialization (old-style Web
services) make sure you're using public fields and properties. If you're
using DataContract serialization (WCF) make sure the class is marked
[DataContract] and the members you want to serialize are marked [DataMember].

--
J.
From: Raj on
1. The object has public properties
2. I am not using WCF
3. I am not using XML serialization using only binary serialization

This is the object sent/returned:

namespace WebServiceData
{
[Serializable]
public class Result
{
private string data;

public Result()
{
}

public Result(string data)
{
this.data = data;
}

public string Reader
{
get{return data;}
}

public string Writer
{
set{data=value;}
}
}
}

Web Service:
using WebServiceData;

namespace WebService1
{
[WebService(Namespace = "http://localhost/KRA/ASPDOTNET/WebService1")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service1 : System.Web.Services.WebService
{
public UserCredentials Consumer;

[SoapHeader("Consumer")]
[WebMethod]

public WebServiceData.Result PutData(WebServiceData.Result res)
{
string result = string.Empty;
WebServiceData.Result wr = new WebServiceData.Result();
try
{

wr.Writer = "Data received: " + res.Reader;
return wr;
}
catch (Exception e)
{
wr.Writer = e.Message;
return wr;
}
}

}
}

Web Service Consumer:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Services;
using WebServiceData;

namespace WebServiceConsumer
{
class Program
{
static void Main(string[] args)
{


Service1 s = new Service1();

WebServiceData.Result r = new WebServiceData.Result();

try
{

r.Writer = "test";
Console.WriteLine(r.Reader);
WebServiceData.Result newr=s.PutData(r);
Console.WriteLine("result: " + newr.Reader);
Console.Read();

}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}


"Jeroen Mostert" wrote:

> On 2010-01-08 5:27, Raj wrote:
> > I have written a web service that receive an object and return an object.
> > Though the object is serialized, data sent to the service is always null and
> > hence object with null values returned . Any help would be appreciated.
> >
> > I have serialized the object using [Serializable] attribute
> >
> [Serializable] affects binary serialization only (remoting) and has no
> effect at all on XML or DataContract serialization.
>
> Without seeing more detailed code with what you're doing it's hard to tell
> what's going wrong. If you're using XML serialization (old-style Web
> services) make sure you're using public fields and properties. If you're
> using DataContract serialization (WCF) make sure the class is marked
> [DataContract] and the members you want to serialize are marked [DataMember].
>
> --
> J.
> .
>
From: Family Tree Mike on


"Raj" wrote:

> 1. The object has public properties
> 2. I am not using WCF
> 3. I am not using XML serialization using only binary serialization
>


I'm pretty sure that nothing shown below changes your service from xml to
binary serialization.


> This is the object sent/returned:
>
> namespace WebServiceData
> {
> [Serializable]
> public class Result
> {
> private string data;
>
> public Result()
> {
> }
>
> public Result(string data)
> {
> this.data = data;
> }
>
> public string Reader
> {
> get{return data;}
> }
>
> public string Writer
> {
> set{data=value;}
> }

When your object is serialized and deserialized, Reader has no _set_ method
and Writer has no _get_ method. They will not serialize, to the best of my
knowledge, and therefor the private field data will never be restored.

> }
> }
>

Mike
From: Jeroen Mostert on
On 2010-01-08 10:01, Raj wrote:
> 1. The object has public properties
> 2. I am not using WCF
> 3. I am not using XML serialization using only binary serialization
>
You cannot use binary serialization in a web service. A web service always
uses XML serialization. The attributes do not choose the mechanism used;
they only provide information to whatever mechanism serializes. If what
you're doing is not compatible with XML serialization, it won't work.

> This is the object sent/returned:
>
> namespace WebServiceData
> {
> [Serializable]
> public class Result
> {
> private string data;
>
> public Result()
> {
> }
>
> public Result(string data)
> {
> this.data = data;
> }
>
> public string Reader
> {
> get{return data;}
> }
>
From http://msdn.microsoft.com/library/182eeyhh: "XML serialization does
not convert methods, indexers, private fields, or read-only properties
(except read-only collections)."

> public string Writer
> {
> set{data=value;}
> }

And that note should also include write-only properties, but it doesn't,
because write-only properties are a bad idea to begin with. See
http://msdn.microsoft.com/library/ms182165.

--
J.