
About this Book
Microservices in .NET Core is a practical introduction to writing microservices in .NET using lightweight and easy-to-use technologies, like the awesome Nancy web framework and the powerful OWIN (Open Web Interface for .NET) middleware. I’ve tried to present the material in a way that will enable you to use what you learn right away. To that end, I’ve tried to tell you why I build things the way I do, as well as show you exactly how to build them.
The Nancy web framework, used throughout this book, was started by Andreas Håkansson, who still leads the project. Andreas was soon joined by Steven Robbins, and the two of them made Nancy great. Today Nancy is carried forward by Andreas, Steven, the Nancy Minions (Kristian Hellang, Jonathan Channon, Damian Hickey, Phillip Haydon, and myself), and the broader community. The full list of Nancy contributors can be found at http://nancyfx.org/contribs.html.
OWIN is an open standard for the interface between web servers and web applications. The work on OWIN was started in late 2010 by Ryan Riley, Benjamin van der Veen, Mauricio Scheffer, and Scott Koon. Since then, a broad community has contributed to the OWIN standard specification—through a Google group in the early days, and now through the OWIN GitHub repository (https://github.com/owin/owin)—and to implementing OWIN.
Microservices in .NET Core is a developers’ book first, but architects and others can benefit from it, too. I wrote it keeping in mind .NET developers who want to get started writing distributed server-side systems in general and microservices in particular, which means that the focus is on what a developer needs to know and do to write the code for a system of microservices. Working knowledge of C# and a bit of HTTP knowledge is assumed.
Microservices in .NET Core has 12 chapters spread across four parts:
Part 1 gives a quick introduction to microservices, answering what they are and why they’re interesting. This part also introduces Nancy and OWIN, the main technologies used throughout the book.
- Chapter 1 introduces microservices—what they are and why they matter. It introduces the six characteristics of microservices that I use to guide the design and implementation of microservices. At the end of the chapter, we say hello to Nancy and OWIN.
- Chapter 2 is a comprehensive example of coding a microservice using Nancy and OWIN, along with the Polly library and .NET Core. At the end of the chapter, we have a complete, albeit simple, microservice.
Part 2 covers how to split a system into microservices and how to implement functionality in a system of microservices.
- Chapter 3 covers how to identify microservices and decide what to put into each microservice. This chapter is about the design of a system of microservices as a whole.
- Chapter 4 shows how to design and implement the collaboration between microservices. This chapter discusses the different ways microservices can collaborate and shows how to implement those collaborations.
- Chapter 5 discusses where data should be stored in a system of microservices and how some of the data may be replicated across several microservices.
- Chapter 6 explains and demonstrates the implementation of some important techniques for making microservice systems robust.
- Chapter 7 takes a thorough look at testing a microservice system, including testing the complete system, testing each microservice, and testing the code inside the microservices.
Part 3 shows how to speed up development of new microservices by building a solid microservice platform tailored to the needs of your particular system. Such a platform provides implementations of a bunch of important concerns that cut across the entire system of microservices, such as logging, monitoring, and security. In this part you’ll build such a platform and see how it’s used to create new microservices quickly.
- Chapter 8 gives an in-depth introduction to OWIN, walks through building OWIN middleware, and shows how OWIN middleware is well suited for handling many crosscutting concerns.
- Chapter 9 explains the importance of monitoring and logging in a microservice system. Building on the OWIN knowledge from chapter 8, you’ll build OWIN middleware implementing monitoring support and middleware that aids good logging from your microservices.
- Chapter 10 discusses security in a microservice system. The highly distributed nature of a microservice system poses some security concerns that we discuss in this chapter. I’ll also walk you through using OWIN middleware to implement security features in your microservices.
- Chapter 11 builds on top of chapters 9 and 10 to create a microservice platform. The platform is built by taking the OWIN middleware from the previous chapters and packaging it in NuGet packages ready to be shared across microservices. The chapter includes an example of creating a new microservice using the platform.
Part 4 consists of chapter 12, which rounds off the book with some approaches to creating end-user applications for a microservices system. The chapter also shows how to build a small application on top of some of the microservices from earlier chapters.
Together, the 12 chapters will teach you how to design and code microservices using a lightweight, no-nonsense, .NET-based technology stack.
Most chapters in this book have sample code. All of this can be found in the download for this book on Manning’s site at https://www.manning.com/books/microservices-in-net-core, or in the Git repository on GitHub found at https://github.com/horsdal/microservices-in-dotnetcore.
The code is based on .NET Core, so to run it, you need to install .NET Core, the dotnet command-line tool, and a suitable IDE. You can find information on how to set these up in appendix A.
Throughout the book, I use a number of third-party open source libraries, particularly the Nancy web framework. .NET Core is a big shift from “traditional” .NET, so existing libraries need to be ported and thoroughly tested before they can claim full .NET Core support. At the time of writing .NET Core has just reached the 1.0.0 release, so not all libraries have been tested on .NET Core. For this reason, the book uses pre-release versions of libraries—Nancy, for instance, is used in a pre-release version of Nancy 2.0. If, when you read the book, there are stable releases for .NET core of the different libraries (for example, if the stable Nancy 2.0 is out), I recommend using those as you code along with the examples.
In the GitHub repository, at https://github.com/horsdal/microservices-in-dotnetcore, the master branch contains the code as it appears in the book. As stable releases of libraries for .NET Core come out, I plan to create a current branch and keep a copy of the code there that I will keep mostly up-to-date with the latest versions of libraries for a few years after publication of this book.
This book contains many examples of source code, both in numbered listings and inline with normal text. In both cases, source code is formatted in a fixed-width font like this to separate it from ordinary text. Sometimes code is also in bold to highlight code that has changed from previous steps in the chapter, such as when a new feature adds to an existing line of code.
In many cases, the original source code has been reformatted; I’ve added line breaks and reworked indentation to accommodate the available page space in the book. In rare cases, even this was not enough, and listings include line-continuation markers (). Additionally, comments in the source code have often been removed from the listings when the code is described in the text. Code annotations accompany many of the listings, highlighting important concepts.
Purchase of Microservices in .NET Core includes free access to a private web forum run by Manning Publications where you can make comments about the book, ask technical questions, and receive help from the author and from other users. To access the forum and subscribe to it, point your web browser to https://www.manning.com/books/microservices-in-net-core. This page provides information on how to get on the forum once you are registered, what kind of help is available, and the rules of conduct on the forum. It also provides links to the source code for the examples in the book, errata, and other downloads.
Manning’s commitment to our readers is to provide a venue where a meaningful dialog between individual readers and between readers and the authors can take place. It is not a commitment to any specific amount of participation on the part of the authors, whose contribution to the forum remains voluntary (and unpaid). We suggest you try asking the author challenging questions lest his interest strays!
The Author Online forum and the archives of previous discussions will be accessible from the publisher’s website as long as the book is in print.
Christian is an independent consultant with many years of experience building web and distributed systems on .NET as well as other platforms. He is part of the Nancy maintainer team and is a Microsoft MVP for .NET.