.NET Standard 2.0 Cookbook - Fiqri Ismail - E-Book

.NET Standard 2.0 Cookbook E-Book

Fiqri Ismail

0,0
39,59 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

The .NET Standard is a standard that represents a set of APIs that all .NET platforms have to implement, making it easy for developers to access and use one common library for their development needs. This book begins with a quick refresher, helping you understand the mechanics of the new standard and offering insight into how it works. You’ll explore the core library concepts, such as working with collections, configurations, I/O, security, and multithreading. You’ll explore the iOS and Android libraries of Xamarin and we’ll guide you through creating a .NET Standard 2.0 library, which you’ll use with both Android and iOS applications.
In the final chapters, you’ll learn the various debugging and diagnostics tools to deliver quality libraries and create a NuGet package of the .NET Standard 2.0 library. By the end of this book, you’ll be able to expand your current workflow to various .NET flavors and have the essential skills to create a .NET Standard 2.0 library from scratch to package and deliver it to the world.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 249

Veröffentlichungsjahr: 2018

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



.NET Standard 2.0 Cookbook

 

 

Develop high quality, fast and portable applications by leveraging the power of .NET Standard Library

 

 

Fiqri Ismail

 

BIRMINGHAM - MUMBAI

.NET Standard 2.0 Cookbook

Copyright © 2018 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

Commissioning Editor: Richa TripathiAcquisition Editors: Denim Pinto/Nitin DasanContent Development Editor: Nikhil BorkarTechnical Editor: Jijo MaliyekalCopy Editor: Safis EditingProject Coordinator: Ulhas KambaliProofreader: Safis EditingIndexer: Priyanka DhadkeGraphics: Tania DuttaProduction Coordinator: Shraddha Falebhai

First published: May 2018

Production reference: 1090518

Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.

ISBN 978-1-78883-466-7

www.packtpub.com

mapt.io

Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.

Why subscribe?

Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals

Improve your learning with Skill Plans built especially for you

Get a free eBook or video every month

Mapt is fully searchable

Copy and paste, print, and bookmark content

PacktPub.com

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.

At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.

Contributors

About the author

Fiqri Ismail is an experienced architect with a demonstrated history of working in the information technology and services industry. Skilled in ASP.NET, ASP.NET MVC, ASP.NET Core, Entity Framework, C#, Microsoft Azure, PHP, and Delphi, he is a Microsoft MVP in Visual Studio and Related Technologies for 11 concussive years to date. He is also a community leader and a speaker at Sri Lanka Developer Forum and in Global Events such as Microsoft Tech Summit and Global Azure Bootcamp.

I would love thank Mr. Arjuna Seneviratna, my first teacher on web-related technologies and Mr. Saman Chandana Silva, who helped me buy my first computer. Also, I would like to thank my wife and daughter for giving their time to complete this book.

About the reviewer

Hansamali Gamage is an experienced professional and Microsoft MVP in Visual Studio and technologies from Sri Lanka. She possesses over 5 years of experience in. NET stack and Azure-related services. She is a frequent speaker at local and global tech events, a tech enthusiast, and an award winning writer on the Microsoft Tech Net forum. She works at TIQRI, previously known as Exilesoft, a technology-focused software engineering company with offices in Scandinavia, Asia, and Australia.

I would like to thank my loving family for their continuous help and support.

Packt is searching for authors like you

If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.

Table of Contents

Title Page

Copyright and Credits

.NET Standard 2.0 Cookbook

Packt Upsell

Why subscribe?

PacktPub.com

Contributors

About the author

About the reviewer

Packt is searching for authors like you

Preface

Who this book is for

What this book covers

To get the most out of this book

Download the example code files

Code in Action

Conventions used

Sections

Getting ready

How to do it…

How it works…

There's more…

See also

Get in touch

Reviews

Back to Basics

Technical requirements

Introduction 

Creating a C#-based console application

Getting ready

How to do it...

How it works...

See also

Creating a C# class library 

Getting ready

How to do it...

