ASP.NET Web API and dependencies in request scope

Regardless of what types of applications you are building with Web API, sooner or later you will run into a situation where you need to scope certain dependencies to the lifetime of a web request.

Examples of these types of in request scope dependencies could be loggers, unit of work implementations, NHibernate session, RavenDB session, Mindscape Lightspeed UnitOfWork or many other. Fortunately, this is very easy to do with Web API and an IoC container.

More after the jump.

Adding Session support to ASP.NET Web API

First the disclaimer. Yes, there are a lot of problems with using session in your ASP.NET applications. Moreover, by default, HTTP (and by extension, REST) is stateless - and as a result each HTTP request should carry enough information by itself for its recipient to process it to be in complete harmony with the stateless nature of HTTP.

So if you are designing a proper API, if you are a REST purist, or if you are Darrel Miller, you definitely do not want to continue reading this article. But if not, and you find yourself in a scenario requiring session - perhaps you are using Web API to facilitate your JS or MVC application, and you want to sync the state of Web API with the state of MVC easily, or you simply want a quick and easy way to persist state on the server side - this article is for you.

More after the jump.

ASP.NET Fall 2012 update (preview) is out – what’s new for Web API developers?

This evening, Microsoft has released a Fall 2012 Update to ASP.NET. The release brings a whole set of goodies for ASP.NET developers, including SignalR (now officially part of ASP.NET), C# FB app project template, Windows Azure Authentication and more. Jon Galloway has the updates very nicely summarized here.

Of course, as you’d expect, there are plenty of updates regarding Web API. Let’s have a look at them.

More after the jump.

Clean up your Web API controllers with model validation and null check filters

Regardless of what kind of applications you build with Web API, you are bound to write a lot of similar code in many of your actions - to check if the model is valid and to check if the submitted object is null.

This creates unnecessary noise and repetition. In this short blog post, let’s have a quick at how you can delegate this kind of repetitive logic to Web API filters.

More after the jump.

Send text messages (SMS) from Web API using Azure Mobile Services

Few weeks ago, I blogged about using Azure Mobile Services in your Web API applications and I think I managed to convince some of you that ZUMO with its REST-style API is really useful and super easy to work with.

The guys at Azure Mobile Services are not slowing down at all. Today Scott Gu announced a whole new super cool set of upgrades, and one of the coolest features is the integration to Twillio and the ability to send text messages (SMS) directly from the ZUMO script.

Let’s go ahead and extend our previous ZUMO example (managing a list of sports team) with SMS messaging.

Remote printing with ASP.NET Web API in Windows Service

We all have different devices at home, right? So I had this idea, why not make a small Web API driven Windows service to sit on my main machine, to which I can send documents, which in turn will send them to the printer and print out. Sounds great doesn’t it?

Since Web API self host can be without any problems hosted as Windows Service, the whole application is really simple to put together. Let’s do that.

Use Web API as a dynamic runtime Typescript compiler

There’s been a lot of community buzz about Typescript recently – and rightfully so – as it solves a lot of Javascript’s issues, and despite being in its infancy, shows a tremendous amount of potential already.

One of the features (or side effects, depending on how you look at it) of Typescript, is that your are required to compile your code, to produce JS output. Sure, it is possible to dynamically compile Typescript in the browser (using its JS compiler), but that requires you to reference Typescript.js which is roughly 250 kB, and might be a tough pill to swallow.

However, using the ASP.NET Swiss army knife called Web API, and an approach we already disuussed on this blog before, let me show you how you can quite smoothly leverage on Web API pipeline to dynamically compile your Typescript code into Javascript at runtime.

More after the jump.

Scripting Web API self host with Roslyn CTP – Part 2

This is part two of the series - if you haven’t read part one check it out before proceeding.

Last time we did some cool stuff with using C# script files to instantiate a working Web API server - by executing them with RCSI and C# interactive window.

In part two, let’s use Roslyn to build our own scripting application (custom C# console), which will act as an input window for the user; user will be able type C# code that’s supposed to be executed and run it (something that services such as Compilify offer). We will expose Web API assemblies in that context, allowing the user to type in the code required for the Web API server to be run.

More after the jump.

Scripting Web API self host with Roslyn CTP – Part 1

If you follow me on Twitter you probably already know that recently I’ve been playing around with Roslyn. If you don’t know about Roslyn, in very short, you can think of it as “compiler as a service”.

There is a whole plethora of stuff that Roslyn allows us to do, one of the coolest being the scripting API - enabling us to use C# as a script language (think i.e. Perl or Python). So I had this idea, why not script a fully functional web server through Web API self host?

More after the jump.

Dealing with large files in ASP.NET Web API

There are a couple of things to consider when dealing with large file uploads and ASP.NET Web API. First of all, there are typical issues related to hosting within IIS context, and the limitations imposed on the service by default by the ASP.NET framework.

Secondly, there is the usual problem of having to deal with out of memory issues when allowing external parties or users to upload buffered large files. As a consequence, streamed uploads or downloads can greatly improve the scalability of the solutions by eliminating the need for large memory overheads on buffers.

Let’s have a look at how to tackle these issues.

About


Hi! I'm Filip W., a cloud architect from Zürich 🇨🇭. I like Toronto Maple Leafs 🇨🇦, Rancid and quantum computing. Oh, and I love the Lowlands 🏴󠁧󠁢󠁳󠁣󠁴󠁿.

You can find me on Github and on Mastodon.

My Introduction to Quantum Computing with Q# and QDK book
Microsoft MVP