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

本文是小编为大家收集整理的关于许多并发客户端和数据库访问的WCF服务的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我是WCF服务的新手,想知道解决以下内容的最佳方法是什么.

我有很多客户(〜200 - 〜500)在工作日中不断地向我的服务要求.大多数请求涉及对基础数据库进行询问以回馈正确的响应.

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

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

我已经考虑使服务成为单个线程的单身人士,该单元源于数据库事务,以便我可以控制线程的数量,但是这是过度的,并且可以限制与服务的连接足够吗?

非常感谢您的任何建议.

推荐答案

正如Marcos已经提到的-WCF具有内置服务节流功能,您可以在服务器上进行调整.这样可以防止您的数据库服务器一次充斥太多请求.

默认值为:

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

请参阅 serviceThrottlottlottlingbebehavior a> msdn文档有关更多详细信息.

这意味着WCF同时处理最多16个呼叫 - 也就是说,如果您的WCF服务类别允许多个呼叫者!

与Marcos相反,我会不建议您将WCF服务班级A Singleton推荐.普遍的最佳实践是拥有一个简单的WCF服务类,并以每个通话方式使用它 - 例如每个传入的请求将获得您的WCF服务类的完全独立,新创建的实例 - 最多由服务节流行为定义并由WCF运行时控制.

.

如果您将WCF服务类作为单身人士,则必须将其并发模式设置为多个 - 但是您需要使用Extreme注意,不要让同类中的两个同时线程相互改变相同的值;多线程安全编程是一个主要挑战!或者,您不会将并发模式设置为多个,但是您的一个和唯一的WCF服务类实例只能以串行方式处理请求,一次是一个 - 不是很可扩展!

每句话和每个请求的一个服务实例绝对是更简单的方法.这是服务的到位,并且随着Ado.net连接池的汇集,可以实现一个非常强大且行为良好的环境!

另请参见 DAN RIGSBY在WCF服务节点上的出色博客文章甚至更多细节.

其他推荐答案

如果您使用ADO连接到数据库,则应提供连接池机制,因此您无需处理.

请,请阅读本文以获取更多信息:

其他推荐答案

我们也有类似的场景,我们只使用网站服务到DB的一项对db的反理解决,并使用SQLServer的火星来解决完美和快速的MARS. SQL Server确实知道如何处理您不必考虑的并发查询.

您避免了开放和关闭连接的开销(显而易见的是,在这种情况下,连接池有助于)

还请记住要添加您的Web服务,例如:

  [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,
        ConcurrencyMode = ConcurrencyMode.Multiple)]

和在配置中:

<serviceThrottling maxConcurrentCalls="100"
 maxConcurrentSessions="100" 
 maxConcurrentInstances="100" />

此选项在内部

  <behaviors>
      <serviceBehaviors>
        <behavior name="...">

希望这会有所帮助:)

本文地址:https://www.itbaoku.cn/post/597741.html

问题描述

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.

其他推荐答案

If you're using ADO to connect to your database, it should to provide a connection pooling mechanism, so you don't need to deal with this.

Please, read this article for more information: ADO.NET Connection Pooling at a Glance

其他推荐答案

We have a similar scenario and we solve it using only one conection from our WebService to the DB and using MARS from SqlServer that works perfectly and pretty fast. Sql Server really knows how to handle concurrent queries you don't must think in that.

You avoid with that the overhead of open and close the connections (obviosly that connection pooling helps in this case)

Remember also to add in your web service something like:

  [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,
        ConcurrencyMode = ConcurrencyMode.Multiple)]

And in the config:

<serviceThrottling maxConcurrentCalls="100"
 maxConcurrentSessions="100" 
 maxConcurrentInstances="100" />

This option is inside

  <behaviors>
      <serviceBehaviors>
        <behavior name="...">

Hope this helps :)