How it works...

Creating a classic Windows-based application to use the library

Getting ready

How to do it...

How it works...

Creating a WPF-based application to use the library 

Getting ready

How to do it...

How it works...

Hello Universe – My first .NET Standard class library

Getting ready

How to do it...

How it works...

Creating a Windows console-based application to use the library

Getting ready

How to do it...

How it works...

Creating an ASP.NET Core-based web application to use the library

Getting ready

How to do it...

How it works...

Primitives, Collections, LINQ, and More

Technical requirements

Introduction

Building a .NET Standard 2.0 library that uses primitives

Getting ready 

How to do it...

How it works...

A .NET console application to use the library

Getting ready

How to do it...

How it works...

Creating collections

Getting ready

How to do it...

How it works...

A WPF application to use the library

Getting ready

How to do it...

How it works...

Describing our library with Reflections

Getting ready

How to do it...

How it works...

A .NET Core console application to use the library

Getting ready 

How to do it...

How it works...

Building a .NET Standard 2.0 library that uses LINQ

Getting ready

How to do it...

How it works...

An ASP.NET MVC application to use the library

Getting ready

How to do it...

How it works...

Working with Files

Technical requirements

Introduction

Setting up .NET Core in Ubuntu

Getting ready

How to do it...

How it works...

Creating a log as text

Getting ready

How to do it...

How it works...

Creating an ASP.NET Core application in Ubuntu to use the library

Getting ready

How to do it..

How it works...

Setting up .NET Core in macOS

Getting ready

How to do it...

How it works...

Reading from a comma separated (CSV) text file

Getting ready

How to do it...

How it works...

Creating a .NET Core console application in macOS to use the library

Getting ready

How to do it...

How it works...

Compressing? Why not?

Getting ready

How to do it...

How it works...

Creating a classic Windows application to use the library

Getting ready

How to do it...

How it works...

Encrypting and decrypting content in a text file

Getting ready

How to do it...

How it works...

Creating a classic Windows application to use the library

Getting ready

How to do it...

How it works...

Functional Programming

Technical requirements

Introduction

Creating a .NET Standard 2.0 library

Getting ready

How to do it...

How it works...

Creating a .NET Core console application to use the library

Getting ready

How to do it...

How it works...

Creating a .NET Standard 2.0 library that uses tuples

Getting ready

How to do it...

How it works...

Creating a Razor Pages web application to use the library

Getting ready

How to do it...

How it works...

Creating a .NET Standard 2.0 library that uses delegates and lambda expressions

Getting ready

How to do it...

How it works...

Creating a .NET console application to use the library

Getting ready

How to do it...

How it works...

XML and Data

Technical requirements

Introduction

Creating a library that reads and writes to an XML file

Getting ready

How to do it...

How it works...

Creating an ASP.NET MVC application to use the XMLLib library

Getting ready

How to do it...

How it works...

Processing an XML file using LINQ to XML

Getting ready

How to do it...

How it works...

Creating a .NET Core console application to use the library

Getting ready

How to do it...

How it works...

Exploring Threading

Technical requirements

Introduction

Creating a library that can perform several things at once

Getting ready

How to do it...

How it works...

Creating a .NET Core console application to use the library

Getting ready

How to do it...

How it works...

Creating an async method with tasks

Getting ready

How to do it...

How it works...

Creating a WPF application to use the library

Getting ready

How to do it...

How it works...

Creating a thread pool

Getting ready

How to do it...

How it works...

Creating a .NET console application to use the library

Getting ready

How to do it...

How it works...

Networking

Technical requirements

Introduction

A library that displays an IP address and the name using sockets

Getting ready

How to do it...

How it works...

Creating a classic Windows application to use the library

Getting ready

How to do it...

How it works...

Creating a library that sends mail

Getting ready

How to do it...

How it works...

Creating a WPF application to use the library

Getting ready

How to do it...

How it works...

Creating a library to call a REST API

Getting ready

How to do it...

How it works...

