Today, let’s have a look at dealing with disposable objects in ASP.NET Core. Imagine you have an object that you’d like to reuse throughout the duration of the HTTP request, but at the end, it must clean up some of its open resources or handles. This is a common requirement for i.e. file writers, DB connections, other unmanaged resources or external processes – to mention just a few.
There are several reasons why it makes sense to build super-lightweight HTTP services (or, despite all the baggage the word brings, “microservices”). I do not need to go into all the operational or architectural benefits of such approach to system development, as it has been discussed a lot elsewhere.
It feels natural that when building such HTTP services, it definitely makes sense to keep the footprint of the technology you chose as small as possible, not to mention the size of the codebase you should maintain long term.
In this point I wanted to show a couple of techniques for building very lightweight HTTP services on top ASP.NET Core, without the use of any framework, and with minimal code bloat.
When developing web applications with ASP.NET, it is common to end up in situations where you require access to HttpContext. This wouldn’t be anything special, but outside of the context of framework level APIs such as controllers, middleware and so on (which would always give you a way to fetch the current HttpContext), it can be tricky.
While generally speaking, HttpContext could be passed around as a regular dependency to the logical components that require it, that solution is often impractical.
Let’s have a look at how you can get a hold of HttpContext in ASP.NET Core.
Using strongly typed configuration is without a question a great convenience and productivity boost for the developers; but what I wanted to show you today is how to bind IConfiguration directly to your POCO object – so that you can inject it directly into the dependent classes without wrapping into IOptions
Today let’s have a look at two extensibility points in ASP.NET Core MVC – IActionConstraint and IParameterModelConvention. We’ll see how we can utilize them to solve a problem, that is not handled out of the box by the framework – creating an MVC action that has mandatory query string parameters.
Let’s have a look.
One of the late additions before the RTM release of ASP.NET Core MVC was the introduction of the [Controller] attribute, and its counterpart, [NonController], which were added in RC2.
Together, they allow you to more specifically control which classes should be considered by the framework to be controllers (or controller candidates) and which shouldn’t. They also help you avoid the nasty hacks we needed to do in i.e. ASP.NET Web API to opt out from the “Controller” suffix in the name.
Let’s have a look.
In this blog post let’s have a look at building a lightweight site in ASP.NET Core.
In “classic” ASP.NET we had the WebPages framework – which allowed us to build sites composed only of views. This was perfect for lightweight projects, where we didn’t need the entire model-controller infrastructure.
At the moment, ASP.NET Core doesn’t have an equivalent yet (though it’s being worked on), but we have already provided a similar type of experience via the WebApiContrib project (you can read more about the project here). With the help of some of the libraries from there, we can build controller-less sites for ASP.NET Core already.
In addition to that, we can combine it with using Markdown tag helpers for content delivery – and it will result in a very cool experience – being able to author ASP.NET Core sites, without controllers, in Markdown. With Razor sprinkled on top of it, to provide dynamic data.
Let’s have a look – more after the jump.
In the past, a bunch of us from the ASP.NET Web API community worked together on a WebApiContrib project (or really, projects, cause there were many of them!).
The idea was to provide an easy to use platform, a one stop place for community contributions for ASP.NET Web API – both larger add ons, such as HTML/Razor support for Web API, as well as smaller things like i.e. reusable filters or even helper methods. This worked extremely well – WebApiContrib packages were downloaded over 500k times on Nuget, and a nice community has emerged around the project on Github.
Recently, we decided to restart the project, this time focusing on ASP.NET Core. Since the “brand” has caught on in the community and is fairly recognizable, we just called it WebApiContrib.Core.
When you are building HTTP APIs with ASP.NET Core MVC, the framework allows you to use FormatFilter to let the calling client override any content negotiation that might have happened on the server side.
This way, the client can – for example – force the return data to be JSON or CSV or any other format suitable (as long as the server supports it, of course) for his consumption.
The built-in mechanism (out of the box version of FormatFilter) is a little limited, so let’s have a look at how you can extend and customize its behavior.
A couple of months ago I blogged about adding a feature to ASP.NET Core MVC (or ASP.NET 5 at the time) that will allow you to set central route prefix(es) to your attribute routing mechanism.
That solution was written against beta8 version of ASP.NET Core and since now we are at RC2 – it doesn’t (surprise, surprise) work anymore.
Here is the updated version.