473,320 Members | 1,988 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 WebServiceEntity{

[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(Namespace = "http://www.fs.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class WebService1 : System.Web.Services.WebService {

public WebService1() { }

[WebMethod]

public BaseEntity GetBaseEntity()

{

BaseEntity instance = new BaseEntity();

instance.FirstName = "Leo";

instance.LastName = "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>Leo</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 WebServiceConsumer {

public partial class Form1 : Form {

BaseEntity entity;

WebService1 service;

public Form1() {

InitializeComponent();

service = new WebService1();

}

private void button1_Click(object sender, EventArgs e) {

entity = service.GetBaseEntity();

this.Text = entity.GetFullName();

}

}

}

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

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

¡*

[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespac e="http://www.fs.org/")]

public partial class BaseEntity {

private string firstNameField;

private string lastNameField;

public string FirstName {

get {return this.firstNameField;}

set {this.firstNameField = value;}

}

public string LastName {

get {return this.lastNameField; }

set {this.lastNameField = value; }

}

}

}
Then I modify the code in Form1.

WebServiceEntity.BaseEntity entity;

And another error raises. ¡°Error 1 Cannot implicitly convert type
'WebServiceConsumer.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 WebServiceEntity{

[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.ToString();

}

}

}

8. Modify the web method:

//[WebMethod]

//public BaseEntity GetBaseEntity()

//{

// BaseEntity instance = new BaseEntity();

// instance.FirstName = "Leo";

// instance.LastName = "Xue";

// return instance;

//}

[WebMethod]

[XmlInclude(typeof(SubEntity1))]

public BaseEntity GetBaseEntity()

{

SubEntity1 instance = new SubEntity1();

instance.FirstName = "Leo";

instance.LastName = "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="SubEntity1"
xmlns="http://www.fs.org/">

<FirstName>Leo</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 WebServiceConsumer{

public partial class Form1 : Form{

WebServiceEntity.SubEntity1 entity;

WebService1 service;

public Form1(){

InitializeComponent();

service = new WebService1();

}

private void button1_Click(object sender, EventArgs e){

try{

entity = (SubEntity1)service.GetBaseEntity();

this.Text = entity.GetFullName();

}

catch (Exception ex){

Debug.WriteLine(ex.Message);

}

}

}

}


11. When I run this application, I get the error: ¡° A first chance
exception of type 'System.InvalidOperationException' 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 1835

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

Similar topics

2
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...
0
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
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...
3
by: Paul Michaud | last post by:
Consider the following: Class A { .... } Class B:A { ....
12
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...
0
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...
1
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...
0
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...
3
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
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.