0%

Book Description

Get up to speed with the latest features of C# 8, ASP.NET Core 3 and .NET Core 3.1 LTS to create robust and maintainable web services

Key Features

  • Apply design patterns and techniques to achieve a reactive, scalable web service
  • Document your web services using the OpenAPI standard and test them using Postman
  • Explore mechanisms to implement a secure web service using client-side SSL and token authentication

Book Description

In recent times, web services have evolved to play a prominent role in web development. Applications are now designed to be compatible with any device and platform, and web services help us keep their logic and UI separate. Given its simplicity and effectiveness in creating web services, the RESTful approach has gained popularity, and this book will help you build RESTful web services using ASP.NET Core.

This REST book begins by introducing you to the basics of the REST philosophy, where you'll study the different stages of designing and implementing enterprise-grade RESTful web services. You'll also gain a thorough understanding of ASP.NET Core's middleware approach and learn how to customize it. The book will later guide you through improving API resilience, securing your service, and applying different design patterns and techniques to achieve a scalable web service. In addition to this, you'll learn advanced techniques for caching, monitoring, and logging, along with implementing unit and integration testing strategies. In later chapters, you will deploy your REST web services on Azure and document APIs using Swagger and external tools such as Postman.

By the end of this book, you will have learned how to design RESTful web services confidently using ASP.NET Core with a focus on code testability and maintainability.

What you will learn

  • Gain a comprehensive working knowledge of ASP.NET Core
  • Integrate third-party tools and frameworks to build maintainable and efficient services
  • Implement patterns using dependency injection to reduce boilerplate code and improve flexibility
  • Use ASP.NET Core's out-of-the-box tools to test your applications
  • Use Docker to run your ASP.NET Core web service in an isolated and self-contained environment
  • Secure your information using HTTPS and token-based authentication
  • Integrate multiple web services using resiliency patterns and messaging techniques

Who this book is for

