Archive for July, 2011
IIS 7.x and HttpResponse.Headers
Posted by suddenelfilio in .net, Asp.net, Programming, Web Services on 15/07/2011
Today I was debugging some application I wrote that was written with an application pool’s mode set to Integrated mode in mind. One of my colleagues told me I needed to test it running in an application pool with its mode set to Classic.
And guess what? Boom… it blew up in my face! After cursing around a bit I discovered in my log files the following message:
ERROR An Exception was thrown:System.PlatformNotSupportedException: This operation requires IIS integrated pipeline mode.
at System.Web.HttpResponse.get_Headers()
The defect piece of code was this:
HttpContext.Current.Response.Headers.Add("Some-Header","Some-Value");
Apparently to do that you need the IIS integrated pipeline mode. Well if you really need to run your app pool in Classic mode just replace your code with the following and all is well:
HttpContext.Current.Response.AppendHeader("Some-Header","Some-Value");
I know it’s kind of silly from Microsoft to not make this both ways compatible.
Intercept values using Microsoft Moles
Posted by suddenelfilio in .net, Programming, Visual Studio .Net on 05/07/2011
Recently I needed to test some code in the most untestable codebase I’ve ever seen. Problem I was experiencing was that the value I needed to test was never exposedm but I knew what happened in the code. So I used Microsoft Moles to capture a value that was calculated in the method but never exposed. Below I’ll demonstrate the technique using a sample.
Let’s start with some code that is located in an external library:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SomeExternalLibrary
{
public class Utilities
{
public bool FormatStringContainsUpperCases(string template, params object[] arguments)
{
var formattedString = GetFormattedText(template,arguments);
return formattedString.ToLower() != formattedString;
}
public string GetFormattedText(string t, params object[] a)
{
return string.Format(t, a);
}
}
}
As you can see in the (useless) piece of code above we have a function that will format a string using a template and arguments and then evaluate if the formatted string contains uppercases. Now let’s say in a test we want to be sure that the formatted string is as we expect. We need a way to get the formatted result in the method FormatStringContainsUpperCases. This can be done using Microsoft Moles because we can create a mole library of the SomeExternalLibrary.dll which allows us to stub the Utilities class.
Once the Moles library is created we can start writing the Test code. Below you can see the entire test class:
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SomeExternalLibrary;
using SomeExternalLibrary.Moles;
namespace SomeTestProject
{
[TestClass]
public class UnitTest1
{
[TestMethod]
[HostType("Moles")]
public void TestUtilities()
{
var inputTemplate = "this is the {0}";
var expectedFormattedString = "this is the Template";
var formattedString = "";
var classUnderTest = new Utilities();
MUtilities.AllInstances.GetFormattedTextStringObjectArray = (s, template, arguments) =>
{
MUtilities.AllInstances.GetFormattedTextStringObjectArray = null;
formattedString = classUnderTest.GetFormattedText(template, arguments);
return formattedString;
};
Assert.IsTrue(classUnderTest.FormatStringContainsUpperCases(inputTemplate, "Template"));
Assert.AreEqual(expectedFormattedString, formattedString);
}
}
}
In the test above we know that in the FormatStringContainsUpperCases method the GetFormattedString method is called. We setup interception using the MUtilities stub and attach a delegate to the GetFormattedString method for all instances of the Utilities class.
Next we remove the delegate we have just attached. If you do not do this the next line of code will cause a StackOverflowException since the code will get in an endless loop calling itself over and over.
MUtilities.AllInstances.GetFormattedTextStringObjectArray = null;
After removing the delegate we can call the original method GetFormattedString on the original class Utilities. This will then return the same result as it would when called in the FormatStringContainsUpperCases method. This result we can store in variable. The last step is to return variable so the calling code can finish correctly.
So to intercept a call you:
- Attach a delegate that will handle the interception.
- In the delegate body remove the interception delegate to prevent a StackOverflowException
- Call the original method and store the result in a variable
- Return the variable so the calling code can execute as if it would without the interception.

