Add automatic OpenAPI client code generation to .NET 6 apps using dotnet-openapi, NSwag and service references

OpenAPI defines a way for web services to clearly define their API for automatic and correct client library generation, and with NSwag, clients for these APIs can be automatically generated for C#. What’s more, rather than generating the source code for these clients manually, a service reference can be added to your .csproj file to generate these clients transparently and automatically at development-time and build-time, —essentially It Just Works! This is truly an amazing (although not specifically unique,) benefit of the .NET tooling ecosystem; any IDE which plugs into the common .NET C# language backend (VS, Code, Rider, you name it) will immediately see and present the generated API client classes and their methods in type suggestions, without anything files having to be compiled or included manually by the developer.

Today I’ll write on how to go about this via a dotnet command-line tool; applicable regardless of the IDE or development platform you use. Whilst Visual Studio users get a simple (and obvious) wizard for adding these API service references, the information regarding the platform-agnostic command-line version seems to be scattered across the ASP.NET Core docs, and personally I felt it was quite obscure and challenging to find. Hopefully this blog post speeds up this process for any other developers going through these same steps for the first time.

Continue reading “Add automatic OpenAPI client code generation to .NET 6 apps using dotnet-openapi, NSwag and service references”

DevOps-to-Things with macOS Shortcuts

If you’re a Things user like me, and have to deal with Azure DevOps for working on software projects, I wouldn’t doubt that you’ve thought that there must be some way the two can be linked–some way to get the work items you’re focusing on from DevOps into Things. Up until now, I’ve been adding my important work items to my Things inbox manually, either by copy and pasting their titles, or re-typing their titles in their entirety–both methods of which are frustrating and time consuming. In my opinion, entering items into your task manager shouldn’t force you into a mental ‘context switch’, and switching into Things to paste/type in long-winded work items from DevOps typically leads to this happening, at least for myself.

Trying to solve this, I decided to automate the process with the new Shortcuts app in macOS Monterey. It’s my first ‘big’ shortcut, and whilst I am a software developer and could’ve very well written a fully-fledged script in a language of my choice for this, I settled on Shortcuts for this for two main reasons; it easily integrates with the applications I needed it to integrate with, and it’s easy to share the automations I create with others. The Shortcuts app is the future of automation in the Apple ecosystem, so I very much enjoyed this opportunity to properly dive into it.

Continue reading “DevOps-to-Things with macOS Shortcuts”

Insider Dev Tour ‘review’

On Friday I attended Microsoft’s ‘Insider Dev Tour’ in Melbourne, one of about 44 similar events being held around the world throughout the month of June. Microsoft advertised the event as being ‘for developers interested in building Microsoft 365 experiences (…) today, using the latest dev technologies, as well as for those who want a peek into the future,’ and it was completely free to attend. Hosted at the offices of Xello, a Melbourne-based IT consultancy company, the event was all day, running from the hours of 8 to 5, and had food and coffee provided.

I was fairly excited when I heard about the event, having being recently drawn in to the Windows desktop development ecosystem through my involvement in the Open Live Writer project. I wasn’t going in with any particular agenda on things I would’ve liked to learn, but rather I was just curious as to how the whole day would play out and if I’d pick up any nifty skills. I’ve never been to any kind of developer conference before, so really this would’ve been a first for me.

Continue reading “Insider Dev Tour ‘review’”

Working with composite primary keys and Entity Framework Core scaffolded controllers

A recent uni assignment has required me to implement an ASP.NET Core REST API using EntityFrameworkCore on a pre-existing database. Whilst EF Core tends to generate controllers for models with single primary keys just fine, I’ve found that it has no support for composite primary keys. This requires you to alter your controller classes to add the support manually, which in my case involved the following;

  • Alter the paths of the individual Get, Put, and Delete endpoints to contain two (or more, depending on how many keys are in your composite) parameters, and then alter the method parameters to match. This requires careful consideration as there are many different ways you can structure the parameters in your route.
  • Alter every usage of _context.nameOfModelHere.FindAsync to contain all components of your primary key. Look at the model’s Entity definition in your database context class, specifically the usage of entity.HasKey, to determine the order in which to list the components in your key to FindAsync.
  • Alter the nameOfModelHereExists method to take the correct amount of parameters for your key. Adjust the body of the method accordingly to check all parts of the key; I just added && at the end of the existing equality expression, and added more equality expressions.
    • Alter all usages of nameOfModelHereExists appropriately.
  • In the Put method, alter the second if statement to check all the components of your primary key, rather than just id.
  • In the Post method, adjust the CreatedAtAction call in the return statement to contain all the components of your primary key.
  • I would also recommend updating all the auto-generated comments that EntityFramework put in to keep things consistent.
Continue reading “Working with composite primary keys and Entity Framework Core scaffolded controllers”