简体   繁体   English

WCF服务限制设置以实现与SQL事务的并发

[英]WCF Service Throttling settings for concurrency with SQL Transaction

I have a WCF service that has a complex operationcontract that has to executed atomically ie either the entire operation succeeds or fails. 我有一个WCF服务,该服务具有复杂的操作合同,必须自动执行,即整个操作成功或失败。 The WCF service is hosted on IIS server in an ASP .NET application . WCF服务托管在ASP.NET应用程序的IIS服务器上 This operation has a sequence of SQL commands that execute in a transaction. 此操作具有在事务中执行的一系列SQL命令。 During tests I found that with concurrent access by 4 - 5 users, atleast one user gets "Transaction Deadlock" error. 在测试期间,我发现4-5个用户并发访问时,至少有一个用户遇到“交易死锁”错误。

I then looked at the serviceThrottling settings which I had set to 然后,我查看了已设置为的serviceThrottling设置

<serviceThrottling  maxConcurrentCalls ="5" maxConcurrentInstances ="50" maxConcurrentSessions ="5" />

and changed it to 并将其更改为

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

I have turned off session since I don't need in the service contract. 由于我不需要服务合同,因此我关闭了会话。 So I don't know whether maxConcurrentSessions will be having any effect at all 所以我不知道maxConcurrentSessions是否会产生任何影响

<ServiceContract([Namespace]:="http://www.8343kf393.com", SessionMode:=SessionMode.NotAllowed)>

This way I was queuing up the requests so that the request are processed serially instead of concurrently. 这样,我对请求进行排队,以便按顺序而不是同时处理请求。 While the transaction issue got away, the process time increased which was expected. 交易问题解决后,处理时间却增加了,这是预期的。

I was wondering 我在想

  1. Whether serviceThrottling is the only way to resolve this issue ? serviceThrottling是否是解决此问题的唯一方法?

  2. How can I set serviceThrottling such that while the service will accept many requests at the same time but will process one at a time? 如何设置serviceThrottling,以便在服务同时接受许多请求但一次处理一个请求的同时呢?

  3. Is setting the InstanceContextMode=InstancePerContext.PerCall relevant here since the application is ASP .Net application which in itself is multithreaded ? 因为应用程序是本身是多线程的ASP .Net应用程序,所以在此处设置InstanceContextMode = InstancePerContext.PerCall是否相关?

  1. Well, i think your going about this the wrong way trying to solve a database deadlock with WCF throttling. 好吧,我认为您尝试使用WCF节流来解决数据库死锁的方式是错误的。 you should try to understand why your database operations causes a deadlock, and try to avoid it (by using maybe locking hints.) 您应该尝试了解数据库操作为何导致死锁,并尝试避免死锁(通过使用可能的锁定提示)。
  2. a singleton will do what you ask , but that isnt very scalable. 单身人士可以满足您的要求,但这并不是很容易扩展。
  3. it is relevant but i think you get my drift , solve the deadlock in the database not in WCF. 这是相关的,但我认为您能解决问题,解决数据库中的死锁而不是WCF。

if its SQL server that you are using , theres a great tool to analyze deadlocks (and a lot more) and its called the SQL Profiler. 如果您使用的是它的SQL Server,则有一个很棒的工具来分析死锁(还有更多),它叫做SQL事件探查器。 Also its a fairly well documented topic in the SQL Books Online 在SQL联机丛书中,它也是一个相当有据可查的主题

Your changes caused the WCF service to function as a singleton instance. 您的更改导致WCF服务充当单例实例。 That fixed your database concurrency issue but it only pushed the process blocking into the client. 这解决了您的数据库并发问题,但仅将进程阻塞推入了客户端。

I'd recommend using a different approach to remove the client blocking penalty. 我建议使用另一种方法来消除客户端阻塞罚款。 You should consider making this service, or at least extracting that operation into a new service that uses a netMsmqBinding (a good overview is here) . 您应该考虑提供此服务,或者至少考虑将该操作提取到使用netMsmqBinding的新服务中(此处有一个很好的概述) This means the client will never be blocked and it guarantees delivery of the request to the service. 这意味着客户端将永远不会被阻塞,并保证将请求传递到服务。 The tradeoff is there can be no immediate response to the request, you'll need to add another operation to poll for completion status and to retrieve any expected results. 折衷方案是无法立即响应请求,您需要添加另一个操作以轮询完成状态并检索任何预期结果。 It does require more work to spin up an MSMQ based service but the reliability is usually worth the effort. 要启动基于MSMQ的服务确实需要更多的工作,但是可靠性通常值得付出努力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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