Search code examples
asp.netasp.net-mvcazureazure-web-rolesazure-configuration

Azure Web Role - Maximum outbound http connections


I am building an Azure Web Role that will need to invoke a lot of external web services.

I remember for ASP.Net 1.1, there was a default maxconnection limit of 2, which you could adjust if your application needed more than that (which was pretty low, let's be honest). On ASP.Net 2.0, I believe they raised this, and the recommendation was 12 times the number of cores.

What are the limits, and the means of adjusting this, on Azure Web Roles? I will have hundreds, or thousands, of concurrent outbound requests going at once. Does this mean that I need bigger instances (more cores), or can I do this by using more "Extra Small" instances and configuring them correctly (even if that means 12 per server)?

Thank you.


Solution

  • The size of your instance will only determine the bandwidth that will be reserved for your instance (XS is 5 Mbps, more info here). What you'll need to do is simply change the DefaultConnectionLimit to more than 2:

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

    Add this to your web.config if you want to allow this in your web application. Add the following to your WebRole.cs if you want to invoke services before starting your instance for example:

        public override bool OnStart()
        {
            ServicePointManager.DefaultConnectionLimit = 12;
            return base.OnStart();
        }
    

    Keep in mind that, even if the requests are queued, you'll get a better overall performance if you call the webservices in an asynchronous way. Here is a very basic example (assuming you call a simple REST service, WCF client proxies have better support for asynchronous requests):

    <%@ Page Async="true" ... %>
    
    public partial class AsyncPage : System.Web.UI.Page
    {
        private WebRequest req;
    
        void Page_Load (object sender, EventArgs e)
        {
            AddOnPreRenderCompleteAsync (
                new BeginEventHandler(BeginWebServiceCall),
                new EndEventHandler (EndWebServiceCall)
            );
        }
    
        IAsyncResult BeginWebServiceCall (object sender, EventArgs e, 
            AsyncCallback cb, object state)
        {
            req = WebRequest.Create("http://some.webs.service/rest/api");
            return req.BeginGetResponse (cb, state);
        }
        void EndWebServiceCall (IAsyncResult ar)
        {
            using (var response = req.EndGetResponse(ar))
            {
                using var reader = 
                    new StreamReader(response.GetResponseStream()))
                {
                    var text = reader.ReadToEnd();
            ...
                }
            }
        }
    }