Asp.Net Core Api Versioning

¡Hola a todos!

En esta nueva entrega de PlainTV, mi compañero Luis Ruiz Pavón me entrevista y hablamos sobre el versionado de Web Api en .NET Core. Gracias al paquete Microsoft.AspNetCore.Versioning, una tarea tediosa y compleja como era versionar nuestros controladores se vuelve sencilla y cómoda.

¡No te lo pierdas! Core with SignalR server and Autofac

SignalR server for core is currently in version 0.2.0. It has not been released yet to the official nuget repositories, so we must configure our project to use the core dev feed to obtain SignalR related packages.
Autofac for core still does not have the hubs registration extensions that we are used to when using full framework packages, but we will see how we can write them from scratch.

First of all we create a new core web project and we create a NuGet.config file so we can specify the core devevelopment NuGet feed so we can restore the websockets and SignalR packages.

The NuGet.config file should look like this:

Once created the nuget file, we should add the dependencies to project.json. The dependencies to use SignalR and autofac are shown below:


We are now set to start configuring our hubs!. As an example we are going to create a ChatHub that will receive a service to keep track of connections in it’s constructor.

Let’s create first the service interface and implementation to register it with AutoFac:




The HubTrack class is a POCO class with the following properties, just as an example:


Now we are ready to create our ChatHub injecting in the constructor the HubTrackingService to allow tracking clients with OnConnected Method:



Now, it is time to configure our Startup.cs to add SignalR Server and dependency injection.
In full framework, we use the RegisterHubs autofac extesion to register our hub classes:

but it is not available yet in Autofac for core so we should write a ContainerBuilder Extension to do this for us, the code is shown below:


Note: We use ExternallyOwned() to allow SignalR disposing the Hubs instead of being managed by Autofac.

Once our extension is added to the project we should set our Configure and ConfigureServices methods in Startup.cs



Notice the line:


In core we don’t have Assembly.GetExecutingAssembly() so we get the assembly from the Startup.cs type
How do we use the Hubs to notify clients when execution is inside a Mvc/Api Controller? Continue reading « Core with SignalR server and Autofac» core node services to execute your nodejs scripts

Nowadays, it is very common to have pieces of code developed in different languages and technologies, and today, more than ever in Javascript.

If you have a tested and battle proven javascript module, maybe you don’t want to rewrite it from scratch in C# to use it in, and you prefer to invoke it and fetch the results.

This post will show how to use Microsoft Asp Net Core Node Services in to execute our javascript commonjs modules.

In the following example, we will consume an online free json post service, that returns lorem ipsum posts information based on the request page id. To Achieve this we have a really simple nodejs module that will fetch the contents from the api using the library axios.

We are going to fetch this information through the Post Api Controller and return them in Json to the client.

Lets get started:


First of all we create a new core project:



Then we add the Reference to Microsoft.AspNetCore.NodeServices in  project.json file:




To finish the setup, we have to create a npm configuration file and add the axios reference that will be used by our node script:

To add the npm configuration file use Add > new Item > client-side > npm configuration file and then add the axios dependency as the following image:




With this the setup is complete, let’s jump to the api configuration, we want to add Node Services and fast configure the JsonSerializer indentation settings on our Startup.cs class in ConfigureServices method:


Once our api is configured to use Node Services we are going to create the Post Controller, and inject the node services in the constructor. The framework will automatically provide it to us so no further configuration is needed. We want to serialize the output to be an ExpandoObject so we can dynamically access to the properties in case it is needed. Right now we just want to send the output to the client in Json format.

The next step will be creating a folder called Node in our root folder and add the following nodejs script within a file named postClient.js.

This js module will just create a GET request to the api, requesting the post Id and invoking the callback based on the network request result:


NOTE: We should define a callback in the module parameters so Node Services can play the output results.
If we want the script to be successful we should pass a null error callback. On the other hand we will provide an error as the first callback parameter so the nodeService.InvokeAsync throws an Exception.


Final Steps, executing the controller action:

Everything is in place and we are going to use Fiddler to create requests to the post controller requesting different page ids:




Once we execute the request we can see debugging the resulting output from the js script:




And then we get the output on fiddler as well:




And that’s all for today, I hope you enjoyed.

Happy Coding!.