This book is for anyone who wants to learn how to build RESTful web services with the ASP.NET Core framework to improve the scalability and performance of their applications. Basic knowledge of C# and .NET Core will help you make the best use of the code samples included in the book.

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. Hands-On RESTful Web Services with ASP.NET Core 3
  3. Dedication
  4. About Packt
    1. Why subscribe?
  5. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  7. Section 1: Getting Started
  8. REST 101 and Getting Started with ASP.NET Core
    1. REST
      1. The importance of being REST compliant
      2. REST requirements
        1. Uniform interface
          1. Manipulation of resources through representations
          2. Self-descriptive messages
          3. Hypermedia as the Engine of Application State
        2. Stateless
        3. Client-server separation
        4. Layered system
      3. Richardson maturity model
    2. Introducing ASP.NET Core
      1. The evolution of ASP.NET
      2. The new .NET ecosystem
        1. .NET STANDARD
      3. Why use ASP.NET Core to build RESTful web services?
    3. Preparing your development environment
      1. .NET Core CLI
      2. IDEs and development tools in ASP.NET Core
    4. Summary
  9. Section 2: Overview of ASP.NET Core
  10. Overview of ASP.NET Core
    1. Setting up our .NET Core project
      1. Overview of .csproj
      2. The Program.cs file in detail
    2. Setting up an ASP.NET Core project
      1. The project structure
      2. The Program.cs and Startup.cs files
      3. Overview of controllers 
    3. Summary
  11. Working with the Middleware Pipeline
    1. Introducing middleware
      1. The middleware pipeline in practice
        1. HttpContext in ASP.NET Core
        2. Class-based middleware
        3. Conditional pipeline
    2. Understanding built-in middleware
    3. Summary
  12. Dependency Injection System
    1. Dependency inversion principle
      1. Benefits of dependency injection
    2. Dependency injection in ASP.NET Core
      1. Registering services using the dependency injection container
        1. Registering services conditionally
      2. Constructor injection
      3. Action method injection
    3. Services life cycle
      1. Transient life cycle
      2. Scoped life cycle
      3. Singleton life cycle
      4. Life cycle madness
      5. Injecting services into middleware
    4. Summary
  13. Web Service Stack in ASP.NET Core
    1. What is a controller?
      1. Identifying controllers
      2. Extending controllers
      3. The ApiController attribute
    2. Handling requests using controllers and actions
      1. Creating an in-memory repository
      2. Handling client requests
        1. Handling HTTP methods using actions
      3. Responding to requests
        1. CreateAt response
        2. Updating resources
        3. Partial updating
        4. Deleting resources
        5. Asynchronous processing and acceptance status
    3. Data transfer objects
      1. Implementing request models
      2. Implementing response models
    4. Implementing validation of requests
      1. Custom validation attributes
    5. Summary
  14. Routing System
    1. Overview of the routing system
    2. Conventional routing
    3. Attribute routing
      1. Custom attribute routing
    4. Routing constraints
      1. Custom constraints
    5. Summary
  15. Filter Pipeline
    1. Introduction to filters
    2. Concrete filter implementations
      1. Asynchronous filters
      2. The scope of filters
      3. The use of filters
      4. Life cycle and dependency injection
    3. Filter use cases
      1. Existing entity constraints
      2. Altering exceptions
    4. Summary
  16. Section 3: Building a Real-World RESTful API
  17. Building the Data Access Layer
    1. Setting up the project
    2. Implementing the domain model
      1. Designing entities
      2. Implement entities
    3. Data access using ORMs
      1. Finding the right tool for the job
    4. Implementing a data access layer using EF Core
      1. Defining the repository pattern and unit of work
      2. Connecting our repository to the database
        1. DbContext definition
        2. Implementing the repository 
      3. Transforming entities into a SQL schema
        1. Custom conversions using the Fluent API
        2. Applying the schema definition on the current data context 
        3. Generating a schema for the Artist and Genre entities
      4. Executing migrations
      5. Defining the configuration part
    5. Testing the EF Core repository
      1. Seeding data using DbContext
      2. Initializing the testing class
    6. Implementing a data access layer using Dapper
      1. Creating stored CRUD procedures
      2. Implementing the IItemRepository interface
    7. Summary
  18. Implementing the Domain Logic
    1. Implementing service classes
      1. The service class interface
      2. Implementing the mapping layer
      3. Mapping logic using Automapper
      4. The service class implementation
    2. Testing the service layer
      1. Refactoring testing classes
      2. Implementing the ItemService tests
    3. Implementing request model validation
      1. Testing request model validation
    4. Dependencies registration
    5. Summary
  19. Implementing the RESTful HTTP Layer
    1. Implementing the item controller
      1. Implementing action methods
    2. Testing controllers using the WebApplicationFactory<T> class
      1. Extending the WebApplicationFactory
      2. Testing a controller
      3. Loading test data using xUnit data attributes
    3. Improving the resilience of the API 
      1. Existence check
      2. JSON-customized errors
      3. Implementing pagination
    4. Exposing related entities 
      1. Extending the data access layer
        1. Extending the test coverage 
      2. Extending the IItemRepository interface
      3. Extending the capabilities of the service layer
      4. Improving the validation mechanism
      5. Updating the dependencies in the Startup class
      6. Adding the related controllers
        1. Extending tests for the ArtistController class
    5. A final overview
    6. Summary
  20. Advanced Concepts of Building an API
    1. Implementing the soft delete procedure
      1. Updating the IItemRepository implementation
      2. Implementing delete operations
    2. Implementing HATEOAS 
      1. Enriching our model with HATEOAS data
      2. Implementing HATEOAS in a controller
    3. The asynchronous code in ASP.NET Core
      1. Task definitions
      2. The need for asynchronous code in ASP.NET Core
      3. What's new in ASP.NET Core?
      4. Best practices in asynchronous programming
        1. Don't use async void methods
        2. Use Task.FromResult over Task.Run
        3. Enable cancellation
        4. Asynchronous code in I/O bound operations
    4. Measure response time using middleware
    5. Summary
  21. The Containerization of Services
    1. An introduction to containers
      1. Docker terminology
    2. Using Docker to run the catalog service
      1. Defining environment variables
      2. Defining the Dockerfile
      3. Executing the docker-compose command
    3. Optimizing Docker images
      1. An overview of different Docker images
      2. Multi-stage builds on the catalog service
    4. Summary
  22. Service Ecosystem Patterns
    1. An introduction to the cart service
      1. The theory behind the mediator pattern
      2. The domain model and the data access layer
        1. The ICartRepository Redis implementation
      3. Handlers and routes implementation
        1. Exposing functionalities using CartController
    2. Implementing resilient communication using an HTTP client
      1. Implementing the catalog HTTP client 
      2. Integrating an HTTP client into the cart service
      3. Implementing resilience using Polly.NET
        1. Integrating Polly into ICatalogService
    3. Sharing events using an event bus
      1. Setting up a RabbitMQ instance and publishing an event
    4. Running the cart service using Docker
    5. Summary
  23. Implementing Worker Services Using .NET Core
    1. Introducing worker services
      1. Understanding the worker services life cycle
      2. Hosting models
    2. Implementing a health-checking worker
      1. Project structure overview
    3. Running a worker service on Docker
    4. Consuming the sold-out event
      1. Creating a sold-out handler
        1. Testing the sold-out process
      2. Configuring the background service
    5. Summary
  24. Securing Your Service
    1. Overview of secure communication
      1. Securing data using HTTPS  
      2. Enforcing HTTPS in ASP.NET Core
      3. HTTP/2 on Kestrel
      4. Enabling CORS in ASP.NET Core
        1. Implementing CORS using the middleware approach
        2. Implementing CORS using the attribute approach
    2. Securing APIs with token-based authentication
      1. Implementing token-based authentication
        1. Defining the service layer
      2. Applying authentication on the controller
      3. Storing data using EF Core
        1. Declaring the identity database context
      4. Testing authentication
    3. Summary
  25. Section 4: Advanced Concepts for Building Services
  26. Caching Web Service Responses
    1. Introduction to the HTTP caching system
      1. The HTTP caching specification
    2. Implementing response caching in ASP.NET Core
    3. Implementing a distributed cache
      1. Implementing the IDistributedCache interface
        1. Inject memory cache into the tests
    4. Summary
  27. Logging and Health Checking
    1. Logging in ASP.NET Core
      1. The key characteristics of logging
    2. Implementing the logging part
      1. Exception logging
      2. Verifying logging using Moq
    3. Implementing logging providers
    4. Implementing a custom log provider in tests
    5. Web service health check 
      1. Implementing health checks on databases
      2. Implementing custom health checks
    6. Summary
  28. Deploying Services on Azure
    1. Getting started with Azure
    2. Pushing containers into Azure Container Registry
      1. Creating an Azure Container Registry
    3. Configuring Azure Container Instances
    4. Configuring app services
      1. Creating an app service using a container image
    5. Summary
  29. Documenting Your API Using Swagger
    1. Understanding OpenAPI
      1. Implementing the Swagger project
    2. Implementing OpenAPI in ASP.NET Core services
      1. Understanding ASP.NET Core's conventions
        1. Custom conventions
    3. Summary
  30. Testing Services Using Postman
    1. Overview of Postman
      1. Testing the API using Postman
      2. Using the runner to test the APIs
    2. Importing a collection using OpenAPI
    3. Summary
  31. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think
3.17.150.89