Dependency injection directly into actions in ASP.NET Web API
There is a ton of great material on the Internet about dependency injection in ASP.NET Web API. One thing that I have not seen anywhere though, is any information about how to inject dependencies into the action, instead of a controller (constructor injection).
Injecting your dependencies directly into an action, rather than in the controller is a very reasonable approach, as it helps you falling into an over-injecting trap, and perhaps resolving too much things, for no real reason.
With Web API, it’s actually extremely easy to do, so let’s go ahead and implement it.
More after the jump.
Building a strongly typed route provider for ASP.NET Web API
ASP.NET Web API 2.2 was released last week, and one of the key new features is the ability to extend and plug in your own custom logic into the attribute routing engine.
Commonly known as “attribute routing”, it’s actually officially called “direct routing”, because, as we are about to show here, it’s not necessary to use it with attributes at all, and you can plug in any route provider into it.
More after the jump.
Using Swift REPL from Sublime Text
Sublime Text is by far my favorite text editor. In its rich ecosystem of plugins you can find some absolute gems, and one of those is SublimeREPL which adds support for REPLs of various programming languages directly into your Sublime Text.
Swift is missing from there - but let’s add support for it!
Using Swift As General Purpose Scripting Language
One of the big advantages of Swift is that it gives you access to all Cocoa APIs and lets you use them in some very flexible ways.
One of those is the possibility to use Swift as a general OS “scripting” language - instead of bash, PyObjC or C or any other option that you might have opted for in the past. Moreover, you can do that entirely from outside of XCode - so write your Swift program in any editor and then simply use Terminal to execute it, as if it was pure script.
The obvious advantage of such approach is that you now have the same single language to handle iOS programming, OS X app programming and generic system/automation tasks that you might want to perform from the command line.
Let’s have a look.
POCO controllers in ASP.NET vNext
One of the very cool features of the vNext of ASP.NET MVC (a unified framework set to succeed MVC, Web API and Web Pages) is the ability use POCO classes as controllers. No base class, no interface to implement, 100% convention.
Let’s look a little bit into that.
Announcing ASP.NET Web API Recipes
It is my pleasure to announce that this summer my ASP.NET Web API book will be released. It’s entitled “ASP.NET Web API Recipes”, and will be published by Apress.
While the publication date is not set in stone yet (probably early August), you can already pre-order at:
The idea behind the book is quite simple - to discuss and dissect some of the most common problems and issues you might encounter in your work with Web API solutions.
There is going to be a total of 12 chapters with about 10 recipes per chapter (the number varies obviously). You will also get a full VS project with source code per each recipe.
Ignoring routes in ASP.NET Web API
If you use centralized routing, it is occasionally needed to ignore a greedy Web API route so that the request can be processed by some other component or handler.
One of the tiny overlooked features of Web API 2.1 was that it finally shipped with a cross-host way to ignore routes. It’s not too exciting, as it’s something that’s been in MVC for ages, but it’s nice to finally have an easy way to do it in Web API.
Opt in and opt out from ASP.NET Web API Help Page
The autogenerated ASP.NET Web API help page is an extremely useful tool for documenting your Web API. It can not only present information about the routes, but also show sample requests and responses in all of supported media type formats, and even display information for DataAnnotations.
However, more often than not, you don’t want all endpoints to be visible in the help page. Let’s have a look at how you can opt in and opt out from the ASP.NET Web API Help Page with your resources.
ASP.NET Web API exception logging with Raygun.io
Jon Galloway recently wrote a monster 4-part series covering the new features of MVC 5.1 and Web API 2.1 releases. One thing he mentioned was the new IExceptionLogger for Web API, and he called out the community to provide some example implementations of it.
Therefore, let’s have a look at how you’d approach that - with a sample of logging Exceptions to the excellent Raygun service from Mindscape.
Per request tracing in ASP.NET Web API
Web API allows you to plug in extensive logging mechanism through the ITraceWriter service. This will log all important events in the pipeline - such as selection of the controller, action, parameter binding and so on - all of which are extremely important in debugging all kinds of issues.
However, any existing instance of an ITraceWriter would log all of the information aside - into a log file, trace, database. But what if you wanted all the trace information produced while the given request was being processed, to be returned together with the server response?
Let’s have a look.
About
Hi! I'm Filip W., a software architect from Zürich 🇨🇭. I like Toronto Maple Leafs 🇨🇦, Rancid and quantum computing. Oh, and I love the Lowlands 🏴.
Recent Posts
- 2024/12/20, Running Phi Inference in .NET Applications with Strathweb Phi Engine
- 2024/12/16, Decorating a Quantum Christmas Tree with Q# and Qiskit
- 2024/12/12, Generating OpenQASM from Q# code
- 2024/11/22, Simplifying the AI workflow: Access different types of model deployments with Azure AI Inference
- 2024/11/15, Strathweb Phi Engine - now with Safe Tensors support
Categories
- ai (17)
- ai search (5)
- apache cordova (1)
- asp.net 5 (17)
- asp.net core (47)
- asp.net mvc (35)
- asp.net mvc 6 (7)
- asp.net vnext (6)
- asp.net web api (96)
- astronomy (1)
- autogen (1)
- azure (22)
- azure service bus (1)
- azure-devops (1)
- benchmark dotnet (1)
- bing maps (1)
- blazor (2)
- c plus (2)
- c-sharp (155)
- cryptography (5)
- csharp (6)
- csharp 10 (2)
- dnx (3)
- dotnet-cli (2)
- dotnet-script (11)
- duende (4)
- editorconfig (1)
- entity framework (2)
- espn api (2)
- events (1)
- ffi (4)
- fsharp (1)
- git (1)
- glimpse (1)
- html5 (4)
- identity server (2)
- iis (2)
- il (1)
- intro to qc (19)
- ios (5)
- javascript (9)
- jquery (4)
- jquery mobile metro (1)
- katana (2)
- kindle (1)
- knockout.js (8)
- kotlin (2)
- last.fm api (2)
- linq (1)
- mac (2)
- macos (1)
- mathematica (1)
- msbuild (3)
- mvc core (3)
- nancy (2)
- native (1)
- net (144)
- net 5 (3)
- net 6 (5)
- net 7 (7)
- net 8 (3)
- net 9 (1)
- net core (49)
- net sdk (2)
- ninject (2)
- odata (4)
- oidc (2)
- omnisharp (13)
- openai (11)
- osx (2)
- owin (5)
- phi (5)
- php (1)
- python (1)
- q-sharp (36)
- qir (3)
- qiskit (1)
- quantum computing (40)
- roslyn (30)
- rust (5)
- scriptcs (11)
- scripting (9)
- security (8)
- servicestack (2)
- signalr (8)
- swift (8)
- testing (5)
- twitter boostrap (1)
- typescript (1)
- visual studio (4)
- visual studio code (11)
- wasi (3)
- wasm (3)
- windows phone 7 (1)
- wordpress (1)
- wpf (2)