Creating an ASP.NET MVC application to use the library

Getting ready

How to do it..

How it works...

To iOS with Xamarin

Technical requirements

Introduction

Installing Visual Studio for Mac

Getting ready

How to do it...

How it works...

Hello iOS – Creating a Xamarin iOS app

Getting ready

How to do it...

How it works...

Creating the .NET Standard 2.0 library

Getting ready

How to do it...

How it works...

Putting things together and testing the application

Getting ready

How to do it...

How it works...

To Android with Xamarin

Technical requirements

Introduction

Hello Android – Creating a Xamarin Android app

Getting ready

How to do it...

How it works...

Adding a .NET Standard 2.0 library to the Xamarin project

Getting ready

How to do it...

How it works...

Putting things together and testing the application

Getting ready

How to do it...

How it works...

Let’s Fine-Tune Our Library

Technical requirements

Introduction

A library logs things itself

Getting ready

How to do it...

How it works...

Creating a .NET Core console application to use the library

Getting ready

How to do it...

How it works...

Informing the end user – Exception handling and error messages

Getting ready

How to do it...

How it works...

Using Visual Studio 2017 diagnostics and debugging tools

Getting ready

How to do it...

How it works...

Packaging and Delivery

Technical requirements

Introduction

Creating a .NET Standard 2.0 library

Getting ready

How to do it...

How it works...

Creating a NuGet package of your library

Getting ready

How to do it...

How it works...

Submitting the package to NuGet package manager

Getting ready

How to do it...

How it works...

Creating a classic Windows application and testing the NuGet package

Getting ready

How to do it...

How it works...

Deploying

Technical requirements

Introduction

Creating a free Azure Cloud subscription

Getting ready

How to do it...

How it works...

Creating an ASP.NET core web application to use the library from NuGet

Getting ready

How to do it...

How it works...

Deploying the application to Azure Cloud

Getting ready

How to do it...

How it works...

Preface

This book is a step-by-step guide to building .NET Standard 2.0 Library and its usage in various .NET platforms. Also, we will demonstrate the usage of the library using ASP.NET, ASP.NET Core, ASP.NET MVC, Xamarin iOS, and Xamarin Android. Finally, we will guide you through how to package and send your library using the NuGet package manager.

Who this book is for

This book is for developers who need to get in touch with the new .NET Standard 2.0 Library. If you have a basic knowledge of C# and what a library can do to your code, that's enough to understand this book. Also, if you are a developer who writes third-party libraries, this will give you a fair idea of what .NET Standard 2.0 Library is and how you can cater to various .NET based platforms in Windows, Linux, and macOS using it.

What this book covers

Chapter 1, Back to Basics, gives an overview of where it all began for the starters trying to build their first .NET based class library. It talks about the problems with this approach. Getting started with a simple project and using it with a console application and a classic Windows based application, the chapter talks about the issues with the current approach of libraries available. It introduces readers to .NET Standard 2.0 and its versions. How to create the first .NET Standard 2.0 library and use it with various .NET flavors such as .NET Framework and .NET Core applications is covered.

Chapter 2, Primitives, Collections, LINQ, and More, explores the Core of .NET Standard 2.0. It talks about Primitives, Collections, Reflection, and LINQ. The areas supported so far are covered. It makes use of features among different flavors of .NET Framework.

Chapter 3, Working with Files, provides an explanation of System.IO and System.Security within the .NET Standard 2.0 and the usage of read write operations using the filesystem. Also, it introduces usage in cross-platform versions of .NET Core in Ubuntu and macOS.

Chapter 4, Functional Programming, introduces functional programming capabilities in C# and how to use them in .NET Standard 2.0 Library.

Chapter 5, XML and Data, explains the usage of System.XML and System.Data within .NET Standard 2.0 for creating XML Documents and the usage of Data Tables.

Chapter 6, Exploring Threading, talks about Thread support for creating a multithreaded .NET Standard 2.0 library. This is in addition to the usage of Tasks as asynchronous capabilities of C# within the library.

