从WCF Serivce返回大量收藏 [英] Returning large collections from WCF Serivce

查看:36
本文介绍了从WCF Serivce返回大量收藏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定构建WCF服务的最佳方法,而我最努力的领域是返回对象列表.

I'm trying to determine the best approach for building a WCF Service, and the area I'm struggling with most is returning lists of objects.

内置的64k的maxMessageSize似乎很高,我真的不想提高它(快速谷歌搜索发现有100多个地方将maxMessageSize提升到了数GB的范围,这似乎很愚蠢).但是,当我返回一组对象(约150个项目)时,我超出了默认的64k.

The built-in maxMessageSize of 64k seems pretty high, and I really don't want to bump it up (quick googling finds 100s of places bumping the maxMessageSize up to multi-gigabyte range which seems foolish). But, when I'm returning a collection of objects (~150 items) I am exceeding the default 64k.

我几乎要返回我自己的类,该类继承IEnumerable并具有hasNext,hasPrevious和PageSize的属性,以便可以在客户端实现分页-这似乎是很多代码.另一个选择是增加maxMessageSize并希望达到最佳效果,但这是错误的.

I'm almost to the point of returning my own class which inherits IEnumerable and has properties for hasNext, hasPrevious and PageSize so that I can implement paging on the client side -- this seems like alot of code. The other option is to jackup the maxMessageSize and hope for the best, but that feels wrong.

我的服务的所有其他方面都运作良好,它只是返回我遇到问题的大型收藏夹.

All other aspects of my service are working great, its just returning large collectiosn where I'm having issues.

在后台,此服务有两种类型的使用者,即主要是Web和/或wpf应用程序的UI应用程序,以及数据处理应用程序,.NET控制台应用程序,也许还有其他一些非UI应用程序.对于UI应用程序,我想保持它们的响应速度,并保持messageSize较小,在控制台应用程序上,这并不重要,因为它们只是拉下数据来进行处理并将其推回服务中.

For background, there are two types of consumers of this service, UI applications which will be primarly web and/or wpf applications, and data processing applications, .NET console apps, and maybe some other non-UI apps. For the UI applications, I would like to keep them responsive and keep the messageSize low, on the console apps it doesn't matter as much as they are just pulling data down to do processing and push it back up to the service.

推荐答案

我相信maxMessageSize的默认值如此之低的原因是为了降低DoS攻击的风险.

I believe the reason the default value for maxMessageSize is so low is to reduce the risk of DoS attacks.

如果响应消息很大,则 client 配置需要增加maxMessageSize.对于客户而言,DoS不太可能构成风险,因此将其增加到非常大的值是安全的.

If response messages are large, it's the client configuration that needs an increased maxMessageSize. For a client, DoS is unlikely to be a risk, so it's safe to increase it to a very large value.

但是,这并不是劫持maxMessageSize并希望达到最佳效果"-您应考虑是否决定使用分页,并适当配置它,从而决定应用程序的预期最大大小.

But this isn't "jacking up maxMessageSize and hoping for the best" - you should decide what the expected maximum size is for your application, taking into account whether or not you decide to use paging, and configure it appropriately.

在服务器上,maxMessageSize必须足够大以容纳允许的最大 request 消息.尽管在某些环境(例如Intranet)中,使用非常大的值可能是安全的,但DoS可能是一个问题.

On a server, maxMessageSize needs to be big enough for the maximum allowed request message. Here DoS is potentially an issue, though in some environments (e.g. Intranet) it is probably safe to use a very large value.

如果您的服务正在公开允许客户端查询潜在大数据集的操作,则另一种分页方法是定义一次呼叫中客户端可以请求的最大项目数.

If your service is exposing an operation that allows a client to query a potentially large data set, an alternative approach to paging is to define a maximum number of items that a client can request in one call.

例如,您可能具有允许客户请求货币列表和日期范围的汇率的操作:

For example, you might have an operation that allows a client to request exchange rates for a list of currencies and a range of dates:

public IList<ExchangeRate> GetExchangeRates(string baseCurrency, IList<string> currencies, DateTime startDate, DateTime endDate);

然后您可以设置服务将返回的结果数的限制,并将其公开给客户端:

You could then set a limit on the number of results your service will return, and expose this to the client:

public int GetMaximumResultCount();

然后将要求客户端查询 GetMaximumResultCount ,并确保:

The client would then be expected to query GetMaximumResultCount, and to ensure that:

(endDate - startDate).TotalDays * currencies.Count < maximumResultCount

如果客户端不遵守此规定,则服务器将引发适当的 FaultException

If the client fails to respect this, the server throws a suitable FaultException

这篇关于从WCF Serivce返回大量收藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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