Queueing up Service Calls

落花浮王杯 提交于 2019-12-12 15:51:25

问题


I'm implementing a Silverlight application that uses WCF services heavily, I've got to the point now where occasionally there are several long service calls that block other service calls from running.

These service calls eventually time out. I'd like to see if its possible to a queue system that executes service calls one after another, this way long calls will hold up other calls but won't cause them to timeout.

I'm using service agents to wrap the service calls

public interface IExampleServiceAgent
{
    void ProcessData(int a, string b, EventHandler<ProcessDataCompletedEventArgs> callback);
}


Public ExampleServiceAgent1 : IExampleServiceAgent
{
     ExampleClient _Client = new ExampleClient();

     public void ProcessData(int anInt, string aString, EventHandler<ProcessDataCompletedEventArgs> callback)
     {
           EventHandler<ProcessDataCompletedEventArgs> wrapper = null;
           wrapper = (a,b) =>
           {
               callback(a,b);
               _Client.ProcessDataCompleted -= wrapper;
           }
           _Client.ProcessDataCompleted += wrapper;
           _Client.ProcessDataAsync(anInt,aString);
     }
}

The above service agent would then be called from code as follows:

ServiceAgent.ProcessData(1,"STRING", (a,b) =>
{
    if (b.Error != null)
    {
         //Handle Error
    }
    else
    { 
         //DO something with the data
    }
}

Is there a way I could put these service calls into a queue and execute them one by one?

I've tried wrapping them as Actions and adding them to a queue, but this does not wait for one to finish executing before starting the next one and although they do call the service correctly no data is returned to the calling ViewModel.


回答1:


WCF services can cope with a huge number of calls but, to avoid denial of service attacks, the number of requests that can be processed is limited by default.

The significant limitations for Silverlight WCF services would be

  • A default limit of 2 simultaneous calls from the same IP address.
  • A limit of approx 10-16 concurrent connections (documentation varies on this one).

This CodeProject article on Quick Ways to Boost Performance and Scalability of ASP.NET, WCF and Desktop Clients was useful.

I am guessing you are immediately hitting the first issue. In your WCF config you need to add the following to increase the single IP connections:

<system.net> 
  <connectionManagement> 
    <add address="*" maxconnection="100" /> 
  </connectionManagement> 
</system.net>

You may then hit the second limit for which the solution is tweak the service behaviors in the web/app.config files.

Here are a few more references I found while sorting out these issues myself:

  • http://weblogs.asp.net/paolopia/archive/2008/03/23/wcf-configuration-default-limits-concurrency-and-scalability.aspx
  • Why does WCF limit concurrent connections to 5?
  • http://msdn.microsoft.com/en-us/magazine/cc163590.aspx#S10
  • http://blogs.msdn.com/b/stcheng/archive/2009/01/06/wcf-things-that-will-impact-concurrency-capacity-behavior-of-wcf-service-with-simoultaneous-client-requests-connections.aspx
  • http://www.codeproject.com/Articles/133738/Quick-Ways-to-Boost-Performance-and-Scalability-of
  • http://www.danrigsby.com/blog/index.php/2008/02/20/how-to-throttle-a-wcf-service-help-prevent-dos-attacks-and-maintain-wcf-scalability/
  • http://msdn.microsoft.com/en-us/library/7w2sway1%28v=vs.71%29.aspx
  • http://www.codeproject.com/Articles/89858/WCF-Concurrency-Single-Multiple-and-Reentrant-and
  • WCF stops responding after about 10 or so calls (throttling)


来源:https://stackoverflow.com/questions/9872342/queueing-up-service-calls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!