Dan Nemec's Blog

Programming Information Security Personal

Quick Development Web Server With LINQPad and WebApi Attribute Routing

By Dan Nemec

20 Dec 2013

When writing an HTTP client application, you often need to test unusual server responses that, while in a known format, require you to twist some knobs to force a strange response. Maybe it requires you to register an account on the server and submit various content requests, all of which may take some time if the server you’re using requires explicit approval or is processed every X hours. Instead, let’s serve up canned responses to the requests you’ll be generating!

My first instinct would be to create a folder representing the path you want, drop some html/json/xml files in, and power up Python’s SimpleHTTPServer then point your app to http://localhost:8080 instead of your normal development server. While this works for simple cases, there are three major downsides to this approach:

Dependencies

Once you have LINQPad installed and open, we need to set up our dependencies. I have the Premium version which comes with NuGet integration, but the required DLLs can be downloaded elsewhere and referenced explicitly if you’re using the free version.

Now press F4 and add the following references:

And the following namespace imports:

Setup

Now change your snippet’s Language to C# Program, which will allow us to define our own methods and classes to setup WebApi.

First bit of code you can straight copy-paste into the panel. Since LINQPad generates your defined classes nested within its own UserQuery class, WebApi’s default ControllerTypeResolver cannot find our controller. This class will reset the resolver so that it can find our code. Major thanks to StrathWeb for the tip.

public class ControllerResolver : DefaultHttpControllerTypeResolver 
{
	public override ICollection<Type> GetControllerTypes(IAssembliesResolver assembliesResolver) 
	{
		var types = Assembly.GetExecutingAssembly().GetExportedTypes();
		return types.Where(i => typeof(IHttpController).IsAssignableFrom(i)).ToList();          
	}
}

Now, we can define a “startup” class responsible for configuring WebApi. Note that we replace the original controller resolver here and map our (future) attribute routes here.

public class Startup
{
	public void Configuration(IAppBuilder appBuilder)
	{
		var config = new HttpConfiguration();

		// LINQPad generates nested classes, not
		// detectable by default resolver.
		config.Services.Replace(
			typeof(IHttpControllerTypeResolver), 
			new ControllerResolver());

		config.MapHttpAttributeRoutes();
		appBuilder.UseWebApi(config);
	}
}

Here’s our Main function which will be responsible for starting and tearing down our self-hosted server. One really neat feature of LINQPad is that it doesn’t necessarily tear down your process once the script is done. You can inject UI controls into the output panel to allow interactive execution of your scripts. We’ll be using a Button to give us control over when our web server shuts down.

void Main()
{
	var baseAddress = "http://localhost:8080";
	var app = WebApp.Start&lt;Startup&gt;(baseAddress);

	var button = new Button { Text = "Click to stop server." };
	var panel= PanelManager.DisplayControl(button, "Webserver");

	button.Click += (o, e) =&gt;
	{
		app.Dispose();
		panel.Close();
	};
}

The Fun

Now for the meat of the script, our ApiController! Let’s say, for example, that we are making two HTTP calls:

Under normal WebApi, these nested paths would probably require some complex route configuration, but with the new Attribute Routing it’s all handled automagically based on the route we define for our Action method. Here’s what our controller will look like:

public class MainController : ApiController
{
	[Route("some/complex/path/{id}")]
	// WebApi parameter binding will turn {id} in the path into an argument and
	// pull 'lc' and 'cc' from the query string (with default values) automatically.
	public HttpResponseMessage Post(string id, string lc = "en", string cc = "us")
	{
		// Print some debug data during the request.
		// Will appear in Results panel.
		(lc + "-" + cc).Dump();
		return StaticFile("c:/users/dan/somedoc.html");
	}

	[Route("some/other/path")]
	// Note that, using WebApi, defining the HTTP Verb used for a particular
	// action is based on the name of the method.
	public string Get()
	{
		throw new HttpResponseException(HttpStatusCode.InternalServerError);
	}
}

To finish things up here’s the implementation of StaticFile, a method that reads a file from disk and returns it as HTML:

private static HttpResponseMessage StaticFile(
	string localFilename, string contentType = "text/html")
{
	if(File.Exists(localFilename))
	{
		return new HttpResponseMessage(HttpStatusCode.OK)
		{
			Content = new StringContent(
					File.ReadAllText(localFilename),
				Encoding.UTF8, contentType)
		};
	}
	else
	{
		return new HttpResponseMessage(HttpStatusCode.NotFound)
		{
			Content = new StringContent("File " + localFilename + " does not exist.")
		};
	}
}

Now click the Run button and try it out! Take a look at the nice, big button that you can click when you’re ready to turn off the web server:

Stop Button

You can save this whole snippet as a Query or a Sample and modify the Controller to return whatever data your heart desires next time you need it for testing.

Bonus: Iterator blocks

Now, in the course of making requests, you might need to perform some “state change” between requests to a single route so that your first request returns one value and the second returns a different once (to reflect outside database updates or some similar situation). Instead of messing around with static fields (remember, ApiController instances are per-request) and state machines, let a couple of helper methods do it for you! Here’s a sample method that will return three different values:

private static IEnumerable<string> PrivMultipleRequests()
{
	yield return "first";
	yield return "second";
	yield return "third";
}

And the route? Here’s where the magic comes in:

[Route("multi/reqs")]
public string GetMultipleRequests()
{
	return Multiple<string>.Next(PrivMultipleRequests);
}

The static Multiple class caches your first call to Next from the GetMultipleRequests method and makes sure the Enumerator is advanced with each call. If you try to make a fourth GET request to /multi/reqs an exception will be thrown (although you could always add an infinite loop of some sort at the end of your iterator block if you want to return a default value instead). Here’s how Multiple works:

private static class Multiple<T>
{
    private static Dictionary<string, IEnumerator<T>> _iters = new Dictionary<string, IEnumerator<T>>();
    
    public static T Next(Func<IEnumerable<T>> init, string uniqueKey = null)
    {
        var callingMethod = new StackTrace().GetFrame(1).GetMethod();
        if(uniqueKey == null)
        {
            // Magic to uniquely identify a specific method called from a specific function.
            // This way, you can pass the same delegate in from different routes and get
            // a new enumerator each time.
            uniqueKey = callingMethod.DeclaringType.FullName + "." + 
                        callingMethod.Name;
        }
        var key = uniqueKey + ":" +
                  init.Method.DeclaringType + "." +
                  init.Method.Name;
        IEnumerator<T> enm;
        if(!_iters.TryGetValue(key, out enm))
        {
            _iters[key] = enm = init().GetEnumerator();
        }
        if(enm.MoveNext())
        {
            return enm.Current;
        }
        throw new InvalidOperationException(String.Format(
            "Too many calls to resource {0} from {1}", init.Method.Name, callingMethod.DeclaringType));
    }
}
Click to display comments. Why? Many hosted comment systems use their reach to track users as they browse, similar to how Facebook like buttons can see when you load a page that embeds the button. That information is used to feed you targeted ads (among other things). Disqus will not be contacted until you open the comments.