C# Web 服务无法序列化接口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/659039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
web service can't serialize an interface
提问by Steven Evers
I have an interface like so:
我有一个这样的界面:
public interface IDocument : ISerializable
{
Boolean HasBeenUploaded { get; set; }
void ISerializable.GetObjectData(SerializationInfo, StreamingContext) { }
}
There are three documents that inherit from this, all of which serialize just fine. But when creating a simple web service, that does nothing, where they can be uploaded to...
从这里继承了三个文档,所有这些都可以很好地序列化。但是当创建一个简单的网络服务时,它什么都不做,它们可以上传到......
public class DCService : System.Web.Services.WebService
{
[WebMethod]
public Boolean ReceiveDocument(IDocument document)
{
DBIO io = new DBIO();
return io.InsertIntoDB(document); // does nothing; just returns true
}
}
I get this when trying to run it: "Cannot serialize interface IDocument"
我在尝试运行它时得到这个:“无法序列化接口 IDocument”
I'm not quite sure why this would be a problem. I know that some peoplehave had trouble because they didn't want to force subclasses to implement custom serialization but I do, and up to this point it has been successful.
我不太确定为什么这会是一个问题。我知道有些人遇到了麻烦,因为他们不想强制子类实现自定义序列化,但我这样做了,并且到目前为止它已经成功了。
edit> If I create individual webmethods that accept the objects that implement the interface, it works fine, but that weakens the contract between the client/server (and undermines the purpose of having the interface in the first place)
编辑> 如果我创建接受实现接口的对象的单独 webmethods,它工作正常,但这削弱了客户端/服务器之间的契约(并破坏了首先拥有接口的目的)
采纳答案by firedfly
You may need to use an XmlInclude attribute to your web method. A example can be found here. We have run into this issue before and have added XmlInclude attributes to both our web service proxy class on the client and to certain web service methods.
您可能需要对 Web 方法使用 XmlInclude 属性。可以在此处找到示例。我们之前遇到过这个问题,并已将 XmlInclude 属性添加到客户端上的 Web 服务代理类和某些 Web 服务方法。
[WebMethod]
[XmlInclude(typeof(MyDocument))]
public Boolean ReceiveDocument(IDocument document)
{
DBIO io = new DBIO();
return io.InsertIntoDB(document); // does nothing; just returns true
}
回答by eglasius
Asp.net must need to be able to tell which specific class it will instantiate when calling that method. This is why it works when defining multiple methods with the specific classes i.e. the call will tell you which class to use.
Asp.net 必须能够知道在调用该方法时它将实例化哪个特定类。这就是为什么它在使用特定类定义多个方法时起作用的原因,即调用会告诉您要使用哪个类。
Consider whether you want the client to send the same set of info for any document, or if you really need to be able to send different info for different documents. With the later, you need the client to know the classes that implement the IDocument, and you do this with the XmlInclude (as firedfly posted).
考虑您是否希望客户端为任何文档发送相同的信息集,或者您是否真的需要能够为不同的文档发送不同的信息。对于后者,您需要客户端知道实现 IDocument 的类,并且您使用 XmlInclude(如 firefly 发布)来完成此操作。
If you instead want to always send the same info and not now about the specific classes, define a class with that info and that is what you receive in the methods. If you do need to play with IDocument in the rest of the service code, have appropiate logic in the service that gets you an IDocument instance using the data received.
如果您希望始终发送相同的信息而不是现在发送关于特定类的信息,请使用该信息定义一个类,这就是您在方法中收到的信息。如果您确实需要在服务代码的其余部分使用 IDocument,请在服务中使用适当的逻辑,使用接收到的数据为您获取 IDocument 实例。
回答by Ohad Schneider
+1 for firedfly, however it should be noted that the XmlInclude attribute may be appended to the web service classrather than to each and every method (or base type, which is also an option). I have tested it and the code is generated well, keeping the inheritance structure.
对于萤火虫+1,但是应该注意, XmlInclude 属性可以附加到 Web 服务类而不是每个方法(或基类型,这也是一个选项)。我已经测试过了,代码生成的很好,保持了继承结构。
I got this from the comments section of the same blog he referred to, so credit goes to the OP.
我从他提到的同一博客的评论部分得到了这个,所以归功于 OP。
BTW this isn't a comment to firedfly's post because I don't have enough reputation to comment
顺便说一句,这不是对萤火虫帖子的评论,因为我没有足够的声誉发表评论