如何通过 WCF 服务发送 IList?

发布于 2024-11-24 04:49:27 字数 125 浏览 3 评论 0原文

我想在我的 WCF 服务中使用 NHibernate,如何通过 WCF 服务发送 IList?

---编辑-----

我可以将 IList 放入 object 变量中并发送吗?

I want to use NHibernate in my WCF service, How can I send an IList via WCF Service?

---EDIT-------

Can I put the IList in an object variable and send it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

闻呓 2024-12-01 04:49:27

尝试传递 IList 对于基于 WCF 的服务来说并不合适。

WCF 根据数据契约进行讨论,定义服务或客户端期望发送和接收的数据的形式。这些契约必须足够具体,以便序列化器知道如何将它们转换为 XML、JSON 和其他表示形式。

当您将像 IList 这样的接口放入服务契约中时,您没有向序列化器提供有关指定数据形状的足够信息。您只关心发送实现的 IList 部分吗?您对实现该接口的内容所包含的任何其他状态数据感兴趣吗?序列化器无法知道。

最简单的解决方案是通过 WCF 使用基本数据传输对象。这些是简单的类,仅负责表示要通过介质传输的信息。这些类仅包含具体类型,并且很容易被序列化器理解。当您想要发送消息时,您可以创建这些对象的实例并用数据填充它们。

对于您的示例,创建 List 而不是 IList 并将值复制到列表中,然后再将其传递到服务中。一种简单的方法是使用 Enumberable.ToList( )扩展方法。

Trying to pass an IList<T> isn't really appropriate for a WCF-based service.

WCF talks in terms of data contracts, defining the shape of the data a service or client will expect to send and receive. These contracts have to be concrete enough for serializers to know how to convert them into XML, JSON and other representations.

When you put an interface like IList<T> into your service contracts, you aren't giving the serializer enough information about the specified shape of your data. Do you care just about sending the IList<T> part of the implementation? Are you interested in any of the other stateful data contained by something implementing the interface? The serializers have no way of knowing.

The simplest solution is to use basic data-transfer objects with WCF. These are simple classes which only have the job of representing information to be transmitted over a medium. These classes contain only concrete types and are easily understood by serializers. When you want to send a message you create instances of these objects and populate them with data.

For your example, create List<T> instead of IList<T> and copy the values into the list prior to passing it into the service. A simple way to do this is with the Enumberable.ToList<T>() extension method.

我一向站在原地 2024-12-01 04:49:27

序列化程序需要知道具体类型,因此使用 [ServiceKnownType] 注释合约,以便它知道哪些类型可用于 IList

Update: 对象,但没有帮助,因为序列化程序仍然不知道什么类型要做的 - 您需要使用 [ServiceKnownType] 告诉它 - 如果您不想在编译时显式显示,您可以在此处提供一个返回类型列表的方法

The serializer needs to know the concrete type so annotate the contract with [ServiceKnownType] so it knows what types could be used for the IList

Update: object doesn't help as the serializer still doesn't know what to do - you will need to tell it using [ServiceKnownType] - you can supply a method here that returns the list of types if you don't want to be explicit at compile time

傲影 2024-12-01 04:49:27

您需要使用泛型并标记列表的类型,以便服务知道会发生什么。方法可以如下所示:

public IList<Something> GetSomething()

也就是说,当我这样做时,我倾向于返回一个数组,而只是因为稍后阅读代码时更少猜测它将返回什么(另一端的非 WCF 客户端是否创建一个数组)它的 IList 类还是将其转换为其他内容?)。大家都知道数组是什么。

You need to use generics and mark what type of list it is so the service knows what to expect. A method can look like this:

public IList<Something> GetSomething()

That said, when I do this I tend to return an array instead just because there's less guessing when reading the code later about exactly what it's going to return (does a non WCF client on the other end create an IList class for it or does it convert it into something else?). Everything knows what an array is.

人海汹涌 2024-12-01 04:49:27

进一步讨论使用列表类型的概念 - 为什么不直接使用数组?

数组似乎是最明显的选择,因为它与 IEnumerable 兼容,并且客户端可以使用自己的扩展方法

Further to the concept of using List type
-- why not just use an array?

An array seems like the most obvious choice as it is compatible with IEnumerable and the client can use their own extension methods

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文