Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use async/await pattern for remote resource requests #55

Open
jt000 opened this issue Jan 19, 2015 · 0 comments
Open

Use async/await pattern for remote resource requests #55

jt000 opened this issue Jan 19, 2015 · 0 comments

Comments

@jt000
Copy link

jt000 commented Jan 19, 2015

In an ASP.NET application there are a limited number of request threads available. When request threads are all used up, then any future requests will be denied (i.e. a DOS attack). A way to mitigate this is to release a thread while I/O operations are occurring allowing re-use of the thread & push an operation back onto the thread pool when the I/O operation completes.

For example, instead of...

        static string GetGoogleHomepage()
        {
            var request = (HttpWebRequest) WebRequest.Create("http://www.google.com");

            var response = request.GetResponse();

            var s = new StringWriter();
            var buffer = new byte[1024];
            using (var stream = response.GetResponseStream())
            {
                int length;
                while ((length = stream.Read(buffer, 0, 1024)) != 0)
                {
                    s.Write(Encoding.UTF8.GetString(buffer, 0, length));
                }
            }

            s.Flush();
            return s.ToString();
        }

... use the async await pattern to allow other requests to re-use the threads while awaiting IO requests...

        static async Task<string> GetGoogleHomepageAsync()
        {
            var request = (HttpWebRequest)WebRequest.Create("http://www.google.com");

            var response = await request.GetResponseAsync();

            var s = new StringWriter();
            var buffer = new byte[1024];
            using (var stream = response.GetResponseStream())
            {
                int length;
                while ((length = await stream.ReadAsync(buffer, 0, 1024)) != 0)
                {
                    await s.WriteAsync(Encoding.UTF8.GetString(buffer, 0, length));
                }
            }

            await s.FlushAsync();
            return s.ToString();
        }

This can be re-used in the synchronous calls to prevent repetitious code and to prevent build breaks by using the Result property (though, don't use this in the async methods since it blocks a thread).

        static string GetGoogleHomepage()
        {
            return GetGoogleHomepageAsync().Result;
        }

For more information on async\await, see http://msdn.microsoft.com/en-us/library/hh191443.aspx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant