O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Building Microservices with .NET Core

Book Description

Architect your .NET applications by breaking them into really small pieces—microservices—using this practical, example-based guide

About This Book

  • Start your microservices journey and understand a broader perspective of microservices development
  • Build, deploy, and test microservices using ASP.Net MVC, Web API, and Microsoft Azure Cloud
  • Get started with reactive microservices and understand the fundamentals behind it

Who This Book Is For

This book is for .NET Core developers who want to learn and understand microservices architecture and implement it in their .NET Core applications. It’s ideal for developers who are completely new to microservices or have just a theoretical understanding of this architectural approach and want to gain a practical perspective in order to better manage application complexity.

What You Will Learn

  • Compare microservices with monolithic applications and SOA
  • Identify the appropriate service boundaries by mapping them to the relevant bounded contexts
  • Define the service interface and implement the APIs using ASP.NET Web API
  • Integrate the services via synchronous and asynchronous mechanisms
  • Implement microservices security using Azure Active Directory, OpenID Connect, and OAuth 2.0
  • Understand the operations and scaling of microservices in .NET Core
  • Understand the testing pyramid and implement consumer-driven contract using pact net core
  • Understand what the key features of reactive microservices are and implement them using reactive extension

In Detail

Microservices is an architectural style that promotes the development of complex applications as a suite of small services based on business capabilities. This book will help you identify the appropriate service boundaries within the business. We'll start by looking at what microservices are, and what the main characteristics are.

Moving forward, you will be introduced to real-life application scenarios, and after assessing the current issues, we will begin the journey of transforming this application by splitting it into a suite of microservices.

You will identify the service boundaries, split the application into multiple microservices, and define the service contracts. You will find out how to configure, deploy, and monitor microservices, and configure scaling to allow the application to quickly adapt to increased demand in the future.

With an introduction to the reactive microservices, you strategically gain further value to keep your code base simple, focusing on what is more important rather than the messy asynchronous calls.

Style and approach

