MVP help required Web Services

D

Douglas Robson

Ok, I've removed the MyItem class which is found in References.cs and
replaced it with a reference to the namespace that has MyItem in it. I've
also got rid of the [Serializable] tag at the top of the MyItem class.

the web service works now, only the problem is, it only returns objects
which are created by the default constructor.

[WebMethod]
public MyItem getMyItem()
{
MyItem i = new MyItem("my item name");
return i;
}

In effect the previous WebMethod might as well be written as.

[WebMethod]
public MyItem getMyItem()
{
MyItem i = new MyItem();
return i;
}

Does anyone have any ideas why it returns the default constructor object
rather than the actual one? I've got private variables and public
properties. The constructor constructs the objects by using the properties.

Any help would be much appreciated. I thought this would be much simpler,
unless it's just me being stupid :-(



Nicholas Paldino wrote:

Douglas,

There are two things going on here.

First, you are using [Serializable] to mark your class as serializable.
The Web Service infrastructure does not use the Serialization framework to
serialize a class for return through a web service. Rather, it uses
XmlSerialization, which serializes only public properties/fields. When the
type is re-hydrated on the client side, it assigns the values through the
properties. This is important if you have some custom logic that is being
employed when properties are being set in your object.

Second, when you create the proxy on the client side, it creates a new
class definition based on the WSDL that the web service emits. While
semantically they are the same, to the CLR they are different.

In order do get around this, on the client side, you will have to modify
the proxy class which makes the call to the web service. Go to the cs file,
and delete the definition for the MyItem type that it created. Then, place
a using statement at the top, using referencing the namespace that the
MyItem type is in (and make sure you have a reference set as well).

You might have to also change the fully qualified names for MyType (but
that is a simple find-and-replace).

Once you do that, you should be able to compile it, and be on your way.

Note that by changing the IDE generated code, if you set the reference
again, or re-generate the proxy, you will have to re-implement these
changes.

A better solution might be to use an interface to define your type. You
will still have to modify the proxy code, but on your MyItem type, you just
have to add the interface declaration.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Douglas Robson said:
Hi,

I've got a simple C# web service. That has the following method.
[WebMethod]
public MyItem getMyItem()
{
MyItem i = new MyItem();
return i;
}

The class MyItem is as follows:

[Serializable]

