473,769 Members | 3,329 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to consume a web service returns inherited object types?


I come across this problem during my recent project and even now I am still
be puzzled by it. So I paste the procedure here. I wish you can figure out
where I am wrong or missing. The sample application is developed with VS
2005 Beta 2.

1. Create a base class named ¡°BaseEntity¡±

namespace WebServiceEntit y{

[Serializable]

public class BaseEntity {

private string _firstName = "";

public string FirstName {

get { return _firstName; }

set { _firstName = value; }

}

private string _lastName = "";

public string LastName {

get { return _lastName; }

set { _lastName = value; }

}

public virtual string GetFullName() {

return this._firstName + " " + this._lastName;

}

}

}

2. Create a web method, which will return an instance of BaseEntity
type.
[WebService(Name space = "http://www.fs.org/")]

[WebServiceBindi ng(ConformsTo = WsiProfiles.Bas icProfile1_1)]

public class WebService1 : System.Web.Serv ices.WebService {

public WebService1() { }

[WebMethod]

public BaseEntity GetBaseEntity()

{

BaseEntity instance = new BaseEntity();

instance.FirstN ame = "Leo";

instance.LastNa me = "Xue";

return instance;

}

}

3. Invoke this Web Method in IE:
http://localhost/WebServiceTest/WebS.../GetBaseEntity. It seems ok.

<?xml version="1.0" encoding="utf-8" ?>

- <BaseEntity xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http ://www.w3.org/2001/XMLSchema" xmlns="http://www.fs.org/">

<FirstName>Le o</FirstName>

<LastName>Xue </LastName>

</BaseEntity>

4. Create a windows application. Add a web reference to
http://localhost/WebServiceTest/WebService1.asmx

Add a form then invoke the web service.
namespace WebServiceConsu mer {

public partial class Form1 : Form {

BaseEntity entity;

WebService1 service;

public Form1() {

InitializeCompo nent();

service = new WebService1();

}

private void button1_Click(o bject sender, EventArgs e) {

entity = service.GetBase Entity();

this.Text = entity.GetFullN ame();

}

}

}

5. Compile it! Then I get a strange error: ¡°Error 1
'BaseEntity' is an ambiguous reference between 'WebServiceEnti ty.BaseEntity'
and 'WebServiceCons umer.MyService. BaseEntity' ¡°.

I don¡¯t remember I have created a class named
WebServiceConsu mer.MyService.B aseEntity. So I trace into the definition of
the class. It is in reference.cs of my web reference.
namespace WebServiceConsu mer.MyService {

¡*

[System.Serializ ableAttribute()]
[System.Xml.Seri alization.XmlTy peAttribute(Nam espace="http://www.fs.org/")]

public partial class BaseEntity {

private string firstNameField;

private string lastNameField;

public string FirstName {

get {return this.firstNameF ield;}

set {this.firstName Field = value;}

}

public string LastName {

get {return this.lastNameFi eld; }

set {this.lastNameF ield = value; }

}

}

}
Then I modify the code in Form1.

WebServiceEntit y.BaseEntity entity;

And another error raises. ¡°Error 1 Cannot implicitly convert type
'WebServiceCons umer.MyService. BaseEntity' to
bServiceEntity. BaseEntity' ¡±

Question 1: Why should this partial class be generated automatically since I
don¡¯t need it. How to avoid it.

6. So I delete the partial class in reference.cs. If I can¡¯t do this,
let me know. This time everything is ok. But if I update the web reference,
the boring partial class appears again. Stupid!

7. Well, continue my demo. Now I should add the inherited class of
BaseEntity.
namespace WebServiceEntit y{

[Serializable]

public class SubEntity1 : BaseEntity {

private int _age = 10;

public int Age {

get { return _age; }

set { _age = value; }

}

public override string GetFullName() {

return this.FirstName + " " + this.LastName + " " +
this._age.ToStr ing();

}

}

}

8. Modify the web method:

//[WebMethod]

//public BaseEntity GetBaseEntity()

//{

// BaseEntity instance = new BaseEntity();

// instance.FirstN ame = "Leo";

// instance.LastNa me = "Xue";

// return instance;

//}

[WebMethod]

[XmlInclude(type of(SubEntity1))]

public BaseEntity GetBaseEntity()