This guide serves as a stepping stone that helps .NET Core developers in their microservices architecture. This book provides just enough theory to understand the concepts and apply the examples.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Errata
      3. Piracy
      4. Questions
  2. What Are Microservices?
    1. Origin of microservices
    2. Discussing microservices
    3. Monolithic architecture
      1. Service-oriented architecture
        1. What is service?
    4. Understanding the microservice architecture
      1. Messaging in microservices
        1. Synchronous messaging
        2. Asynchronous messaging
        3. Message formats
    5. Why should we use microservices?
    6. How does the microservice architecture work?
    7. Advantages of microservices
    8. SOA versus microservices
    9. Prerequisites of the microservice architecture
    10. Understanding problems with the monolithic architecture style
      1. Challenges in standardizing a .NET stack
        1. Fault tolerance
      2. Scaling
        1. Vertical scaling or scale up
        2. Horizontal scaling or scale out
        3. Deployment challenges
        4. Organizational alignment
        5. Modularity
        6. Big database
    11. Prerequisites for microservices
      1. Functional overview of the application
        1. Solutions for current challenges
        2. Handling deployment problems
        3. Making much better monolithic applications
          1. Introducing dependency injections
          2. Database refactoring
          3. Database sharding and partitioning
          4. DevOps culture
          5. Automation
          6. Testing
          7. Versioning
          8. Deployment
    12. Identifying decomposition candidates within monolithic
      1. Important microservices advantages
        1. Technology independence
        2. Interdependency removal
        3. Alignment with business goals
        4. Cost benefits
        5. Easy scalability
        6. Security
          1. Data management
          2. Integrating monolithic
    13. Summary
  3. Building Microservices
    1. Size of microservices
    2. What makes a good service?
    3. DDD and its importance for microservices
      1. Domain model design
        1. Importance for microservices
    4. The concept of Seam
      1. Module interdependency
      2. Technology
      3. Team structure
      4. Database
      5. Master data
      6. Transaction
    5. Communication between microservices
      1. Benefits of the API gateway for microservices
      2. API gateway versus API management
    6. Revisiting the case study--Flix One
      1. Prerequisites
      2. Transitioning to our product service
      3. Migrations
        1. Code migration
        2. Creating our project
        3. Adding the model
        4. Adding a repository
        5. Registering the repositories
        6. Adding a product controller
        7. The ProductService API
        8. Adding EF core support
        9. EF Core DbContext
        10. EF Core migrations
      4. Database migration
      5. Revisiting repositories and the controller
      6. Introducing ViewModel
      7. Revisiting the product controller
    7. Summary
  4. Integration Techniques
    1. Communication between services
      1. Styles of collaborations
    2. Integration patterns
      1. The API gateway
      2. The event-driven pattern
      3. Event sourcing
      4. Eventual consistency
      5. Compensating Transaction
      6. Competing Consumers
      7. Azure Service Bus queues
      8. Implementation of an Azure Service Bus queue
        1. Prerequisites
        2. Sending messages to the queue
        3. Receiving messages from the queue
    3. Summary
  5. Testing Strategies
    1. How to test microservices
      1. Handling challenges
    2. Testing strategies (testing approach)
    3. Testing pyramid
    4. Types of microservice tests
      1. Unit testing
      2. Component (service) testing
      3. Integration testing
      4. Contract testing
        1. Consumer-driven contracts
        2. How to implement a consumer-driven test
        3. How Pact-net-core helps us achieve our goal
      5. Performance testing
      6. End-to-end (UI/functional) testing
      7. Sociable versus isolated unit tests
      8. Stubs and mocks
    5. Tests in action
      1. Getting ready with the test project
      2. Unit tests
      3. Integration tests
    6. Summary
  6. Deployment
    1. Monolithic application deployment challenges
    2. Understanding the deployment terminology
    3. Prerequisites for successful microservice deployments
    4. Isolation requirements for microservice deployment
    5. Need for a new deployment paradigm
    6. Containers
      1. What are containers?
      2. Suitability of containers over virtual machines
      3. Transformation of the operation team's mindset
      4. Containers are new binaries
        1. It works on your machine? Let's ship your machine!
    7. Docker quick introduction
      1. Microservice deployment with Docker overview
      2. Microservice deployment example using Docker
        1. Setting up Docker on your machine
        2. Creating an ASP.NET web application
          1. Adding Docker Support
      3. Summary
  7. Security
    1. Security in monolithic applications
    2. Security in microservices
      1. Why traditional .NET auth mechanism won't work?
      2. JSON Web Tokens
      3. What is OAuth 2.0?
      4. What is OpenID Connect?
      5. Azure Active Directory
      6. Microservice Auth example with OpenID Connect, OAuth 2.0, and Azure AD
        1. Step 1 - Registration of TodoListService and TodoListWebApp with Azure AD tenant
        2. Step 2 - Generation of AppKey for TodoListWebApp
        3. Step 3 - Configuring Visual Studio solution projects
        4. Step 4 - Generate client certificates on IIS Express
        5. Step 5 - Run both the applications
      7. Azure API management as an API gateway
      8. Container security
      9. Other security best practices
    3. Summary
  8. Monitoring
    1. Instrumentation and telemetry
      1. Instrumentation
      2. Telemetry
    2. The need for monitoring
      1. Health monitoring
      2. Availability monitoring
      3. Performance monitoring
      4. Security monitoring
      5. SLA monitoring
      6. Auditing sensitive data and critical business transactions
      7. End user monitoring
      8. Troubleshooting system failures
    3. Monitoring challenges
      1. Monitoring strategies
    4. Logging
      1. Logging challenges
      2. Logging strategies
        1. Centralized logging
        2. Use of a correlation ID in logging
        3. Semantic logging
    5. Monitoring in Azure Cloud
      1. Microsoft Azure Diagnostics
      2. Storing diagnostic data using Azure storage
        1. Using Azure portal
        2. Specifying a storage account
        3. Azure storage schema for diagnostic data
      3. Introduction of Application Insights
    6. Other microservice monitoring solutions
      1. A brief overview of the ELK stack
        1. Elasticsearch
        2. Logstash
        3. Kibana
      2. Splunk
        1. Alerting
        2. Reporting
    7. Summary
  9. Scaling
    1. Scalability overview
    2. Scaling infrastructure
      1. Vertical scaling (scaling up)
      2. Horizontal scaling (scaling out)
    3. Microservices scalability
      1. Scale Cube model of scalability
        1. X-axis scaling
        2. Z-axis scaling
        3. Y-axis scaling
      2. Characteristics of a scalable microservice
    4. Scaling the infrastructure
      1. Scaling virtual machines using scale sets
      2. Auto Scaling
      3. Container scaling using Docker swarm
    5. Scaling service design
      1. Data persistence model design
      2. Caching mechanism
      3. Redundancy and fault tolerance
        1. Circuit breakers
        2. Service discovery
    6. Summary
  10. Reactive Microservices
    1. What are reactive microservices?
      1. Responsiveness
      2. Resilience
      3. Autonomous
      4. Being message-driven
    2. Making it reactive
    3. Event communication
      1. Security
        1. Message-level security
      2. Scalability
      3. Communication resilience
    4. Managing data
    5. The microservice ecosystem
    6. Reactive microservices - coding it down
      1. Creating the project
      2. Client - coding it down
    7. Summary
  11. Creating a Complete Microservice Solution
    1. Architectures before microservices
      1. The monolithic architecture
      2. Challenges in standardizing the .NET stack
      3. Scaling
      4. Service-oriented architecture
      5. Microservice-styled architecture
        1. Messaging in microservices
    2. Monolith transitioning
      1. Integration techniques
      2. Deployment
      3. Testing microservices
      4. Security
    3. Monitoring
      1. Monitoring challenges
        1. Scale
        2. Component lifespan
        3. Information visualization
    4. Monitoring strategies
      1. Scalability
        1. Infrastructure scaling
        2. Service design
    5. Reactive microservices
    6. Greenfield application
      1. Scoping our services
        1. The book-listing microservice
        2. The book-searching microservice
        3. The shopping cart microservice
        4. The order microservice
        5. User authentication
        6. Synchronous versus asynchronous
      2. The book catalog microservice
      3. The shopping cart microservice
      4. The order microservice
      5. The user auth microservice
    7. Summary