473,608 Members | 2,264 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 = "testproduc t";
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.ge tMyItem( );

Cannot implicitly convert type 'TestApplicatio n.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 29783
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
XmlSerializatio n, 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.co m

"Douglas Robson" <nonspecified > wrote in message
news:uH******** ******@TK2MSFTN GP12.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 = "testproduc t";
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.ge tMyItem( );

Cannot implicitly convert type 'TestApplicatio n.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.c om> wrote in
message news:%2******** ********@TK2MSF TNGP10.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
XmlSerializatio n, 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.co m

"Douglas Robson" <nonspecified > wrote in message
news:uH******** ******@TK2MSFTN GP12.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 = "testproduc t";
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.ge tMyItem( );

Cannot implicitly convert type 'TestApplicatio n.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
2689
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 DataTable LoadStuff() { DataTable dt = new DataTable(); .... dt.Columns.Add(...) ....
4
1391
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 signs in as at client). This is in the context of a ASP.Net web service running on an intranet. - I've ticked and unticked all the right boxes in IIS.
5
2675
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 = DayOfWeek.Wednesday
1
2061
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 only have three weeks to finish it all! My GUI and Web services were finished, but I hadn't tested them. So I linked the GUI to the Web service and started testing them.
3
2702
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 ADO.NET and of course ASP classic uses ADO and doesn't understand a dataset. Any suggestions??
2
1563
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
1925
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 seen a couple ways to do this. The dataset coming back to me is a SybaseASE Data Reader. Any help you can provide would be much appreciated. thanks, Anthony Right now I have done the following:
0
1300
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 using the .net client it bombs out with: System.InvalidOperationException: Client found response content type of 'text/plain', but expected 'text/xml'. The request failed with the error message: --
0
1707
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 a web service. Unfortunatly i get a NULL response and i can't understand why. RpcRsat.RSATWSPortType rpt = new RpcRsat.RSATWSPortTypeClient(); RpcRsat.retrieve_seqResponse res = rpt.retrieve_seq(req);
0
8002
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8496
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8475
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6816
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5475
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3962
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4024
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2474
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 we have to send another system
0
1329
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.