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?