IEnumerable的< T>作为WCF方法的返回类型
如果我定义一个Test对象,使用string和Datetime属性并使用它来返回WCF中的IEnumerable(T)集合
If i define a Test object, with string and Datetime properties and use it to return IEnumerable(T) collection in WCF
[OperationContract]
IEnumerable<Test> GetTestNotes();
当从客户端调用服务时,我看到它正在将IEnumerable转换为Test [] :
and when calling the service from the client, I see that it is converting the IEnumerable to Test[]:
public Test[] GetTestNotes() {
return base.Channel.GetTestNotes();
}
我能够获得数据。
问题是:使用IEnumerable(T)接口而不是具体的List(T)有多可靠?
Question is: How reliable is it to use IEnumerable(T) interface rather than concrete, List(T)?
我的客户端使用这些WCF服务不仅在.NET中,而且在JAVA中。
My clients that consume these WCF services are not only in .NET but also in JAVA.
如果您正在使用Visual Studio生成的服务引用,那么可以选择用于集合的类型。右键单击Service References下的服务,然后选择 Configure Service Reference… 。您应该看到以下选项:
If you're using the Visual Studio generated service references, you can pick what type is used for collections. Right-click on the service under Service References and select Configure Service Reference…. You should see these options:
但是,这仍然不允许您选择 IEnumerable< T>
。如果您希望更严格地控制客户端界面的外观,最好的办法是在单独的程序集中定义合同,然后在客户端和服务器上引用这些程序集。
However, this still won't allow you to select IEnumerable<T>
. If you want tighter control over what the client interface looks like, your best bet is to define the contracts in a separate assembly and then reference those assemblies on both the client and server.