Chapter 7, Networking, focuses on the usage of System.Net within .NET Standard 2.0. It dives into Sockets, Http, and Mail and how to use them in a .NET Standard Library 2.0.

Chapter 8, To iOS with Xamarin, outlines creating a simple mobile-based application using Visual Studio for Mac mobile iOS tools. The chapter guides through to create a .NET Standard 2.0 library and use it with the built iOS application.

Chapter 9, To Android with Xamarin, takes you through creating a simple mobile-based application using Visual Studio for Mac and Android tools. It showcases how to create a .NET Standard 2.0 library and use it with the built Android application.

Chapter 10, Let's Fine-Tune Our Library, demonstrates how to fine-tune .NET Standard 2.0 library using Debugging tools and Diagnostics tools. Also, it helps you capture exceptions and ensure that the end user has a solid experience using our .NET Standard Library 2.0.

Chapter 11, Packaging and Delivery, discusses how to deliver a completed .NET Standard 2.0 Library to the world. How to use NuGet package manager, creating a package, and delivering it is covered.

Chapter 12, Deploying, informs how to create a .NET Standard Library 2.0, use it with ASP.NET Core web applications, and deploy it to Azure Cloud.

To get the most out of this book

This book assumes that readers have basic knowledge of C#. Also, it assumes basic knowledge of using Visual Studio, installing packages using NuGet, and referencing libraries within projects from other projects.

Basic knowledge of using command-line tools such as bash on Ubuntu and terminal in macOS will be an added advantage but not a must.

Download the example code files

You can download the example code files for this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

Log in or register at

www.packtpub.com

.

Select the

SUPPORT

tab.

Click on

Code Downloads & Errata

.

Enter the name of the book in the

Search

box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

WinRAR/7-Zip for Windows

Zipeg/iZip/UnRarX for Mac

7-Zip/PeaZip for Linux

The code bundle for the book is also hostedon GitHub athttps://github.com/PacktPublishing/DotNET-Standard-2-Cookbook. We also have other code bundles from our rich catalog of books and videos available athttps://github.com/PacktPublishing/. Check them out!

 

Code in Action

Visit the following link to check out videos of the code being run:https://goo.gl/GwN5Xq

Sections

