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”

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’”

C# Dirty Delegate Hack

Just a quick one for today. For a uni C# assignment I’ve had to implement a multi-delegate that processes a list of data, with the delegate consisting of three disparate functions which I also had to implement, according to a unit test spec. Data has to flow between each step of the delegate to produce the correct output. Now the usual way to do this would be to use pass-by-reference to alter the original data in-place, however in this circumstance it was not possible as the tests required the signatures of the delegate methods to pass-by-value. After thinking for some time, I came to the following solution. Beware, it’s fairly horrible.

In the classes containing the delegate methods, I define a helper function;

public static List<List<string>> S(List<List<string>> oldData, List<List<string>>newData) {
     for (var i = 0; i < oldData.Count; i++) oldData[i] = newData[i];
     return oldData;
}

Then in the delegate methods, I wrap the results expression in this helper function, passing the original list as the first parameter

public List<List<string>> StripWhiteSpace(List<List<string>> data)
     => S(data, data.Select(row => row.Select(val => val.Trim()).ToList()).ToList());

This then results in the new data being returned from the method (satisfies the tests) as well as the original data being replaced (allows the data to flow to the next delegate method). How does it work? It comes down to the fact that the data I’m working with here is non-primitive; passing it between methods is implicitly by reference rather than by value. It did take me a while to reach this conclusion, my initial thinking was that I’m working with lists of strings, and strings are primitive and therefor pass-by-value. When I realised I was actually working with Lists, which are non-primitive objects, it occurred to me that I could write a helper method to modify each member of the original List in-place. Because the new data is being placed back into the original List object, the same object of which is referenced later on, the data carries itself forward into those next methods of the delegate.

Open Live Writer, Blogger and Google Drive now working

docklands-laptop

Photo: Open Live Writer running on Windows 10, taken at Docklands Melbourne. This image was published to this Blogger blog with Open Live Writer.

Beginning from the next release of Open Live Writer (0.6.3), Blogger users will now be able to successfully post images to their blogs again. After being affected by the issue myself in my own usage of the software, I took it upon myself to develop and deliver a fix. Today, Open Live Writer will, instead of uploading to Google Photos using the now non-functional Picasa API, upload to Google Drive, publicly link-share the photo, and then embed the direct URL to the image within your post. This all happens automatically from within Open Live Writer, there are no work-arounds or hacks at play.

Continue reading “Open Live Writer, Blogger and Google Drive now working”