public class MyItem
{
private string _name;
private int _price;
private string[] features;

public MyItem()
{
name = "testproduct";
price = 22;
features = new string[2];
features[0] = "wireless lan";
features[1] = "bluetooth";
}

.......................



I have a client application from which i simply wish to obtain an object
from the web service.

MyItem item = localservice.getMyItem( );

Cannot implicitly convert type 'TestApplication.localservice.MyItem' to
'MyItem'



How can i simply get a custom object from a web service ? Both classes
are present in both the user application and the web service. Help please
!! It's taken me ages on this.
 
N

Nicholas Paldino [.NET/C# MVP]

Douglas,

That is a "gotcha" of Xml Serialization. You have to have a default
constructor, which is what is called by the Xml Serialization engine when
the object is being deserialized.

If you are relying on the object to have parameters passed in, you will
have to find some other way of setting those parameters.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Douglas Robson said:
Ok, I've removed the MyItem class which is found in References.cs and
replaced it with a reference to the namespace that has MyItem in it. I've
also got rid of the [Serializable] tag at the top of the MyItem class.

the web service works now, only the problem is, it only returns objects
which are created by the default constructor.

[WebMethod]
public MyItem getMyItem()
{
MyItem i = new MyItem("my item name");
return i;
}

In effect the previous WebMethod might as well be written as.

[WebMethod]
public MyItem getMyItem()
{
MyItem i = new MyItem();
return i;
}

Does anyone have any ideas why it returns the default constructor object
rather than the actual one? I've got private variables and public
properties. The constructor constructs the objects by using the
properties.

Any help would be much appreciated. I thought this would be much simpler,
unless it's just me being stupid :-(



Nicholas Paldino wrote:

Douglas,

There are two things going on here.

First, you are using [Serializable] to mark your class as serializable.
The Web Service infrastructure does not use the Serialization framework to
serialize a class for return through a web service. Rather, it uses
XmlSerialization, which serializes only public properties/fields. When
the
type is re-hydrated on the client side, it assigns the values through the
properties. This is important if you have some custom logic that is being
employed when properties are being set in your object.

Second, when you create the proxy on the client side, it creates a new
class definition based on the WSDL that the web service emits. While
semantically they are the same, to the CLR they are different.

In order do get around this, on the client side, you will have to
modify
the proxy class which makes the call to the web service. Go to the cs
file,
and delete the definition for the MyItem type that it created. Then,
place
a using statement at the top, using referencing the namespace that the
MyItem type is in (and make sure you have a reference set as well).

You might have to also change the fully qualified names for MyType (but
that is a simple find-and-replace).

Once you do that, you should be able to compile it, and be on your way.

Note that by changing the IDE generated code, if you set the reference
again, or re-generate the proxy, you will have to re-implement these
changes.

A better solution might be to use an interface to define your type.
You
will still have to modify the proxy code, but on your MyItem type, you
just
have to add the interface declaration.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Douglas Robson said:
Hi,

I've got a simple C# web service. That has the following method.
[WebMethod]
public MyItem getMyItem()
{
MyItem i = new MyItem();
return i;
}

The class MyItem is as follows:

[Serializable]

public class MyItem
{
private string _name;
private int _price;
private string[] features;

public MyItem()
{
name = "testproduct";
price = 22;
features = new string[2];
features[0] = "wireless lan";
features[1] = "bluetooth";
}

.......................



I have a client application from which i simply wish to obtain an object
from the web service.

MyItem item = localservice.getMyItem( );

Cannot implicitly convert type 'TestApplication.localservice.MyItem' to
'MyItem'



How can i simply get a custom object from a web service ? Both classes
are present in both the user application and the web service. Help
please
!! It's taken me ages on this.
 
D

Douglas Robson

Thanks Nicholas,

Do you have any suggestions ?

Would structs be something i should look at ?

Nicholas Paldino said:
Douglas,

That is a "gotcha" of Xml Serialization. You have to have a default
constructor, which is what is called by the Xml Serialization engine when
the object is being deserialized.

If you are relying on the object to have parameters passed in, you will
have to find some other way of setting those parameters.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Douglas Robson said:
Ok, I've removed the MyItem class which is found in References.cs and
replaced it with a reference to the namespace that has MyItem in it.
I've
also got rid of the [Serializable] tag at the top of the MyItem class.

the web service works now, only the problem is, it only returns objects
which are created by the default constructor.

[WebMethod]
public MyItem getMyItem()
{
MyItem i = new MyItem("my item name");
return i;
}

In effect the previous WebMethod might as well be written as.

[WebMethod]
public MyItem getMyItem()
{
MyItem i = new MyItem();
return i;
}

Does anyone have any ideas why it returns the default constructor object
rather than the actual one? I've got private variables and public
properties. The constructor constructs the objects by using the
properties.

Any help would be much appreciated. I thought this would be much
simpler,
unless it's just me being stupid :-(



Nicholas Paldino wrote:

Douglas,

There are two things going on here.

First, you are using [Serializable] to mark your class as
serializable.
The Web Service infrastructure does not use the Serialization framework
to
serialize a class for return through a web service. Rather, it uses
XmlSerialization, which serializes only public properties/fields. When
the
type is re-hydrated on the client side, it assigns the values through the
properties. This is important if you have some custom logic that is
being
employed when properties are being set in your object.

Second, when you create the proxy on the client side, it creates a new
class definition based on the WSDL that the web service emits. While
semantically they are the same, to the CLR they are different.

In order do get around this, on the client side, you will have to
modify
the proxy class which makes the call to the web service. Go to the cs
file,
and delete the definition for the MyItem type that it created. Then,
place
a using statement at the top, using referencing the namespace that the
MyItem type is in (and make sure you have a reference set as well).

You might have to also change the fully qualified names for MyType
(but
that is a simple find-and-replace).

Once you do that, you should be able to compile it, and be on your
way.

Note that by changing the IDE generated code, if you set the reference
again, or re-generate the proxy, you will have to re-implement these
changes.

A better solution might be to use an interface to define your type.
You
will still have to modify the proxy code, but on your MyItem type, you
just
have to add the interface declaration.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Douglas Robson said:
Hi,

I've got a simple C# web service. That has the following method.
[WebMethod]
public MyItem getMyItem()
{
MyItem i = new MyItem();
return i;
}

The class MyItem is as follows:

[Serializable]

public class MyItem
{
private string _name;
private int _price;
private string[] features;

public MyItem()
{
name = "testproduct";
price = 22;
features = new string[2];
features[0] = "wireless lan";
features[1] = "bluetooth";
}

.......................



I have a client application from which i simply wish to obtain an object
from the web service.

MyItem item = localservice.getMyItem( );

Cannot implicitly convert type 'TestApplication.localservice.MyItem' to
'MyItem'



How can i simply get a custom object from a web service ? Both classes
are present in both the user application and the web service. Help
please
!! It's taken me ages on this.
 
D

Douglas Robson

The problem is with the deserializiation since when i invoke the web service
from the web page, the XML i get back is correct and has the correct values.


Nicholas Paldino said:
Douglas,

That is a "gotcha" of Xml Serialization. You have to have a default
constructor, which is what is called by the Xml Serialization engine when
the object is being deserialized.

If you are relying on the object to have parameters passed in, you will
have to find some other way of setting those parameters.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Douglas Robson said:
Ok, I've removed the MyItem class which is found in References.cs and
replaced it with a reference to the namespace that has MyItem in it.
I've
also got rid of the [Serializable] tag at the top of the MyItem class.

the web service works now, only the problem is, it only returns objects
which are created by the default constructor.

[WebMethod]
public MyItem getMyItem()
{
MyItem i = new MyItem("my item name");
return i;
}

In effect the previous WebMethod might as well be written as.

[WebMethod]
public MyItem getMyItem()
{
MyItem i = new MyItem();
return i;
}

Does anyone have any ideas why it returns the default constructor object
rather than the actual one? I've got private variables and public
properties. The constructor constructs the objects by using the
properties.

Any help would be much appreciated. I thought this would be much
simpler,
unless it's just me being stupid :-(



Nicholas Paldino wrote:

Douglas,

There are two things going on here.

First, you are using [Serializable] to mark your class as
serializable.
The Web Service infrastructure does not use the Serialization framework
to
serialize a class for return through a web service. Rather, it uses
XmlSerialization, which serializes only public properties/fields. When
the
type is re-hydrated on the client side, it assigns the values through the
properties. This is important if you have some custom logic that is
being
employed when properties are being set in your object.

Second, when you create the proxy on the client side, it creates a new
class definition based on the WSDL that the web service emits. While
semantically they are the same, to the CLR they are different.

In order do get around this, on the client side, you will have to
modify
the proxy class which makes the call to the web service. Go to the cs
file,
and delete the definition for the MyItem type that it created. Then,
place
a using statement at the top, using referencing the namespace that the
MyItem type is in (and make sure you have a reference set as well).

You might have to also change the fully qualified names for MyType
(but
that is a simple find-and-replace).

Once you do that, you should be able to compile it, and be on your
way.

Note that by changing the IDE generated code, if you set the reference
again, or re-generate the proxy, you will have to re-implement these
changes.

A better solution might be to use an interface to define your type.
You
will still have to modify the proxy code, but on your MyItem type, you
just
have to add the interface declaration.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Douglas Robson said:
Hi,

I've got a simple C# web service. That has the following method.
[WebMethod]
public MyItem getMyItem()
{
MyItem i = new MyItem();
return i;
}

The class MyItem is as follows:

[Serializable]

public class MyItem
{
private string _name;
private int _price;
private string[] features;

public MyItem()
{
name = "testproduct";
price = 22;
features = new string[2];
features[0] = "wireless lan";
features[1] = "bluetooth";
}

.......................



I have a client application from which i simply wish to obtain an object
from the web service.

MyItem item = localservice.getMyItem( );

Cannot implicitly convert type 'TestApplication.localservice.MyItem' to
'MyItem'



How can i simply get a custom object from a web service ? Both classes
are present in both the user application and the web service. Help
please
!! It's taken me ages on this.
 
N

Nicholas Paldino [.NET/C# MVP]

Douglas,

Based on what you said, I'm guessing that based on the parameters that
you are passed through the constructor, the class derives other values of a
read-only nature (otherwise, the values would just be set on your class).

If this is the case, you might want to consider an interface, one that
is implemented on the proxy object that is created by the reference to the
web service, and the other that is referenced by the object on the web
service side. Instead of passing instances of this class around, use the
interface.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Douglas Robson said:
The problem is with the deserializiation since when i invoke the web
service from the web page, the XML i get back is correct and has the
correct values.


Nicholas Paldino said:
Douglas,

That is a "gotcha" of Xml Serialization. You have to have a default
constructor, which is what is called by the Xml Serialization engine when
the object is being deserialized.

If you are relying on the object to have parameters passed in, you
will have to find some other way of setting those parameters.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Douglas Robson said:
Ok, I've removed the MyItem class which is found in References.cs and
replaced it with a reference to the namespace that has MyItem in it.
I've
also got rid of the [Serializable] tag at the top of the MyItem class.

the web service works now, only the problem is, it only returns objects
which are created by the default constructor.

[WebMethod]
public MyItem getMyItem()
{
MyItem i = new MyItem("my item name");
return i;
}

In effect the previous WebMethod might as well be written as.

[WebMethod]
public MyItem getMyItem()
{
MyItem i = new MyItem();
return i;
}

Does anyone have any ideas why it returns the default constructor object
rather than the actual one? I've got private variables and public
properties. The constructor constructs the objects by using the
properties.

Any help would be much appreciated. I thought this would be much
simpler,
unless it's just me being stupid :-(



Nicholas Paldino wrote:

Douglas,

There are two things going on here.

First, you are using [Serializable] to mark your class as
serializable.
The Web Service infrastructure does not use the Serialization framework
to
serialize a class for return through a web service. Rather, it uses
XmlSerialization, which serializes only public properties/fields. When
the
type is re-hydrated on the client side, it assigns the values through
the
properties. This is important if you have some custom logic that is
being
employed when properties are being set in your object.

Second, when you create the proxy on the client side, it creates a
new
class definition based on the WSDL that the web service emits. While
semantically they are the same, to the CLR they are different.

In order do get around this, on the client side, you will have to
modify
the proxy class which makes the call to the web service. Go to the cs
file,
and delete the definition for the MyItem type that it created. Then,
place
a using statement at the top, using referencing the namespace that the
MyItem type is in (and make sure you have a reference set as well).

You might have to also change the fully qualified names for MyType
(but
that is a simple find-and-replace).

Once you do that, you should be able to compile it, and be on your
way.

Note that by changing the IDE generated code, if you set the
reference
again, or re-generate the proxy, you will have to re-implement these
changes.

A better solution might be to use an interface to define your type.
You
will still have to modify the proxy code, but on your MyItem type, you
just
have to add the interface declaration.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

"Douglas Robson" <nonspecified> wrote in message
Hi,

I've got a simple C# web service. That has the following method.
[WebMethod]
public MyItem getMyItem()
{
MyItem i = new MyItem();
return i;
}

The class MyItem is as follows:

[Serializable]

public class MyItem
{
private string _name;
private int _price;
private string[] features;

public MyItem()
{
name = "testproduct";
price = 22;
features = new string[2];
features[0] = "wireless lan";
features[1] = "bluetooth";
}

.......................



I have a client application from which i simply wish to obtain an
object
from the web service.

MyItem item = localservice.getMyItem( );

Cannot implicitly convert type 'TestApplication.localservice.MyItem' to
'MyItem'



How can i simply get a custom object from a web service ? Both classes
are present in both the user application and the web service. Help
please
!! It's taken me ages on this.
 
W

William Stacey

Can we see the MyItem class? This should work, so something else is going
on. The serializer creates an instance of the object (on deserialize), but
then pokes in the public read/write properties found in the XML. So your
properties need to have a Setter if you want to pass them and have them
deserialized. So in this case, I don't currently see the empty constructor
as the issue here.
--
William Stacey [MVP]

Douglas Robson said:
Ok, I've removed the MyItem class which is found in References.cs and
replaced it with a reference to the namespace that has MyItem in it. I've
also got rid of the [Serializable] tag at the top of the MyItem class.

the web service works now, only the problem is, it only returns objects
which are created by the default constructor.

[WebMethod]
public MyItem getMyItem()
{
MyItem i = new MyItem("my item name");
return i;
}

In effect the previous WebMethod might as well be written as.

[WebMethod]
public MyItem getMyItem()
{
MyItem i = new MyItem();
return i;
}

Does anyone have any ideas why it returns the default constructor object
rather than the actual one? I've got private variables and public
properties. The constructor constructs the objects by using the
properties.

Any help would be much appreciated. I thought this would be much simpler,
unless it's just me being stupid :-(



Nicholas Paldino wrote:

Douglas,

There are two things going on here.

First, you are using [Serializable] to mark your class as serializable.
The Web Service infrastructure does not use the Serialization framework to
serialize a class for return through a web service. Rather, it uses
XmlSerialization, which serializes only public properties/fields. When
the
type is re-hydrated on the client side, it assigns the values through the
properties. This is important if you have some custom logic that is being
employed when properties are being set in your object.

Second, when you create the proxy on the client side, it creates a new
class definition based on the WSDL that the web service emits. While
semantically they are the same, to the CLR they are different.

In order do get around this, on the client side, you will have to
modify
the proxy class which makes the call to the web service. Go to the cs
file,
and delete the definition for the MyItem type that it created. Then,
place
a using statement at the top, using referencing the namespace that the
MyItem type is in (and make sure you have a reference set as well).

You might have to also change the fully qualified names for MyType (but
that is a simple find-and-replace).

Once you do that, you should be able to compile it, and be on your way.

Note that by changing the IDE generated code, if you set the reference
again, or re-generate the proxy, you will have to re-implement these
changes.

A better solution might be to use an interface to define your type.
You
will still have to modify the proxy code, but on your MyItem type, you
just
have to add the interface declaration.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Douglas Robson said:
Hi,

I've got a simple C# web service. That has the following method.
[WebMethod]
public MyItem getMyItem()
{
MyItem i = new MyItem();
return i;
}

The class MyItem is as follows:

[Serializable]

public class MyItem
{
private string _name;
private int _price;
private string[] features;

public MyItem()
{
name = "testproduct";
price = 22;
features = new string[2];
features[0] = "wireless lan";
features[1] = "bluetooth";
}

.......................



I have a client application from which i simply wish to obtain an object
from the web service.

MyItem item = localservice.getMyItem( );

Cannot implicitly convert type 'TestApplication.localservice.MyItem' to
'MyItem'



How can i simply get a custom object from a web service ? Both classes
are present in both the user application and the web service. Help
please
!! It's taken me ages on this.
 
D

Douglas Robson

I greatly appreciate your help guys.



using System;

namespace MyItemClasses

{

public class MyItem

{

private string name;

private int price;

private string[] features;

public MyItem()

{

this.Name = "default";

this.Price = 0;

this.Features = new string[2];

this.Features[0] = "robust";

this.Features[1] = "quality";

}

public MyItem (string name, int price)

{

this.Name = name;

this.Price = price;

this.Features = new string[2];

this.Features[0] = "robust";

this.Features[1] = "quality";

}

public string Name

{

get{ return this.name;}

set{this.name = Name;}

}

public int Price

{

get{ return this.price;}

set{this.price = Price;}

}

public string[] Features

{

get{ return this.features;}

set{this.features = Features;}

}

}





}

William Stacey said:
Can we see the MyItem class? This should work, so something else is going
on. The serializer creates an instance of the object (on deserialize),
but then pokes in the public read/write properties found in the XML. So
your properties need to have a Setter if you want to pass them and have
them deserialized. So in this case, I don't currently see the empty
constructor as the issue here.
--
William Stacey [MVP]

Douglas Robson said:
Ok, I've removed the MyItem class which is found in References.cs and
replaced it with a reference to the namespace that has MyItem in it.
I've
also got rid of the [Serializable] tag at the top of the MyItem class.

the web service works now, only the problem is, it only returns objects
which are created by the default constructor.

[WebMethod]
public MyItem getMyItem()
{
MyItem i = new MyItem("my item name");
return i;
}

In effect the previous WebMethod might as well be written as.

[WebMethod]
public MyItem getMyItem()
{
MyItem i = new MyItem();
return i;
}

Does anyone have any ideas why it returns the default constructor object
rather than the actual one? I've got private variables and public
properties. The constructor constructs the objects by using the
properties.

Any help would be much appreciated. I thought this would be much
simpler,
unless it's just me being stupid :-(



Nicholas Paldino wrote:

Douglas,

There are two things going on here.

First, you are using [Serializable] to mark your class as
serializable.
The Web Service infrastructure does not use the Serialization framework
to
serialize a class for return through a web service. Rather, it uses
XmlSerialization, which serializes only public properties/fields. When
the
type is re-hydrated on the client side, it assigns the values through the
properties. This is important if you have some custom logic that is
being
employed when properties are being set in your object.

Second, when you create the proxy on the client side, it creates a new
class definition based on the WSDL that the web service emits. While
semantically they are the same, to the CLR they are different.

In order do get around this, on the client side, you will have to
modify
the proxy class which makes the call to the web service. Go to the cs
file,
and delete the definition for the MyItem type that it created. Then,
place
a using statement at the top, using referencing the namespace that the
MyItem type is in (and make sure you have a reference set as well).

You might have to also change the fully qualified names for MyType
(but
that is a simple find-and-replace).

Once you do that, you should be able to compile it, and be on your
way.

Note that by changing the IDE generated code, if you set the reference
again, or re-generate the proxy, you will have to re-implement these
changes.

A better solution might be to use an interface to define your type.
You
will still have to modify the proxy code, but on your MyItem type, you
just
have to add the interface declaration.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Douglas Robson said:
Hi,

I've got a simple C# web service. That has the following method.
[WebMethod]
public MyItem getMyItem()
{
MyItem i = new MyItem();
return i;
}

The class MyItem is as follows:

[Serializable]

public class MyItem
{
private string _name;
private int _price;
private string[] features;

public MyItem()
{
name = "testproduct";
price = 22;
features = new string[2];
features[0] = "wireless lan";
features[1] = "bluetooth";
}

.......................



I have a client application from which i simply wish to obtain an object
from the web service.

MyItem item = localservice.getMyItem( );

Cannot implicitly convert type 'TestApplication.localservice.MyItem' to
'MyItem'



How can i simply get a custom object from a web service ? Both classes
are present in both the user application and the web service. Help
please
!! It's taken me ages on this.
 
D

Douglas Robson

Wrong file i gave you there. this is the correct one





using System;

namespace MyItemClasses

{

public class MyItem

{

private string name;

private int price;

private string[] features;

public MyItem()

{

this.name = "default";

this.price = 0;

this.features = new string[2];

this.features[0] = "robust";

this.features[1] = "quality";

}

public MyItem (string name, int price)

{

this.name = name;

this.price = price;

this.features = new string[2];

this.features[0] = "robust";

this.features[1] = "quality";

}

public string Name

{

get{ return this.name;}

set{this.name = Name;}

}

public int Price

{

get{ return this.price;}

set{this.price = Price;}

}

public string[] Features

{

get{ return this.features;}

set{this.features = Features;}

}

}


}

William Stacey said:
Can we see the MyItem class? This should work, so something else is going
on. The serializer creates an instance of the object (on deserialize),
but then pokes in the public read/write properties found in the XML. So
your properties need to have a Setter if you want to pass them and have
them deserialized. So in this case, I don't currently see the empty
constructor as the issue here.
--
William Stacey [MVP]

Douglas Robson said:
Ok, I've removed the MyItem class which is found in References.cs and
replaced it with a reference to the namespace that has MyItem in it.
I've
also got rid of the [Serializable] tag at the top of the MyItem class.

the web service works now, only the problem is, it only returns objects
which are created by the default constructor.

[WebMethod]
public MyItem getMyItem()
{
MyItem i = new MyItem("my item name");
return i;
}

In effect the previous WebMethod might as well be written as.

[WebMethod]
public MyItem getMyItem()
{
MyItem i = new MyItem();
return i;
}

Does anyone have any ideas why it returns the default constructor object
rather than the actual one? I've got private variables and public
properties. The constructor constructs the objects by using the
properties.

Any help would be much appreciated. I thought this would be much
simpler,
unless it's just me being stupid :-(



Nicholas Paldino wrote:

Douglas,

There are two things going on here.

First, you are using [Serializable] to mark your class as
serializable.
The Web Service infrastructure does not use the Serialization framework
to
serialize a class for return through a web service. Rather, it uses
XmlSerialization, which serializes only public properties/fields. When
the
type is re-hydrated on the client side, it assigns the values through the
properties. This is important if you have some custom logic that is
being
employed when properties are being set in your object.

Second, when you create the proxy on the client side, it creates a new
class definition based on the WSDL that the web service emits. While
semantically they are the same, to the CLR they are different.

In order do get around this, on the client side, you will have to
modify
the proxy class which makes the call to the web service. Go to the cs
file,
and delete the definition for the MyItem type that it created. Then,
place
a using statement at the top, using referencing the namespace that the
MyItem type is in (and make sure you have a reference set as well).

You might have to also change the fully qualified names for MyType
(but
that is a simple find-and-replace).

Once you do that, you should be able to compile it, and be on your
way.

Note that by changing the IDE generated code, if you set the reference
again, or re-generate the proxy, you will have to re-implement these
changes.

A better solution might be to use an interface to define your type.
You
will still have to modify the proxy code, but on your MyItem type, you
just
have to add the interface declaration.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Douglas Robson said:
Hi,

I've got a simple C# web service. That has the following method.
[WebMethod]
public MyItem getMyItem()
{
MyItem i = new MyItem();
return i;
}

The class MyItem is as follows:

[Serializable]

public class MyItem
{
private string _name;
private int _price;
private string[] features;

public MyItem()
{
name = "testproduct";
price = 22;
features = new string[2];
features[0] = "wireless lan";
features[1] = "bluetooth";
}

.......................



I have a client application from which i simply wish to obtain an object
from the web service.

MyItem item = localservice.getMyItem( );

Cannot implicitly convert type 'TestApplication.localservice.MyItem' to
'MyItem'



How can i simply get a custom object from a web service ? Both classes
are present in both the user application and the web service. Help
please
!! It's taken me ages on this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top