In this book, you will find several headings that appear frequently (Getting ready, How to do it..., How it works..., There's more..., and See also).

To give clear instructions on how to complete a recipe, use these sections as follows:

Getting ready

This section tells you what to expect in the recipe and describes how to set up any software or any preliminary settings required for the recipe.

How to do it…

This section contains the steps required to follow the recipe.

How it works…

This section usually consists of a detailed explanation of what happened in the previous section.

There's more…

This section consists of additional information about the recipe in order to make you more knowledgeable about the recipe.

See also

This section provides helpful links to other useful information for the recipe.

Get in touch

Feedback from our readers is always welcome.

General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packtpub.com.

Back to Basics

In this chapter, we will cover the following recipes:

Creating a C#-based console application

Creating a C# class library

Creating a classic Windows application to use the library

Creating a WPF-based application to use the library 

Hello Universe – My first .NET Standard class library

Creating a Windows console-based application to use the library

Creating a ASP.NET Core-based web application to use the library

Technical requirements

Readers should have a basic knowledge of C#. They should also have a basic knowledge of using Visual Studio, installing packages using NuGet, and referencing libraries within projects from other projects.

The code files for this chapter can be found on GitHub:https://github.com/PacktPublishing/DotNET-Standard-2-Cookbook/tree/master/Chapter01

Check out the following video to see the code in action:https://goo.gl/PoR4HM

Introduction 

Microsoft .NET is a general-purpose development platform with support for multiple programming languages, which is a key feature. Other key features include asynchronous and concurrent programming models and native interoperability. The .NET Framework supports multiple programming languages, such as C#, VB.NET, and F#, which are actively developed and supported by Microsoft. In this book, we are going to look at C#. 

C# is a modern, object-oriented, type-safe programming language that helps developers build robust, secure applications using the .NET Framework. C# was introduced with .NET Framework 1.0 in 2002. Since that time, C# has evolved and matured. At the time of writing, the current version of C# is 7.0 and .NET has various flavors to use with the following: 

.NET Framework

: The full flavor of .NET that is distributed with Windows. Used by developers to build ASP.NET 4.5/4.6 under Windows or desktop Windows applications.

.NET Core

: Another flavor of .NET that runs under Windows, Mac, and Linux. Used by developers to build cross-platform .NET-based applications including cross-platform web applications, using ASP.NET Core.

Xamarin

: A mono-based framework used for mobile applications for iOS, Android, and Windows phone devices. macOS desktop applications are supported with this flavor.

.NET Standard

: A replacement for

Portable Class Libraries

(

PCL

) used by developers to share code among all platforms, but supported with APIs in the latest version, 2.0. Also, you should note that .NET Standard 2.0 is supported in .NET Core 2.0, .NET Framework 4.6.1, and later versions, as well as in Visual Studio 2017 (version 15.3).

Creating a C#-based console application

Let's get started with a simple C#-based console application. This console application will introduce some basic C# code and get things up and running for the library we are going to build in the next recipe. Our main focus is to get to the C# coding and prepare ourselves for all the excitement we are going to have later. 

Getting ready

To step through this recipe, you will need a running copy of Visual Studio 2017 with the latest version of .NET Framework. If you don't have a copy of Visual Studio 2017, you can download it from https://www.visualstudio.com/.

This will take you to Microsoft's Visual Studio website. Follow the instructions on the site to get a copy of Visual Studio and get things started. 

How it works...

Let us take a quick look at what we did in the previous recipe. In steps 1 to 4, we created a C#-based console application. The skeleton for a console application already comes with Visual Studio as a template. Giving a proper name to your project and a location is a good habit. These things will help you to track down your project easily for future use. In step 5, we just make sure the default console application template works fine and that there are no surprises waiting for us before doing any actual coding. 

In step 6, we created a static method that takes a string parameter and returns a message with that parameter; this is called String Interpolation. It's a new feature introduced in C# 6.0 and can be used instead of the traditional string.format() method. Step 7 uses that method inside the main method. As in a normal console application, Console.ReadLine() will wait till any key is pressed before exiting. Finally, in step 9, we debug the code to check that everything works fine and as expected. 

See also

C# fundamentals  (

Chapter 2

Primitives, Collections, LINQ, and More

)

Creating Windows-based applications using C# (Creating a classic Windows-based application to use the Library—

Chapter 1

,

Back to Basics

)

Creating a C# class library 

In this recipe, we are going to build a simple C# class library. This library will have a simple public method that takes a parameter and returns a string. Also, we will be creating a blank Visual Studio solution and adding the library project. This solution will be used in later recipes. 

Getting ready

Make sure you have installed a flavor of Visual Studio 2017 and its latest updates. At the time of writing, the latest Visual Studio 2017 version is 15.3.5.

How to do it...

Open Visual Studio 2017.

Click

File

|

New

|

Project

and, in the

New Project

template dialog box, select

Visual Studio Solutions

under the 

Other Project Types

node in the left-hand pane, and select

Blank Solution

in the right-hand pane:

In the 

Name:

 textbox, type a name for your application. In this case, type 

Chapter1.Library

. Select a preferred location under the 

Location:

 drop-down list or click the 

Browse...

 button and select a location. Leave the defaults as they are:

Now you have a blank solution. Let's add a C# class library project to the solution. Click

Project

|

Add New

Item...

or you can right-click on the

Chapter1.Library

solution label in the

Solution Explorer

, and select

Add

|

New Project...

.

In the

Add New Project

template dialog box, select

Visual C#

in the left side, pane and select

Class Library (.NET Framework)

in the right-hand pane:

In the

Name:

textbox, type a name for your class library. In this case, type

Chapter1.Library.HelloLib