简体   繁体   中英

IEnumerable<T> as return type for WCF methods

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();

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();
} 

And Im able to get data.

Question is: How reliable is it to use IEnumerable(T) interface rather than concrete, List(T)?

My clients that consume these WCF services are not only in .NET but also in JAVA.

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:

配置服务参考

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM