Skip to main content

Back to the liblab blog

Improve your ASP.NET Core API inner development loop by integrating liblab into MSBuild

| 4 min

As an ASP.NET developer building an API, you probably have a workflow where you are using a tool like NSwag to generate an OpenAPI spec for you on every build. This can be integrated into your MSBuild targets so that after every build, your spec is updated.

For example, in your .csproj file, you might have the following NSwag target that runs after a build to generate the OpenAPI spec using a nswag.json file for configuration:

<Target Name="NSwag" AfterTargets="Build">
<Exec WorkingDirectory="$(ProjectDir)"
EnvironmentVariables="ASPNETCORE_ENVIRONMENT=Development"
Command="$(NSwagExe_Net80) run nswag.json /variables:Configuration=$(Configuration)" />
</Target>

The flow would be this:

This is a great flow to always get an updated OpenAPI spec with every build.

But what if you wanted to take this further, and generate an SDK with every build? Maybe you are experimenting with an API and want to see not only how it works as an API, but what the improved user experience is when using an SDK. One way to do this is to integrate liblab into your MSBuild targets.

How to integrate liblab into MSBuild

MSBuild allows you to define targets, and in these targets execute commands. In the example for NSwag above, this is exactly what is happening - after the Build target is complete, an NSwag target is executed to create the OpenAPI spec. This is executed as a dotnet tool - the $(NSwagExe_Net80) property expands to the command dotnet "~/.nuget/packages/nswag.msbuild/14.0.7/buildTransitive/../tools/Net80/dotnet-nswag.dll".

In the same way, we can add a MSBuild target to execute the liblab CLI. To do this you need to:

  1. Ensure you have the liblab CLI installed, and you are logged in

  2. Create a liblab config file for your generated OpenAPI spec. Make sure this is set to create a csharp SDK in the languages options, and the specFilePath is pointing to the generated OpenAPI spec.

  3. Add the following target to your .csproj file:

    <Target Name="liblab" AfterTargets="NSwag">
    <Exec WorkingDirectory="$(ProjectDir).." Command="liblab build --yes" />
    </Target>

    Change the value of WorkingDirectory to be where you want to run from, this should be where your liblab config file lives.

  4. Build your project using dotnet build. After NSwag has generated the OpenAPI spec, liblab will run and generate the SDK.

    Terminal
    vscode ➜ /workspaces/nswag-liblab-demo/src (main) $ dotnet build
    MSBuild version 17.9.8+b34f75857 for .NET
    Determining projects to restore...
    All projects are up-to-date for restore.
    NSwagSample -> /workspaces/nswag-liblab-demo/src/bin/Debug/net8.0/NSwagSample.dll
    NSwag command line tool for .NET Core Net80, toolchain v14.0.7.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))
    Visit http://NSwag.org for more information.
    NSwag bin directory: /home/vscode/.nuget/packages/nswag.msbuild/14.0.7/tools/Net80

    Executing file 'nswag.json' with variables 'Configuration=Debug'...
    Launcher directory: /home/vscode/.nuget/packages/nswag.msbuild/14.0.7/tools/Net80
    Done.

    Duration: 00:00:00.8058853

    Your SDK's are being generated.

    - Building C#
    - C# built
    Successfully generated SDKs downloaded. You can find them inside the /workspaces/nswag-liblab-demo/output folder
    Successfully generated SDK's for C# ♡

This gives the following flow:

Check out our sample

To show this off, we have created a sample project on GitHub that uses this flow. It is based on the NSwag .NET 8.0 API sample, with the added liblab target.

As always, give this demo a try, and if you have any questions join the liblab Discord server and ask away!

C#

ASP.NET