Expose additional classes in ASP.NET 2.0 webservice
Consider a webmethod which exposes an abstract class:
[WebMethod]
public void Save(AbstractEntity obj)
{
// ..
}
开发者_如何学Python
There are several classes inheriting from AbstractEntity
like
public class Patient : AbstractEntity
{
// ...
}
Now I want to enable the webservice consumer to create a new Patient object and save it:
service.Save(new Patient { Name = "Doe", Number = "1234567" });
Because "Save" takes an AbstractEntity, there will be no Patient proxy on the client side. I could of course create a dummy method which exposes a Patient, but I'm hoping there is a better way.
How do I expose the Patient class and other classes not directly referenced in the webservice interface in a nice way?
You need to add an XmlInclude attribute to your method:
[WebMethod]
[XmlInclude(typeof(Patient))]
public void Save(AbstractEntity obj)
{
// ..
}
As written in the comments, when you add the XmlInclude attribute and update the web reference on the client-side, proxy classes for both AbstractEntity and Patient (deriving from AbstractEntity) will be generated.
One thing which is not so nice, is that whenever you create a new class derived from AbstractEntity, you will have to add another XmlInclude attribute to all related web methods.
精彩评论