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?