在ASP.NET 2.0 Web服务中公开其他类

Jan*_*m B 2 .net c# web-services asmx .net-2.0

考虑一个公开抽象类的web方法:

[WebMethod]
public void Save(AbstractEntity obj) 
{  
   // ..
}
Run Code Online (Sandbox Code Playgroud)

有几个继承自AbstractEntitylike的类

public class Patient : AbstractEntity 
{
   // ...
}
Run Code Online (Sandbox Code Playgroud)

现在我想让webservice使用者创建一个新的Patient对象并保存它:

service.Save(new Patient { Name = "Doe", Number = "1234567" });
Run Code Online (Sandbox Code Playgroud)

因为"保存"采用AbstractEntity,所以客户端没有患者代理.我当然可以创建一个暴露患者的虚拟方法,但我希望有更好的方法.

如何以一种很好的方式公开Patient类和其他未直接在webservice接口中引用的类?

M4N*_*M4N 5

You need to add an XmlInclude attribute to your method:

[WebMethod]
[XmlInclude(typeof(Patient))] 
public void Save(AbstractEntity obj) 
{  
   // ..
}
Run Code Online (Sandbox Code Playgroud)

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.