将自定义对象传递给 Web 服务 [英] Passing custom objects to a web service

查看:33
本文介绍了将自定义对象传递给 Web 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将自定义对象传递给远程 Web 服务.我已经读到可能有必要实现 ISerializable,但我已经这样做了,但遇到了困难.在 C# 中将自定义对象传递给 Web 服务方法的正确方法是什么?

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?

推荐答案

您在服务请求中作为参数提供的对象必须标有 [Serializable] 并且基于我之前发布的一些答案,您还需要确保您的自定义对象在构造函数中不包含任何参数.

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.

还要记住,您在类中拥有的任何逻辑都不会在客户端创建的代理类中创建.您将在客户端看到的只是默认构造函数和属性.因此,如果您向自定义对象添加方法,请记住客户端将看不到它们或无法使用它们.

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.

示例

[Serializable]
public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
}

这篇关于将自定义对象传递给 Web 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