Hands-On RESTful Web Services with ASP.NET Core 3
eBook - ePub

Hands-On RESTful Web Services with ASP.NET Core 3

Design production-ready, testable, and flexible RESTful APIs for web applications and microservices

  1. 510 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Hands-On RESTful Web Services with ASP.NET Core 3

Design production-ready, testable, and flexible RESTful APIs for web applications and microservices

About this book

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.

Trusted by 375,005 students

Access to over 1.5 million titles for a fair monthly price.

Study more efficiently using our study tools.

Information

Year
2019
Print ISBN
9781789537611
eBook ISBN
9781789539240

Section 1: Getting Started

In this section, we will introduce you to all the macro concepts of REST APIs and ASP.NET Core. You will also learn about local development.
This section includes the following chapter:
  • Chapter 1, REST 101 and Getting Started with ASP.NET Core

REST 101 and Getting Started with ASP.NET Core

Nowadays, almost all applications rely on web services. A lot of them operate using the RESTful method. The resource-centric approach and the simplicity of the REST style have become an industry standard. Therefore, it is essential to understand the theory behind the REST way of working and why it is important. This chapter will introduce you to the Representational State Transfer (REST) method. We will see what the definition of REST is and how to identify REST-compliant web services. We will also introduce .NET Core 3.1 and ASP.NET Core, the brand new version of the open-source, cross-platform framework provided by Microsoft.
In summary, this chapter covers the following topics:
  • Overview of REST architectural elements
  • A brief introduction to the .NET ecosystem
  • Why you should choose .NET to build a RESTful web service
By the end of this chapter, you will have an overview of some useful tools and IDEs that you can use to start developing on .NET Core.
This chapter will cover some of the base concepts of .NET Core 3.1 and ASP.NET Core. You need to have either Windows, Linux, or macOS installed. The setup process will depend on which OS you are using. We'll look at the different tools that can be used to develop apps and web services in .NET Core.

REST

What is REST? Representational State Transfer (REST) is usually defined as a software architecture style that applies some constraints to a web service. It identifies a set of resource-centric rules that describe the roles and the interaction between the constraints of a distributed hypermedia system, instead of focusing on the implementation of the components. Although it is quite rare to find a REST service that does not use HTTP, the definition does not mention any of these topics and instead describes REST as media- and protocol-agnostic.
The preceding definition can be further explained with an example. Consider an e-commerce website. When you browse a list of products and click on one them, the browser interprets your click as a request to a specific resource; in this case, the details of the product you clicked on. The browser makes an HTTP call to the URI, which corresponds to the details of the product and asks for a specific resource using the URI. This process is shown in the following diagram:
The concept of REST is pretty similar: the client asks the server for a specific resource, and this allows them to navigate and obtain other information about resources stored on the server.

The importance of being REST compliant

Before we discuss REST, we should understand the importance of web services in the modern application development world.
A typical modern application uses web services to obtain and query data. Any company that develop any product or solution uses web services to deliver and track information. This is because it can be difficult to replicate all the data and every behavior you need for every client who uses your application. Web services are also useful for providing access to third-party clients and services. Consider, for example, the Application Programming Interfaces (APIs) of Google Maps or Instagram: these platforms expose information through HTTP to share it with other companies and services.
The REST approach to web service architecture became increasingly popular because it is straightforward and clear. Unlike some old methods, such as the Simple Object Access Protocol (SOAP) or the Windows Communication Foundation (WCF), REST services provide a clean way of querying data. Different information can be obtained using different URIs without the need for adding any overhead to requests.
Today, more than ever, our services need to be prepared to scale so that they can be adapted to consumers. In a world of different technologies and distributed teams around the globe, we should be able to transform our architecture to solve complex business challenges. HTTP and REST help us address these challenges.

REST requirements

Roy Thomas Fielding is the main person behind the formalization of the HTTP protocol and the REST style: he described them in his dissertation, Architectural Styles and the Design of Network-based Software Architectures (https://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm). Moreover, Fielding's dissertation clearly defines the constraints of a REST system, as follows:
  • Uniform interface
  • Stateless
  • Cacheable
  • Client-server
  • Layered system
  • Code on demand
All of these, apart from code on demand, are essential when we wish to define a web service as REST-compliant. To explain these concepts, we will use the APIs provided by The New York Times: https://developer.nytimes.com/. To use these APIs, you need to go through a pre-authentication process. You can get an API key for this from the following link: https://developer.nytimes.com/signup.

Uniform interface

Uniform interface refers to the separation between the client and the server. This is advantageous because it means that the two systems are independent. The first principle of a uniform interface is that it should be resource-based, which means that we should start to think in a resource-oriented way. Therefore, every object or entity inside our system is a resource and a URI uniquely identifies each of them. Furthermore, if we think about the HTTP protocol, each resource is presented to the client in the form of XML or JSON to decouple the client from the server.
Let's use The New York Times APIs to understand this topic. Consider the following URI:
http://api.nytimes.com/svc/archive/v1/2018/6.json?api-key={your_api_key}
It contains some precise information about the resources we are asking for, including the following:
  • The fact that we are getting information from the archive section of the newspaper data
  • The specific month we are asking for; in this case, June 2018
  • The fact that the resource will be serialized using JSON format

Manipulation of resources through representations

Let's take another API request provided by The New York Times:
https://api.nytimes.com/svc/books/v3/lists.json?api-key={your_api_key}&list=hardcover-fiction
This provides a list of books based on a category; in this case, this category is Hardcover Fiction. Let's analyze the response:
{
"status": "OK",
"copyright": "Copyright (c) 2018 The New York Tim...

Table of contents

  1. Title Page
  2. Copyright and Credits
  3. Dedication
  4. About Packt
  5. Contributors
  6. Preface
  7. Section 1: Getting Started
  8. REST 101 and Getting Started with ASP.NET Core
  9. Section 2: Overview of ASP.NET Core
  10. Overview of ASP.NET Core
  11. Working with the Middleware Pipeline
  12. Dependency Injection System
  13. Web Service Stack in ASP.NET Core
  14. Routing System
  15. Filter Pipeline
  16. Section 3: Building a Real-World RESTful API
  17. Building the Data Access Layer
  18. Implementing the Domain Logic
  19. Implementing the RESTful HTTP Layer
  20. Advanced Concepts of Building an API
  21. The Containerization of Services
  22. Service Ecosystem Patterns
  23. Implementing Worker Services Using .NET Core
  24. Securing Your Service
  25. Section 4: Advanced Concepts for Building Services
  26. Caching Web Service Responses
  27. Logging and Health Checking
  28. Deploying Services on Azure
  29. Documenting Your API Using Swagger
  30. Testing Services Using Postman
  31. Other Books You May Enjoy

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn how to download books offline
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.5M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1.5 million books across 990+ topics, we’ve got you covered! Learn about our mission
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more about Read Aloud
Yes! You can use the Perlego app on both iOS and Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app
Yes, you can access Hands-On RESTful Web Services with ASP.NET Core 3 by Samuele Resca in PDF and/or ePUB format, as well as other popular books in Informatique & Programmation en C#. We have over 1.5 million books available in our catalogue for you to explore.