473,511 Members | 15,178 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# Web Service Returning objects

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.
Nov 17 '05 #1
2 29778
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]
- mv*@spam.guard.caspershouse.com

"Douglas Robson" <nonspecified> wrote in message
news:uH**************@TK2MSFTNGP12.phx.gbl...
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.

Nov 17 '05 #2
Thanks very much Nicholas for such a detailed description.

I'll give this a try now.

Much appreciated.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP10.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Douglas Robson" <nonspecified> wrote in message
news:uH**************@TK2MSFTNGP12.phx.gbl...
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.


Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2684
by: bubby | last post by:
Should I be concerned about the classic "Deep/Shallow" copy problem when returning objects, specifically a DataTable or DataView from a method? For example, see the code below: private...
4
1388
by: Daniel Bass | last post by:
Hey, I've been asked to look into network security where an IIS virtual directory is configure to not have anonymous access, but rather to go with the windows authentication (what the user...
5
2665
by: Rob | last post by:
Hi, I have defined an enumeration thus: Public Enum CollectionDayOfWeek NoCollectionDay = -1 Sunday = DayOfWeek.Sunday Monday = DayOfWeek.Monday Tuesday = DayOfWeek.Tuesday Wednesday =...
1
2043
by: Matthias De Ridder | last post by:
Hello, I really hope that someone will be able to help me, because I'm desperate now! I'm a student, graduating this year, and I'm working on a thesis where C# Web Services are involved. I...
3
2695
by: James | last post by:
I need to create a C# web service that returns a recordset for an ASP classic applicaiton to consume. My problem is that so far the only thing that I have found I can return is a dataset using...
2
1553
by: Water Cooler v2 | last post by:
I create a test Web service like so: public class ServiceThingy: System.Web.Services.WebService { public int ReturnFour() { return 4; }
2
1922
by: Anthony Biondo Jr | last post by:
I am trying to figure out the best way to return data through a web service. If the value is a single value I can just set it equal to the web service name. If I am returning a set of data I have...
0
1293
by: robert112 | last post by:
Hi All, I have a .net WSE 3.0 Web Service acting as a client calling a j2EE web Service. The service works when I call it using a client program called soapUI (which is free to download) but when...
0
1701
by: Algobardo | last post by:
Good morning, this is the first time i write on this forum because i googled and i've seen related post with no solution. I will expose briefly the problem. I'm using c# 3.5 and i'm trying using...
0
7417
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7074
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7506
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5659
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5063
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4734
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3219
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.