简体   繁体   中英

Expose additional classes in ASP.NET 2.0 webservice

Consider a webmethod which exposes an abstract class:

[WebMethod]
public void Save(AbstractEntity obj) 
{  
   // ..
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM