Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Improving your C# Skills

You're reading from   Improving your C# Skills Solve modern challenges with functional programming and test-driven techniques of C#

Arrow left icon
Product type Course
Published in Feb 2019
Publisher
ISBN-13 9781838558383
Length 606 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (4):
Arrow left icon
Ovais Mehboob Ahmed Khan Ovais Mehboob Ahmed Khan
Author Profile Icon Ovais Mehboob Ahmed Khan
Ovais Mehboob Ahmed Khan
Clayton Hunt Clayton Hunt
Author Profile Icon Clayton Hunt
Clayton Hunt
John Callaway John Callaway
Author Profile Icon John Callaway
John Callaway
Rod Stephens Rod Stephens
Author Profile Icon Rod Stephens
Rod Stephens
Arrow right icon
View More author details
Toc

Table of Contents (26) Chapters Close

Title Page
Copyright and Credits
About Packt
Contributors
Preface
1. What's New in .NET Core 2 and C# 7? FREE CHAPTER 2. Understanding .NET Core Internals and Measuring Performance 3. Multithreading and Asynchronous Programming in .NET Core 4. Securing and Implementing Resilience in .NET Core Applications 5. Why TDD is Important 6. Setting Up the .NET Test Environment 7. Setting Up a JavaScript Environment 8. What to Know Before Getting Started 9. Tabula Rasa – Approaching an Application with TDD in Mind 10. Testing JavaScript Applications 11. Exploring Integrations 12. Changes in Requirements 13. The Legacy Problem 14. Unraveling a Mess 15. Geometry 16. Randomization 17. Files and Directories 18. Advanced C# and .NET Features 19. Cryptography 1. Other Books You May Enjoy Index

Exploring .NET Core CLI and New Project Templates


Command Line Interface (CLI) is a very popular tool is almost all popular frameworks like Yeoman Generator, Angular, and others. It lends developers access to execute commands to create, build, and run projects, restore packages, and so on.

.NET CLI provides a toolset with a handful commands that can be executed from the command line interface to create .NET Core projects, restore dependencies, and build and run projects. Under the wire, Visual Studio 2015/2017 and Visual Studio Code even uses this tool to perform different options taken by the developers from their IDE; for example, to create a new project using .NET CLI, we can run the following command:

dotnet new

It will list down the available templates and the short name that can be used while creating the project.

Here is the screenshot containing the list of project templates that can be used to create/scaffold projects using .NET Core CLI:

And by running the following command, a new ASP.NET Core MVC application will be created:

dotnet new mvc

The following screenshot shows the provisioning of the new MVC project after running the preceding command. It creates the project in the same directory where the command is running and restores all the dependencies:

To install the .NET Core CLI toolset, there are some native installers available for Windows, Linux, and macOS. These installers can install and set up the .NET CLI tooling on your machine and developers can run the commands from the CLI.

Here is the list of commands with their descriptions that are provided in the .NET Core CLI:

Command

Description

Example

new

Creates a new project based on the template selected

dotnet new razor

restore

Restores all the dependencies defined in the project

dotnet restore

build

Builds the project

dotnet build

run

Runs the source code without any additional compile

dotnet run

publish

Packages the application files into a folder for deployment

dotnet publish

test

Used to execute unit tests

dotnet test

vstest

Executes unit tests from specified files

dotnet vstest [<TEST_FILE_NAMES>]

pack

Packs the code into a NuGet package

dotnet pack

migrate

Migrates .NET Core preview 2 to .NET Core 1.0

dotnet migrate

clean

Cleans the output of the project

dotnet clean

sln

Modifies a .NET Core solution

dotnet sln

help

Displays the list of commands available to execute through .NET CLI

dotnet help

store

Stores the specified assemblies in the runtime package store

dotnet store

 

Here are some of the project level commands that can be used to add a new NuGet package, remove an existing one, list references, and others:

Command

Description

Example

add package

Adds a package reference to the project

dotnet add package Newtonsoft.Json

remove package

Removes a package reference from the project

dotnet remove package Newtonsoft.Json

add reference

Adds a project reference to the project

dotnet add reference chapter1/proj1.csproj

remove reference

Removes the project reference from the project

dotnet remove reference chapter1/proj1.csproj

list reference

List down all the project references in the project

dotnet list reference

 

The following are some common Entity Framework Core commands that can be used to add migration, remove migration, update the database, and so on.

Command

Description

Example

dotnet ef migrations add

Adds a new migration

dotnet ef migrations add Initial

- Initial is the name of migration

dotnet ef migrations list

List available migrations

dotnet ef migrations list

dotnet ef migrations remove

Remove specific migration

dotnet ef migrations remove Initial

- Initial is the name of migration

dotnet ef database update

To update the database to a specified migration

dotnet ef database update Initial

- Initial is the name of migration

dotnet ef database drop

Drops the database

dotnet ef database drop

 

Here are some of the server level commands that can be used to delete the NuGet package from its actual source repository from the machine, add NuGet package into its actual source repository on the machine, and so on:

Command

Description

Example

nuget delete

Deletes the package from the server

dotnet nuget delete Microsoft.AspNetCore.App 2.0

nuget push

Pushes a package to the server and publishes it

dotnet nuget push foo.nupkg

nuget locals

Lists the local NuGet resources

dotnet nuget locals -l all

msbuild

Builds a project and all of its dependencies

dotnet msbuild

dotnet install script

The script to install the .NET CLI tools and the shared runtime

./dotnet-install.ps1 -Channel LTS

 

To run the preceding commands, we can use the tool known as dotnet from the command line and specify the actual command followed by that. When the .NET Core CLI is installed, it is set into the PATH variable in Windows OS and can be accessed from any folder. So, for example, if you are at your project root folder and wanted to restore the dependencies, you can just call the following command and it will restore all the dependencies that have been defined in your project file:

dotnet restore

The preceding command will start restoring the dependencies or project-specific tools, as defined in the project file. The restoration of tools and dependencies are done in parallel:

We can also set the path where packages can be restored by using the --packages argument. However, if this is not specified, it uses the .nuget/packages folder under the system's user folder. For example, the default NuGet folder for Windows OS is {systemdrive}:\Users\{user}\.nuget\packages and /home/{user} for Linux OS, respectively.

You have been reading a chapter from
Improving your C# Skills
Published in: Feb 2019
Publisher:
ISBN-13: 9781838558383
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image