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

C# 7 and .NET Core 2.0 High Performance

Book Description

Improve the speed of your code and optimize the performance of your apps

About This Book
  • Understand the common performance pitfalls and improve your application's performance
  • Get to grips with multi-threaded and asynchronous programming in C#
  • Develop highly performant applications on .NET Core using microservice architecture
Who This Book Is For

This book is for .NET developers looking at improving the speed of their code or simply wanting to take their skills to the next level. Basic C# knowledge is assumed.

What You Will Learn
  • Measure application performance using BenchmarkDotNet
  • Explore the techniques to write multithreaded applications
  • Leverage TPL and PLinq libraries to perform asynchronous operations
  • Get familiar with data structures to write optimized code
  • Understand design techniques to increase your application's performance
  • Learn about memory management techniques in .NET Core
  • Develop a containerized application based on microservices architecture
  • Learn tools and techniques to monitor application performance
In Detail

While writing an application, performance is paramount. Performance tuning for realworld applications often involves activities geared toward fnding bottlenecks; however, this cannot solve the dreaded problem of slower code. If you want to improve the speed of your code and optimize an application's performance, then this book is for you. C# 7 and .NET Core 2.0 High Performance begins with an introduction to the new features of what?explaining how they help in improving an application's performance. Learn to identify the bottlenecks in writing programs and highlight common performance pitfalls, and learn strategies to detect and resolve these issues early. You will explore multithreading and asynchronous programming with .NET Core and learn the importance and effcient use of data structures. This is followed with memory management techniques and design guidelines to increase an application's performance. Gradually, the book will show you the importance of microservices architecture for building highly performant applications and implementing resiliency and security in .NET Core. After reading this book, you will learn how to structure and build scalable, optimized, and robust applications in C#7 and .NET.

Style and approach