{

SubEntity1 instance = new SubEntity1();

instance.FirstN ame = "Leo";

instance.LastNa me = "Xue";

instance.Age = 20;

return instance;

}


9. Invoke the web method in IE again:
http://localhost:1639/WebService1/We.../GetBaseEntity

<?xml version="1.0" encoding="utf-8" ?>

- <BaseEntity xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http ://www.w3.org/2001/XMLSchema" xsi:type="SubEn tity1"
xmlns="http://www.fs.org/">

<FirstName>Le o</FirstName>

<LastName>Xue </LastName>

<Age>20</Age>

</BaseEntity>
It is ok, right?

10. Well, let¡¯s invoke it in Form1. (Of course I have deleted the partial
classes in refernce.cs. ) Here¡¯s my code:

namespace WebServiceConsu mer{

public partial class Form1 : Form{

WebServiceEntit y.SubEntity1 entity;

WebService1 service;

public Form1(){

InitializeCompo nent();

service = new WebService1();

}

private void button1_Click(o bject sender, EventArgs e){

try{

entity = (SubEntity1)ser vice.GetBaseEnt ity();

this.Text = entity.GetFullN ame();

}

catch (Exception ex){

Debug.WriteLine (ex.Message);

}

}

}

}


11. When I run this application, I get the error: ¡° A first chance
exception of type 'System.Invalid OperationExcept ion' occurred in
System.Xml.dll, There is an error in XML document ¡°. Why?

Question 2: Since I can invoke the web method correctly in IE, how can I
invoke it in my application?
Nov 23 '05 #1
0 1872

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

Similar topics

2
4256
by: Michael Hatmaker | last post by:
I have begun experimenting with web services, and I created some simple web services in C# and was able to install them with IIS and create an equally simple C# client to consume them. My next experiment was to use Python to consume these same web services, and even though I am able to get Python to consume web services from a variety of sources (Apache SOAP, Glue, AXIS), I cannot get web services created with MS.NET to work. Actually,...
0
1741
by: pranoliver | last post by:
Hi, How can I consume a Web Service that returns a DATASET from classic ASP. I know how to consume the Web Service when it returns just a variable using SOAP Toolkit 3.0. Thanks In Advance.
4
6822
by: geilen | last post by:
I'm trying to use a dataset returned from a web service in an unmanaged C++ (MFC) client. The dataset is returned as a BSTR, and I'm having trouble reading the BSTR into an XML document for processing. The data looks correct in the BSTR. Can anyone help point me in the right direction on this?
3
3160
by: Paul Michaud | last post by:
Consider the following: Class A { .... } Class B:A { ....
12
5342
by: Noel | last post by:
Hello, I'm currently developing a web service that retrieves data from an employee table. I would like to send and retrieve a custom employee class to/from the webservice. I have currently coded the custom employee class and have built it as a separate library (employee.dll). This employee.dll is being referenced by both the web service and the windows application. I face the following problem when I send this class to the webservice.
0
1683
by: leslie_tighe | last post by:
Hello, I have a set of web services running on Java server that are exposed through axis 1.2.1. I can invoke these services in browser and through a java test client. However, when I try to consume them in VB .NET I get objects with nothing in them. Interestingly, if I call a webservice that returns a string, then it all works fine. I have pasted the wsdl file for review. Any suggestions would be greatly appreciated. I used the...
1
4880
by: Mike9900 | last post by:
I would like to return an object which inherits an interface. A web service instantiate a class that inherits that interface and returns that object. But I get error when referencing the web service from windows form. the error is: "System.NotSupportedException: Cannot serialize interface EmployeeCommon.IEmployee." EmployeeCommon.IEmployee is an interface which is inerited by the instantiated class. That class is instantiated and...
0
998
by: Michael Reinhart | last post by:
I'm kinda new to web services... I'm trying to consume a web service that returns a dataset and am getting this error: Server was unable to process request. ---Object reference not set to an instance of an object. Here's my code: Dim myDS As New System.Data.DataSet myDS = serviceShoe.LoadCustomers() ' This is where the error is being thrown
3
6625
dmjpro
by: dmjpro | last post by:
plz send me a good link which can clearify me how the J2EE framework works i want the details information .... plz help thanx
0
9587
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10211
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
10045
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
8870
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
6672
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
5298
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
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3958
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
2
3561
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.