Sunday, March 8, 2015
HttpConnection Limit in .NET
Does your .NET application need to make a large number of HTTP requests concurrently? Be warned that the amount of simultaneous HTTP connections might get throttled by the .NET Framework. Often this can be a good thing, as it is a restriction that is designed to help protect an application from harming a larger system.
Don't worry, you can easily raise the connection limit by adding a simple Connection Management Section to your app.config or web.config:
<configuration>
<system.net>
<connectionManagement>
<add address="*" maxconnection="10000" />
</connectionManagement>
</system.net>
</configuration>
Enjoy,
Tom
Sunday, June 8, 2014
How to stream a FileResult from one web server to another with ASP.NET MVC
MVC has a lot of great built in tooling, including the ability to stream very large file results straight from disk without having to load the whole file stream into memory.
What about the scenario where you want to stream a large file from one web server to another?
For example, I have an ASP.NET MVC application that needs to expose a download for a file hosted on another server, but I can not just redirect my users directly to the other URL. For that, we need to create a custom ActionResult type!
WebRequestFileResult
Here is a simple of example of what your controller might look like:
public class FileController : Controller
{
public ActionResult LocalFile()
{
return new FilePathResult(@"c:\files\otherfile.zip", "application/zip");
}
public ActionResult RemoteFile()
{
return new WebRequestFileResult("http://otherserver/otherfile.zip");
}
}
Monday, December 31, 2012
CookieContainer for HttpWebRequest
Happy New Year!
Are you making a series of HttpWebRequests where you need to persist cookies? For example, perhaps you are experiencing an infinite series of authentication hops. Don't worry, this is very easy to resolve by using the System.Net.CookieContainer class.
xUnit Example
[Theory]
[InlineData("http://www.yahoo.com", 0)]
[InlineData("http://www.google.com", 2)]
[InlineData("http://www.bing.com", 8)]
public void CookieJarCount(string requestUriString, int expectedCookieCount)
{
var request = (HttpWebRequest) HttpWebRequest.Create(requestUriString);
// Create a CookieContainer and assign it to the request.
var cookieJar = new CookieContainer();
request.CookieContainer = cookieJar;
// Make the request like normal.
var response = (HttpWebResponse) request.GetResponse();
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
// Let's see how many cookies are in the cookie jar!
Assert.Equal(expectedCookieCount, cookieJar.Count);
}
Enjoy,
Tom