问题描述
我想在WCF服务中使用NHibernate,如何通过WCF服务发送IList?
I want to use NHibernate in my WCF service, How can I send an IList via WCF Service?
---编辑-------
---EDIT-------
我可以将IList放在object
变量中并发送它吗?
Can I put the IList in an object
variable and send it?
推荐答案
尝试通过IList<T>
并不真正适合基于WCF的服务.
Trying to pass an IList<T>
isn't really appropriate for a WCF-based service.
WCF讨论数据合同,定义服务或客户端期望发送和接收的数据的形状.这些合同必须足够具体,以使序列化程序知道如何将其转换为XML,JSON和其他表示形式.
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.
将类似IList<T>
的接口放入服务合同中时,没有为序列化程序提供有关数据指定形状的足够信息.您是否只关心发送实现的IList<T>
部分?您是否对实现接口的某些内容所包含的其他有状态数据感兴趣?序列化器无法知道.
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.
最简单的解决方案是将基本数据传输对象与WCF一起使用.这些是简单的类,仅具有代表要在介质上传输的信息的作用.这些类仅包含具体类型,并且可由序列化程序轻松理解.当您要发送消息时,可以创建这些对象的实例,并用数据填充它们.
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.
在您的示例中,创建List<T>
而不是IList<T>
并将值复制到列表中,然后再将其传递到服务中.一种简单的方法是使用 Enumberable.ToList<T>()
扩展方法.
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.
这篇关于如何通过WCF服务发送IList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!