C# 将自定义对象传递给 Web 服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/947680/
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
Passing custom objects to a web service
提问by dreadwail
I have a need to pass a custom object to a remote web service. I have read that it may be necessary to implement ISerializable, but I've done that and I'm encountering difficulties. What is the proper way in C# to pass a custom object to a web service method?
我需要将自定义对象传递给远程 Web 服务。我已经读到可能有必要实现 ISerializable,但我已经这样做了,但遇到了困难。在 C# 中将自定义对象传递给 Web 服务方法的正确方法是什么?
采纳答案by Jim Scott
The objects you provide as arguments as part of the service request must be marked with [Serializable] and based on some of the answers posted before mine you also need to make sure your custom object does not contain any parameters in the constructor.
您作为服务请求的一部分提供作为参数的对象必须用 [Serializable] 标记,并且根据我之前发布的一些答案,您还需要确保您的自定义对象在构造函数中不包含任何参数。
Also keep in mind any logic you have inside your class will not get created in the proxy class that gets created on the client side. All you will see on the client side is a default constructor and properties. So if you add methods to your custom objects keep in mind that the client will not see them or be able to use them.
还要记住,您在类中拥有的任何逻辑都不会在客户端创建的代理类中创建。您将在客户端看到的只是默认构造函数和属性。因此,如果您向自定义对象添加方法,请记住客户端将看不到它们或无法使用它们。
Same thing goes for any logic you might put into any of the properties.
对于您可能放入任何属性的任何逻辑,情况也是如此。
Example
例子
[Serializable]
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
}
回答by fretje
Edit: removed the part about [Serializable]
编辑:删除了关于 [Serializable] 的部分
Are you creating the service, or consuming it?
您是在创建服务还是在使用它?
To create an object which can be passed as a parameter of a webmethod, you don't have to do anything special. That is if you're creating an asmx webservice.
要创建一个可以作为 webmethod 参数传递的对象,您不必做任何特殊的事情。也就是说,如果您正在创建一个 asmx 网络服务。
OTOH, If you're creating a WCF service then you have to mark the class with [DataContract] and all the members you want to be serialized with [DataMember].
OTOH,如果您正在创建 WCF 服务,那么您必须使用 [DataContract] 标记该类以及您希望使用 [DataMember] 序列化的所有成员。
If you're consuming the webservice, then the proxy classes for the object to pass should be generated when you added the service reference. You just have to use them.
如果您正在使用 Web 服务,则应在添加服务引用时生成要传递的对象的代理类。你只需要使用它们。
回答by DreamSonic
Looks like a duplicate to this question
看起来像这个问题的重复
Anyway, all objects involved in WS interactions should be XML-serializable, not ISerializable (which is binary serialization). Moreover, they should be described in the service contract (WSDL), otherwise clients won't be able to consume them. This articleshould be useful to understand XML-serialization with XML Web Services.
无论如何,WS 交互中涉及的所有对象都应该是 XML 可序列化的,而不是 ISerializable(这是二进制序列化)。此外,它们应该在服务合同 (WSDL) 中进行描述,否则客户端将无法使用它们。这篇文章对于理解使用 XML Web Services 进行 XML 序列化应该很有用。
However, if you are talking about really customobjects (i.e. any type). You should consider passing them in binary form: either as base64-encoded, or as attachments. The question I linked to has an answer how to do that.
但是,如果您谈论的是真正的自定义对象(即任何类型)。您应该考虑以二进制形式传递它们:作为 base64 编码或作为附件。我链接到的问题有一个答案如何做到这一点。