.NET Annotated Monthly | April 2023
- What’s New: APIs in Microsoft Graph
- Announcing .NET 8 Preview 2
- ASP.NET Core updates in .NET 8 Preview 2
- .NET March 2023 Updates – .NET 7.0.4, .NET 6.0.15
- What’s new for the WinForms Visual Basic Application Framework
- Introducing the Reliable Web App Pattern for .NET
- Announcing: Azure Developers – .NET Day
- Announcing TypeScript 5.0
We’d like to thank Poornima Nayar for curating this month’s featured content! Poornima is a .NET developer with over 10 years of experience in .Net and Umbraco. She is a Microsoft MVP for Developer Technologies and an Umbraco MVP. Poornima is passionate about technology and enjoys sharing her knowledge with the wider tech communities. Outside work, Poornima is a mother and spends a lot of time with her daughter. Poornima is also a student of Carnatic Music, a stream of Indian Classical Music. Connect with Poornima on Twitter.
Thank you JetBrains for having me as an author, to write about one of my favourite topics – APIs! APIs or Application Programming Interfaces are everywhere! Talk about shopping online, booking a cab or getting a takeaway for your dinner on a Friday night – APIs are everywhere. They are software intermediaries that help establish communication between applications. When I think about building APIs in .NET, the terms that come to my mind are REST, GraphQL and gRPC. As of today, if you think about building APIs in .NET these are the options available to us.
gRPC is a flavour of Remote Procedure Framework(RPC). We had WCF in the past, with .NET Framework for RPC requirements. gRPC started having out-of-the-box support in the modern .NET world from .NET 3.0 onwards. Microsoft recommends gRPC for your RPC requirements. gRPC is an open-source framework and is language-neutral, platform-neutral and extensible. And this is made possible using the magic that is Protocol Buffers. With gRPC, Protocol Buffers are used as the interface definition language as well as the message exchange format.
The contract of your API (service and procedures) is defined in a
.proto file. The tooling available creates C# code for each proto file whereby each service is generated as an abstract class and the procedures in a service generated as virtual methods in the abstract class. You can give the various procedures your own implementation(by default, the generated virtual methods cannot be used until you give them an implementation) by inheriting from the abstract class and overriding the methods.
gRPC is designed for HTTP/2 and the .NET implementation of gRPC was the first of its kind to come up with suggestions on how gRPC should work over HTTP/3. There is superior tooling available in the .NET world to help you develop gRPC APIs and you can make use of the out-of-the-box dotnet templates to get yourself started with gRPC. With .NET 7, a feature called gRPC JSON Transcoding was added to gRPC whereby you can extend your existing gRPC API to an HTTP API as well, without code duplication! gRPC is a perfect candidate for internal services, as microservices, for IOT devices and polyglot environments.
gRPC services written in one language can be consumed by clients written in many other languages. To consume a gRPC service you need a gRPC Client. But fear not, the tooling has got it all covered for you. The
.proto file together with the tooling can generate the client and stub methods for a wide variety of programming languages. Sharing the
.proto file, or rather, making it available to the consumers to generate the client and stub methods is of prime importance here.
Representational State Transfer (REST) is undoubtedly the most widely adopted and the most popular and successful API pattern out there. It was first mentioned in 2000 by Roy Fielding in his famous dissertation. He suggested it as an architectural pattern for distributed hypermedia systems like the Web. It is a set of guiding principles, known as the famous Fielding constraints. Any system that confirms these constraints can be termed as a RESTful system. It is not limited to just API at all. In fact, REST was proposal to standardise a hypermedia system like the web. We are bringing it to an API level. It is not a standard by any means, how you implement these constraints is totally up to you!
With REST, everything is based on resources – collection of resources, modelling a resource and acting upon them. A resource can be anything that can have a computer readable format – a person, a vehicle, a fruit, anything! Resources have a unique identifier – a url in this instance. When you ask for information about a resource, the server gives you a representation of the resource that you just asked. This helps the client change the application state, stored on the client.
Similarly, to change a resource, the client can provide an updated representation of the resource that might lead to a change in the resource state on the server. This manipulation of application state and resource state using resource representations is the essence of REST and where it gets its name from. There is of course, out-of-the-box support for building REST APIs in the .NET world!
REST reuses the rich toolkit HTTP provides us with – HTTP Verbs, Status Codes, Headers, Requests, Responses, Caching. With a REST API a typical endpoint serves a set of resources for e.g. users. Getting a list of resources, a particular resource, creating, updating and deleting resources are made possible by issuing the appropriate HTTP Verb and headers to the endpoint.
Writing about REST cannot be complete without mentioning HATEOAS (Hypermedia as the Engine of Application State). HATEOAS is what completes REST. It is the factor that decouples your clients from the server and is the server’s answer to the question “What is the next step I should take?” asked by the client. Application state is stored on the client. Instead of relying upon hardcoded next course of action to change the application state on the client, it relies upon hypermedia included in the server response. Hypermedia drives the change in application state. This is the concept of HATEOAS.
Typically, every GET response from the server includes a set of hypermedia links describing the actions that can be performed on the resource as well as how to gather information about related resources. Without HATEOAS implemented, what you would be having is a HTTP API. Read more about Richardson Maturity Model to understand more about this concept!
REST is the most versatile API when it comes to client usage. Any app that can issue an HTTP request can consume a REST API. It excels as microservices and management (CRUD) APIs.
GraphQL is an API pattern which is very very unique. It is a query language for your API, supported by a server-side runtime that can parse, validate and execute these queries. GraphQL acts like an application layer. Your data could be in a database, coming from a REST API or even another GraphQL endpoint. It is essentially an interface between your consuming app and back-end hence GraphQL APIs can be termed GraphQL servers. GraphQL helps you visualise your data as a graph and makes it possible to extract parts of that graph, hence the name GraphQL.
GraphQL is renowned as a client-focussed API. With gRPC and REST you have fixed requests and fixed responses. However, with GraphQL, you get a single endpoint that can take in any valid GraphQL query. The consuming app can issue any query depending upon their needs and the server replies back with a response that mirrors the query. The response structure is therefore predictable to the consuming developer. The response returned only contains what the client asked for – nothing more, nothing less.
Furthermore, the queries can be really flexible. The consuming app can query users and their friend details using a single query. In a typical REST world this would be at least 3 different API calls. In fact the very existence of GraphQL was to overcome this challenge REST posed. GraphQL is a specification. It is a blueprint that various implementations of this blueprint tries to confirm. There are many implementations of GraphQL across various different programming languages.
In the .NET world, there are 2 implementations of GraphQL – GraphQL.NET and HotChocolate. Both these implementations are open source. GraphQL, as a specification, is transport agnostic. Serving GraphQL over HTTP is the most popular choice.
GraphQL supports 3 operations – query, mutation and subscription. Query is the root operation type for reading information. A mutation can be issued to mutate/change data. This covers inserts, updates and deletes. Subscription stands for real-time, pushed updates from the server. Clients can subscribe to real-time information from the server. All the 3 operations are performed over a HTTP POST.
GraphQL is an experience-driven API. It is an ideal candidate for mobile apps where APIs need to cater in for flexibility. GraphQL is also useful in scenarios where you need data to be fetched from multiple sources. GraphQL can act like a gateway in such an instance.
If you wish to learn more on how these API patterns compare, watch my session REST, GraphQL and gRPC : A Comparison on the JetBrains YouTube Channel.
You are reading this newsletter thanks to an API! APIs are everywhere :-)
Programing tutorials and tips
.NET tutorials and tips
- Minimal APIs and HATEOAS – Poornima, our guest author, forgot to mention this excellent blog post she wrote on minimal APIs and HATEOAS (Hypermedia As The Engine Of Application State). See what it is and how you could use it.
- Control access with an API Gateway in .NET – If you’re working with microservices, you may need an API Gateway to manage your services, and in this video Layla Porter explains how to use them in .NET.
- Getting started with Image Caching in .NET MAUI – Caching is an important part of managing resources on the client just like it is on the server side. Let Leomaris Reyes show you how to do it.
- Create and Validate a Sign-Up Form in .NET MAUI – The classic sign-up form, MAUI style, with controls by Syncfusion for form management. Jeyasri Murugan walks us step by step through the form and data validation.
- What are the advantages of .NET in an isolated worker model? – In this quick video, Melony Qin discusses how the isolated worker model helps you avoid conflicts, gives you full control over startup, and more.
- How to Split Text to Columns in Excel XLSX using C# and VB.NET – So many businesses are run by Excel. So bookmark this one so that you have a reference when you need to programmatically access it to automate common tasks like text manipulation. Post by Mackenzie Albitz.
- Working with Git in JetBrains Rider and Understanding the .NET ecosystem: The introduction of .NET Standard – Check out these two awesome articles by Andrew Lock. First up: Git in Rider! And next is understanding .NET Standard, a source of confusion since its inception.
- .NET 7 SDK built-in container improvements – Helpful for everyone working with Docker and containers. .NET 7 has some nice improvements, as detailed by Laurent Kempé.
- Solving .NET JSON Deserialization Issues – Check out all the issues Khalid Abuhakmeh has. This time, JSON deserialization issues.
- Many Ways to make and Deserialize an HTTP GET with HttpClient – More on deserialization with this post by Bryan Hogan showing a lot of cool ways to work with information in an `HTTP GET` request.
- Writing a .NET profiler in C# — Part 3 – Buckle-up for some great in-depth information about source generators, native code wrapping, and performance that you will learn by writing an app profiler. Or instead of writing your own, read this series by Kevin Gosse. (Here are Part 1 and Part 2).
- How I keep my test names short and functional – It’s common to find really long test names in code, since many developers name to match the name with exactly what’s going on in the test. But Dennis Doomen shows us that we don’t need to make a novella out of a test name while it’s still functional and descriptive.
- Comparing FirstOrDefault and SingleOrDefault – Have you ever got confused over these two methods? Matthias Jost clears them up for us.
- Wrapping Compression Streams in Blazor – Kristoffer Strube published this detailed post demonstrating how to wrap compression streams in Blazor.
- How Async/Await Really Works in C# – Stephen Toub throws down the gauntlet with this one. Do you know how async/await really works?
- How to Unit Test ILogger in ASP.NET Core – Nice post showing how to test ILogger in ASP.NET Core by Code Maze.
- From Ancient Greeks to Modern Geeks: Basic Machine Learning Algorithms in C# – Here’s a great primer on Machine Learning in .NET by Simon Painter. If you were considering learning about ML, now’s the time.
- How to get the client IP in ASP.NET Core even behind a proxy – This is a good one! Often logging requires an IP address, but if you’re behind a proxy then you need to ensure it’s correct. Learn how to write the code to capture the right IP address with this post by Thomas Ardal.
- Successful and Failed Attempt: My First Pull Request for ASP.NET Core – Follow the First Pull Request Saga by Lex Li and see what goes on with ASP.NET Core on Windows 11 ARM64.
- Converting string to enum at the cost of 50 GB: let’s analyze the CVE-2020-36620 vulnerability – What’s this? A simple NuGet package that does only a string conversion can be a DDOS vulnerability? Who knew? Sergey Vasiliev did and he tells us all about it, as well as other important facts about securing apps in this excellent in-depth post.
- A first look at Blazor and .NET 8 – New stuff! These posts are always fun. The `<QuickGrid>` component looks quite useful. Thanks Damien Bod for passing this along.
- C# 11.0 new features: list pattern matching – More new stuff! Ian Griffiths demonstrates list pattern matching in C# 11. Sweet!
- Write your own AutoMapper in C# – Get the popcorn! Steven Giesel exposes a few things that mappers do that could be better, along some of his personal preferences around using them. What do you think of Steven’s approach?
- ASP.NET Core7: Use endpoint groups to manage minimal APIs versioning – Don’t miss this super handy post by Anthony Giretti for managing endpoints. API versioning really trips up a lot of developers – learn to manage it well.
- C# Performance tips and tricks – Perf tricks are always good to have. Here are a few by John-Daniel Trask.
- Use XML Literals in Visual Basic .NET to generate TwiML – VB baby!!! And one of its best features – XML Literals! Niels Swimberghe generates TwiML (Twilio’s language for using their services) in an ASP.NET VB app.
- Embed Blazor Components in Any Webpage with .NET 7’s Official Custom Elements Support – Another technique that is great to know. If you’ve spent that time on a Blazor component, it would be awesome to use it in any webpage, wouldn’t it? Thank Jon Hilton for this one.
Here’s a nice reference graphic to print and hang on your desk.
Related programming tutorials and tips:
- A Beginner’s Guide to Contributing to Open-Source Projects: Navigating Git and GitHub – Are you wondering what it’s like to step into the world of open source? Let Bitian Zhang guide you through OSS via Git/GitHub – the tool everyone uses for open source work.
- How GraphQL fits into the API lifecycle – We often don’t think of GraphQL as something that we need to fit into a lifecycle, but indeed, we do have to do that. Christina Hastenrath outlines the steps.
- “I NEED data from another service!”… Do you really? – Well, do you? (Probably not but read on and see) Derek Comartin answers this oft asked question.
- Performance stability of GitHub Actions – JetBrains developer Andrey Akinshin does his thing analyzing code – this time, it’s GitHub actions and their performance stability.
Interesting and cool stuff
We all know a programmer like this. 🙈🙉🙊
And finally, the latest from JetBrains
⚒️ Check out our .NET Guide! Videos, tips, and tricks on .NET related topics. ⚒️
- ReSharper 2023.1 was just released! 🎉
- Rider 2023.1 is out! 🎉
- ReSharper C++ 2023.1 is out! 🎉
- ReSharper and Rider 2022.3.3 Bug Fixes Have Landed
- Unity DOTS support in Rider 2023.1
Blog posts, webinars, etc..
- Remote Development with JetBrains Rider
- ReSharper IL Viewer and Low-Level C#
- Connecting to a Running Docker Container Shell
- Why is ReSharper suggesting this?
- Static Interface Members, Generic Attributes, Auto-Default Structs – Using C# 11 in Rider and ReSharper
- Webinar – Object-Oriented vs. Functional Programming With C# and F#
Sharing is caring! So share content that you find useful with other readers. Don’t keep it to yourself! Send us an email with your suggestions for publication in future newsletters!
Subscribe to Blog updates
How To Use AI Assistant to Generate Test Data For .NET Applications
How To Use AI Assistant to Generate Test Data For .NET Applications for better unit tests.
Rider 2024.1 EAP 5: Collections Visualizer, Sticky Lines, Improved GitHub Actions Support, and More
We are mid-way through the Early Access Program for Rider 2024, which means the EAP 5 build has just been released, bringing a host of amazing updates. But before you go to download it, let’s go over the new features this preview contains. Collections visualizer Rider 2024.1 introduces t…