This book will be a step by step easy to follow guide with focused examples to increase performance of applications and provide optimization techniques.

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 files e-mailed directly to you.

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. C# 7 and .NET Core 2.0 High Performance
  3. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  4. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  5. 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
  6. What's New in .NET Core 2 and C# 7?
    1. Evolution of .NET
    2. New improvements in .NET Core 2.0
      1. Performance improvements
        1. RyuJIT compiler in .NET Core
        2. Profile guided optimization
      2. Simplified packaging
    3. Upgrading path from .NET Core 1.x to 2.0
      1. 1. Install .NET Core 2.0
      2. 2. Upgrade TargetFramework
      3. 3. Update .NET Core SDK version
      4. 4. Update .NET Core CLI
      5. Changes in ASP.NET Core Identity
    4. Exploring .NET Core CLI and New Project Templates
    5. Understanding .NET Standard
      1. Versioning of .NET Standard
      2. New improvements in .NET Standard 2.0
        1. More APIs in .NET Standard 2.0
        2. Compatibility mode
    6. Creating a .NET Standard library
    7. What comes with ASP.NET Core 2.0
      1. ASP.NET Core Razor Pages
        1. Automatic Page and View compilation on publishing
        2. Razor support for C# 7.1
      2. Simplified configuration for Application Insights
      3. Pooling connections in Entity Framework Core 2.0
    8. New features in C# 7.0
      1. Tuples
      2. Patterns
        1. Constant pattern
        2. Type pattern
        3. Var pattern
      3. Reference returns
      4. Expression bodied member extended
      5. Creating Local Functions
      6. Out variables
      7. Async Main
    9. Writing quality code
    10. Summary
  7. Understanding .NET Core Internals and Measuring Performance
    1. .NET Core internals
      1. CoreFX
      2. CoreCLR
      3. Understanding MSIL, CLI, CTS, and CLS
      4. How the CLR works
      5. From compilation to execution – Under the hood
      6. Garbage collection
        1. Generations in GC
      7. .NET Native and JIT compilation
    2. Utilizing multiple cores of the CPU for high performance
    3. How releasing builds increases performance
    4. Benchmarking .NET Core 2.0 applications
      1. Exploring BenchmarkDotNet
      2. How it works
      3. Setting parameters
      4. Memory diagnostics using BenchmarkDotnet
      5. Adding configurations
    5. Summary
  8. Multithreading and Asynchronous Programming in .NET Core
    1. Multithreading versus asynchronous programming
    2. Multithreading in .NET Core
      1. Multithreading caveats
      2. Threads in .NET Core
        1. Creating threads in .NET Core
        2. Thread lifetime
        3. The thread pool in .NET
      3. Thread synchronization
        1. Monitors
      4. Task parallel library (TPL)
        1. Creating a task using TPL
        2. Task-based asynchronous pattern (TAP)
          1. Naming convention
          2. Return type
          3. Parameters
          4. Exceptions
          5. Task status
          6. Task cancellation
          7. Task progress reporting
        3. Implementing TAP using compilers
        4. Implementing TAP with greater control over Task
      5. Design patterns for parallel programming
        1. Pipeline pattern
        2. Dataflow pattern
        3. Producer/consumer pattern
        4. Parallel.ForEach
        5. Parallel LINQ (PLINQ)
    3. Summary
  9. Data Structures and Writing Optimized Code in C#
    1. What are data structures?
    2. Understanding the use of Big O notation to measure the performance and complexity of an algorithm
      1. Logarithms
    3. Choosing the right data structure for performance optimization
      1. Arrays
      2. Lists
      3. Stacks
      4. Queue
      5. Linked lists
        1. Singly linked lists
        2. Doubly linked lists
        3. Circular linked lists
      6. Dictionaries, hashtables, and hashsets
      7. Generic lists
    4. Best practices in writing optimized code in C#
      1. Boxing and unboxing overhead
      2. String concatenation
      3. Exception handling
      4. For and foreach
      5. Delegates
    5. Summary
  10. Designing Guidelines for .NET Core Application Performance
    1. Coding principles
      1. Naming convention
      2. Code comments
      3. One class per file
      4. One logic per method
    2. Design principles
      1. KISS (Keep It Simple, Stupid)
      2. YAGNI (You Aren't Gonna Need It)
      3. DRY (Don't Repeat Yourself)
      4. Separation of Concerns (SoC)
      5. SOLID principles
        1. Single Responsibility Principle
        2. Open Closed principle
          1. Parameters
          2. Inheritance
          3. Composition
        3. Liskov principle
        4. The Interface Segregation principle
        5. The Dependency Inversion principle
      6. Caching
      7. Data structures
      8. Communication
        1. Using lighter interfaces
        2. Minimizing message size
        3. Queuing communication
      9. Resource management
        1. Avoiding improper use of threads
        2. Disposing objects in a timely fashion
        3. Acquiring resources when they are required
      10. Concurrency
    3. Summary
  11. Memory Management Techniques in .NET Core
    1. Memory allocation process overview
    2. Analysing CLR internals through the SOS debugger in .NET Core
    3. Memory fragmentation
    4. Avoiding finalizers
    5. Best practices for disposing of objects in .NET Core
      1. Introduction to the IDisposable interface
      2. What are unmanaged resources?
      3. Using IDisposable
      4. When to implement the IDisposable interface
      5. Finalizer and Dispose
    6. Summary
  12. Securing and Implementing Resilience in .NET Core Applications
    1. Introduction to resilient applications
      1. Resilient policies
        1. Reactive policies
          1. Implementing the retry pattern
          2. Implementing circuit breaker
          3. Wrapping the circuit breaker with retry
          4. Fallback policy with circuit breaker and retry
        2. Proactive policies
          1. Implementing timeout
          2. Implementing caching
          3. Implementing health checks
    2. Storing sensitive information using Application Secrets
    3. Protecting ASP.NET Core APIs
      1. SSL (Secure Socket Layer)
        1. Enabling SSL in an ASP.NET Core application
      2. Preventing CSRF (Cross-Site Request Forgery) attacks
      3. Reinforcing security headers
      4. Adding the HTTP strict transport security header
        1. Adding the X-Content-Type-Options header
        2. Adding the X-Frame-Options header
        3. Adding the X-Xss-Protection header
        4. Adding the Content-Security-Policy header
        5. Adding the referrer-policy header
        6. Enabling CORS in the ASP.NET Core application
      5. Authentication and authorization
        1. Using ASP.NET Core Identity for authentication and authorization
      6. Authentication
      7. Authorization
      8. Implementing authentication and authorization using the ASP.NET Core Identity framework
        1. Adding more properties in the user table
    4. Summary
  13. Microservices Architecture
    1. Microservices architecture
      1. Benefits of microservices architecture
      2. Standard practice when developing microservices
      3. Types of microservices
        1. Stateless microservices
        2. Stateful microservices
      4. DDD
      5. Data manipulation with microservices
        1. Wrapping microservices behind an API gateway
        2. Denormalizing data into a flat schema for read/query purposes
      6. Consistency across business scenarios
      7. Communication with microservices
      8. Database architecture in microservices
        1. Tables per service
        2. Database per service
        3. Challenges in segregating tables or databases per service
      9. What is API composition?
      10. CQRS
    2. Developing microservices architecture with .NET Core
      1. Creating a sample app in .NET Core using microservices architecture
        1. Solution structure
        2. Logical architecture
        3. Developing a Core infrastructure project
          1. Creating the BaseEntity class
          2. The UnitOfWork pattern
          3. Creating a repository interface
          4. Logging
          5. Creating the APIComponents infrastructure project
        4. Developing an identity service for user authorization
          1. OpenIddict connect flows
          2. Creating the identity service project
        5. Implementing the vendor service
        6. Creating a vendor domain
        7. Creating the vendor infrastructure
        8. Creating the vendor service
        9. Implementing the mediator pattern in the vendor service
    3. Deploying microservices on Docker containers
      1. What is Docker?
      2. Using Docker with .NET Core
      3. Running Docker images
    4. Summary
  14. Monitoring Application Performance Using Tools
    1. Application performance key metrics
      1. Average response time
      2. Apdex scores
      3. Percentage of errors
      4. Request rate
      5. Throughput/endpoints
      6. CPU and memory usage
    2. Tools and techniques to measure performance
      1. Introducing App Metrics
      2. Setting up App Metrics with ASP.NET Core
      3. Tracking middleware
        1. Adding tracking middleware
        2. Setting up configuration
      4. Adding visual reports
        1. Setting up InfluxDB
          1. Setting up the Windows subsystem for Linux
          2. Installing InfluxDB
        2. Installing Grafana
        3. Adding the InfluxDB dashboard
        4. Configuring InfluxDB
        5. Modifying the Configure and ConfigureServices methods in Startup
        6. Testing the ASP.NET Core App and reporting on the Grafana dashboard
    3. Summary
  15. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think