Using NSwag with Cake on .NET Core


Some of you may know that I published Cake.NSwag, a powerful Cake addin for working with Swagger files using the NSwag toolchain.

This addin directly interfaced with the NSwag library to do the parsing and generation work, which is how it avoided any dependencies and made it very simple to use. However, this approach had it's drawbacks: loading the NSwag libraries during execution worked well running on the .NET Framework but led to a lot of problems trying to run on .NET Core.

Since Rico added support for .NET Core to NSwag itself, it seemed a waste to keep Cake users from using it, so I have just published a new library: Cake.NSwag.Console.

This package is a wrapper around the NSwag CLI rather than the library itself, meaning it can be used on .NET Framework (Cake/Cake.exe), or on .NET Core (Cake.CoreCLR/Cake.dll). This does mean that you need to ensure that the NSwag CLI is available during your Cake build, but opens up to running on any platform where .NET Core is supported!

Switching Over

Fortunately (and deliberately), Cake.NSwag.Console has 100% API Compatibility with Cake.NSwag. That means that if you're currently using Cake.NSwag and want to start using Cake.NSwag.Console on .NET Core, you just need to change your scripts #addin directive and that's it!. You don't need to change your script and you should see largely the same behaviour as with the "full" version of the addin.

// just change
#addin nuget:?package=Cake.NSwag
// to:
#addin nuget:?package=Cake.NSwag.Console
// rest of your script is exactly the same

Note this means you can't include both addins at the same time: your script will fail to compile ☹


I'm planning on maintaining both Cake.NSwag and Cake.NSwag.Console to have the same API so you can use either as a drop-in replacement, depending on the environment.

You can find the source for Cake.NSwag.Console over on GitHub, and the package is now on NuGet.

If you have any questions or issues, just raise an issue on GitHub and I'll try and sort you out!


comments powered by Disqus