C# Web Service Returning objects

D

Douglas Robson

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,

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.
 
D

Douglas Robson

Thanks very much Nicholas for such a detailed description.

I'll give this a try now.

Much appreciated.

Nicholas Paldino said:
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