Book Image

C# 7 and .NET Core 2.0 Blueprints

By : Dirk Strauss, Jas Rademeyer
Book Image

C# 7 and .NET Core 2.0 Blueprints

By: Dirk Strauss, Jas Rademeyer

Overview of this book

.NET Core is a general purpose, modular, cross-platform, and open source implementation of .NET. With the latest release of .NET Core, many more APIs are expected to show up, which will make APIs consistent across .Net Framework, .NET Core, and Xamarin. This step-by-step guide will teach you the essential .NET Core and C# concepts with the help of real-world projects. The book starts with a brief introduction to the latest features of C# 7 and .NET Core 2.0 before moving on to explain how C# 7 can be implemented using the object-oriented paradigm. You'll learn to work with relational data using Entity Framework and see how to use ASP.NET Core practically. This book will show you how .NET Core allows the creations of cross-platform applications. You'll also learn about SignalR to add real-time functionality to your application. Then you will see how to use MongoDB and how to implement MongoDB into your applications. You'll learn about serverless computing and OAuth concepts, along with running ASP.NET Core applications with Docker Compose. This project-based guide uses practical applications to demonstrate these concepts. By the end of the book, you'll be proficient in developing applications using .NET Core 2.0.
Table of Contents (17 chapters)
Title Page
Copyright and Credits
Dedication
Packt Upsell
Contributors
Preface
Index

Building the server


We'll need to build a server for our chat program, which will contain the methods we want to call from our connected clients. We'll use the SignalR Hubs API, which provides the methods needed for connected clients to communicate with our chat server.

SignalR Hub subclass

We now need to create the SignalR Hub. To do this, perform the following steps:

  1. Add a class to your project to handle the server-side of the chat. We'll call it Chat:

This will need to be a subclass of the SignalR Hub class. Make sure to add the using directive for Micosoft.AspNetCore.SignalR. Visual Studio's Quick Actions works well for this:

  1. Now add a Task method to the class to handle the sending of the messages:
        public Task Send(string sender, string message) 
        { 
            return Clients.All.InvokeAsync("UpdateChat", sender, 
            message); 
        } 

This method will be called through any of the connected clients and will invoke all connected clients' Send function, passing through...