WCF服务用于许多并发客户端和数据库访问 [英] WCF Service for many concurrent clients and database access

查看:160
本文介绍了WCF服务用于许多并发客户端和数据库访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有很多客户端(〜200〜〜500),这是我最喜欢的WCF服务。都在工作日期间不断地提出我的服务请求。大多数请求涉及查询底层数据库以反馈正确的响应。



我关心的是从传入请求中产生的潜在数据库连接数。如果所有客户端同时发出请求,则数据库服务器将被强制命中。如果可能,我想避免连接到数据库的愚蠢数量。



最好限制与WCF服务的并发连接数,因此不经意减少可能的数据库连接数量?



我已经看过使服务成为一个单例,它产生线程来做数据库事务,所以我可以控制线程数,但是

解决办法:



方案

正如Marcos已经提到的--WCF有一个内置的服务调节功能,您可以在服务器上进行调整。



默认值为:

 < serviceThrottling 
maxConcurrentCalls =16
maxConcurrentSessions =10
maxConcurrentInstances =26/&

请参阅有关详细信息,请参阅ServiceThrottlingBehavior的MSDN文档。



这意味着最大的16个调用由WCF同时处理 - 即 IF ,您的WCF服务类允许一次调用多个调用者。



与Marcos将建议使您的WCF服务类为单身。常见的最佳实践是具有简单的WCF服务类,并且以每个呼叫方式使用它。每个传入的请求将获得自己的,完全独立的,新创建的WCF服务类的实例 - 达到由服务调节行为定义的并由WCF运行时控制的最大值。



如果你使你的WCF服务类是一个单例,你必须将其并发模式设置为多个 - 但是你需要非常小心,不要让你的类中的两个并发线程改变相同的值从下一个;多线程安全编程是一个重大挑战!或者你不将并发模式设置为Multiple,但是你的唯一的WCF服务类实例只能以串行方式处理请求,一次一个 - 不是非常可扩展!



每个请求的每个调用和一个服务实例绝对是更容易的方式。服务调节到位,并与ADO.NET连接池,使一个非常强大和行为良好的环境!



另见 Dan Rigsby的关于WCF服务调节的优秀博文更详细。


I'm new to WCF services and wondered what the best way to tackle the following would be.

I have many clients (~200 - ~500) that are all making requests of my service fairly constantly during the working day. Most requests involve interrogation of an underlying database to feed the correct response back.

What I'm concerned with is the potential number of database connections spawned from the incoming requests. If all clients make simultaneous requests then the database server will be hit hard. I'd like to avoid a silly number of connections to the database if possible.

Would it be better to restrict the number of concurrent connections to the WCF service and hence inadvertently reduce the possible number of database connections?

I've looked at making the service a singleton that spawns threads to do the database transaction so I can control the number of threads but is this overkill and would restricting connections to the service suffice?

Many thanks for any advice.

解决方案

As Marcos already mentioned - WCF has a built-in service throttling capability, which you can tweak on the server. This prevents that your database server will be flooded with too many requests at once.

The defaults are:

<serviceThrottling 
      maxConcurrentCalls="16"
      maxConcurrentSessions="10" 
      maxConcurrentInstances="26" />

See the MSDN docs on ServiceThrottlingBehavior for more details.

This means that a maximum of 16 calls are handled concurrently by WCF - that is, IF your WCF service class allows multiple callers at once!

Contrary to Marcos, I would not recommend making your WCF service class a singleton. The common best practice is to have a simple WCF service class, and use it in a per call fashion - e.g. each incoming request will get its own, totally separate, newly created instance of your WCF service class - up to a maximum as defined by the service throttling behavior and controlled by the WCF runtime.

If you make your WCF service class a singleton, you have to either set its ConcurrencyMode to Multiple - but then you need to use extreme caution not to let two simultaneous threads in your class change the same values from under one another; multi-threading safe programming is a major challenge! Or you don't set the concurrency mode to Multiple, but then your one and only WCF Service class instance can only handle requests in a serial fashion, one at a time - not very scalable!

Per-call and one service instance per request is definitely the much easier way to go. That with service throttling in place, and with ADO.NET connection pooling makes for a very powerful and well behaved environment!

Also see Dan Rigsby's excellent blog post on WCF service throttling for even more detail.

这篇关于WCF服务用于许多并发客户端和数据库访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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