views:

33

answers:

1

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?

+4  A: 

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.

M4N
In client code, does the reference then provide a Patient proxy or does the OP have to derive from the proxy AbstractEntity?
Adam
thanks! ... (filler) ...
Jan Willem B
The Patient proxy derives from Abstractentity proxy, which are both provided.
Jan Willem B
Nice one, well - I've learnt something new today too.
Adam