将其他类在ASP.NET 2.0 Web服务NET、ASP、Web

2023-09-06 08:13:12 作者:키스 마ȕ

考虑一个WebMethod这暴露了一个抽象类:

Consider a webmethod which exposes an abstract class:

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

有几类,从 AbstractEntity 继承像

public class Patient : AbstractEntity 
{
   // ...
}

现在我想使web服务消费者创造一个新的病人对象,并将其保存:

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" });

由于保存需要一个AbstractEntity,就会出现在客户端上没有病人的代理。当然,我可以创建一个公开一个病人一个虚拟的方法,但我希望有一个更好的办法。

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.

我如何揭露Patient类和其他类不能直接引用的Web服务接口在一个不错的方式?

How do I expose the Patient class and other classes not directly referenced in the webservice interface in a nice way?

推荐答案

您需要添加的 XmlInclude 属性,以你的方法:

You need to add an XmlInclude attribute to your method:

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

由于写的评论,当您添加XmlInclude属性和更新客户端的Web引用,将生成的代理类为AbstractEntity和病人(从AbstractEntity派生)。

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.

一件事是不是很好,是当你创建一个从AbstractEntity派生新类,则必须添加另一个XmlInclude属性,所有相关的网页的方法。

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.