[Fundamentals of dot net core 1] SERVICES OF DOT NET CORE - CORE SERVICES
CONFIGURATION - configuration api(json)
ENVIRONMENTS - (are we on develeopement or production or staging we can program accordingly)
LOGGING - (debugging purpose, tracking issue in our app)
DEPENDENCY INJECTION - ()
ASP.NET Core Fundamentals
Out of the box, ASP.NET Core comes with an interesting set of services that you can make use of in your applications. In this course, you’ll explore some of the core services that are available out of the box in ASP.NET Core and various configuration options. Then you’ll learn about leveraging dependency injection support, defining custom environments, various logging options, how to configure logging, working with data in .NET applications, and the Entity Framework, Finally, you’ll explore various options for seeding a database, querying and manipulating data, working with the front-end user experience, bundling and minifying JavaScript and CSS files, script and link tag helpers, TypeScript, and LESS. This course was originally created by Global Knowledge (GK).
Table of Contents
1. Video: Explore the Core Services (it_sdafgk_02_enus_01)
2. Video: Registering Services with Dependency Injection (it_sdafgk_02_enus_02)
3. Video: Working with Environments (it_sdafgk_02_enus_03)
4. Video: Configuration Support (it_sdafgk_02_enus_04)
5. Video: Exploring the Configuration API (it_sdafgk_02_enus_05)
6. Video: Logging Support (it_sdafgk_02_enus_06)
7. Video: Setting Up Logging (it_sdafgk_02_enus_07)
8. Video: Working with Data (it_sdafgk_02_enus_08)
9. Video: Creating Models and DbContext (it_sdafgk_02_enus_09)
10. Video: Seeding a Database (it_sdafgk_02_enus_10)
11. Video: Mapping Models (it_sdafgk_02_enus_11)
12. Video: Querying Data (it_sdafgk_02_enus_12)
13. Video: Manipulating Data (it_sdafgk_02_enus_13)
14. Video: Enhancing the Front End (it_sdafgk_02_enus_14)
15. Video: Bundling and Minifying Assets (it_sdafgk_02_enus_15)
16. Video: Script and Link Tag Helpers (it_sdafgk_02_enus_16)
17. Video: Introducing TypeScript (it_sdafgk_02_enus_17)
18. Video: Working with TypeScript (it_sdafgk_02_enus_18)
Course HTML Resources
| • | Configure your application |
Out of the box, ASP.NET Core comes with an interesting set of core services that you can make use of in your applications. In this section of the course, we'll explore configuration and logging APIs, as well as the built-in dependency injection container.
Objectives
- Register and inject services
- Set up various configuration sources
- Configure third-party loggers
- Bind to strongly-typed configuration models
| • | Use your own container |
The built-in dependency injection container that comes with ASP.NET Core should be able to handle the needs of most applications. However, there may be situations where you need to plug in another. There are many open source dependency injection containers for .NET that have a much richer feature set, such as StructureMap, Autofac, and Ninject.
Setting up Autofac
Many container libraries today already have NuGet packages that make integration into ASP.NET Core much easier. For instance, to use Autofac you could need to install the Autofac.Extensions.DependencyInjection package from NuGet.
In your Startup.cs file, we're going to add another method called ConfigureContainer. Just like Configure and ConfigureServices, ConfigureContainer will get discovered and executed by convention when your application starts.
public void ConfigureContainer(ContainerBuilder builder) {
builder.RegisterType<FakeDataService>().As<IDataService>();
}
Inside ConfigureContainer, you would register services into the container using their native APIs.
The last thing that needs to happen is for you to register the container early on in the bootstrapping of your application. That happens in the Program.cs file where WebHostBuilder is getting created. Because we're using Autofac for this example, we will make a call to ConfigureServices(services => services.AddAutofac()) off of the WebHostBuilder instance.
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.ConfigureServices(services => services.AddAutofac())
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
With this in place, you will have fully integrated a third party dependency injection container (Autofac) in your application.
| • | Add a third-party logger |
If you would like to make use of a third-party logger in ASP.NET Core, it's actually pretty easy to do. Most .NET loggers today have great support for doing this already. Here, we will set up Serilog. Serilog is a an open source logging library for .NET that is built with powerful structured event data in mind.
Install NuGet packages
To get started with Serilog in your ASP.NET Core projects, install the following NuGet Packages:
- Serilog.Extensions.Logging - Adds support for the new Logging APIs
- Serilog.Sinks.RollingFile - Adds support for rolling files
Setting up the logger
The code below is what we'll use to set up the logger in Startup.cs.
public Startup() {
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo
.RollingFile(Path.Combine(env.ContentRootPath, "logfile-{Hour}.txt"))
.CreateLogger();
}
public void Configure(IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerFactory)
{
loggerFactory.AddSerilog();
}
Serilog exposes a static Log.Logger instance that can be used to configure logging. In the code above, we make use of the LoggerConfiguration to set the log level and also the sink. Sinks are where Serilog sends its log data. In this case, we're sending data to a rolling file sink and providing a template for naming the files. With this setup, the rolling file sink will create a new log file every hour as logs get created. This is just one of the many sinks that are available for Serilog.
The last crucial piece of the configuration happens in the Configure method, where we call the AddSerilog extension method on the loggerFactory. Now, your application will send its logging data to Serilog. I told you it was easy!
| • | Access data with Entity Framework Core |
Every application will need to either consume or produce data in some form. As Software Developers, we are very familiar with leveraging databases to storage the data for our applications.
In the .NET ecosystem, we have a variety of libraries at our disposal that will help us easily incorporate the use of databases in our applications. Entity Framework is one such library provided by Microsoft that allows us to connect with relational database systems such as SQL Server, MySQL, and even PostgreSQL.
Objectives
- Configure Entity Framework Core in an ASP.NET Core application
- Query data from a database using LINQ
- Update database records using the DbContext
- Work with an in-memory Data Provider
| • | Entity Framework Core in memory |
As you're building out your applications with Entity Framework, sometimes you would like to actually run your application logic without having to set up a database. Another scenario may be when you're writing tests for your code and don't want to go through the hassle of constantly having to restore your database to a given state. Luckily, with Entity Framework Core, we have the option for leveraging an in-memory data provider.
Setting up the provider
To get started with the in-memory provider, the first thing you'll need to do is install the Microsoft.EntityFrameworkCore.InMemory NuGet package.
If you recall what our Entity Framework setup code looked like in the Startup's Configure method:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<DemoContext>(options => options.UseSqlite("Data Source=demo.db"));
...
}
This current setup leverages SQLite. To use the in-memory data provider, we can simply replace the call to UseSqlite to UseInMemory.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<DemoContext>(options => options.UseInMemory());
...
}
The rest of your Entity Framework Core code can remain the same. You can run now your application or execute your tests without having to set up a real database.
Disclaimer The in-memory provider was not meant to be used in production scenarios. Also, it does not alleviate the need to run proper integration tests and profiling on your application with a real database.
| • | Manage front end code |
Presenting your clients with a rich user experience can be critical to the success of your web applications. Even if your software services internal clients, having a fast and responsive UI might be key the greatly improving their productivity.
In the front-end web development space, there are so many tools and techniques to choose from. Between built-in support in ASP.NET Core and a few handy extensions in Visual Studio, developers have all the support they need to create beautiful web applications.
Objectives
- Enable bundling and minification
- Compile LESS files
- Compile Typescript to JavaScript
- Work with the link and script Tag Helpers
| • | Final Practice: Enable bundling for front-end assets |
Objective
In this practice exercise, you will learn how to bundle your front-end assets using the Web Essentials extension for Visual Studio.
Instructions
Download Bundle-Practice.zip. The zip file should contain a Visual Studio solution with a single project. The provided project should be compatible with Visual Studio 2017 and later.
1. Install Web Essentials
In Visual Studio, go to the Tools menu and select Extensions and Updates. Install the Web Essentials 2017 extensions for Visual Studio. You will be required to restart your IDE to complete the installation.
2. Create .min files for your CSS and JavaScript files
Open the wwwroot folder and locate the site.css and site.js. These are the files that will be used for bundling and minification. Next, open the bundleconfig.json file. This file contains the configuration for what the input and output files will be.
Right-click on the project, select Bundler & Minifier, and then select Update Bundles. If you inspect your project, you should now see that minified versions of your files have been generated.

3. Complete the exercise
Once you have finished this exercise, and viewed the solution video if necessary, you can proceed to the next activity. If you encounter any problems and want one of our instructors to review your work, you can create a zip file containing the Visual Studio project files and click on the Upload button to submit your completed work for review.



















Comments
Post a Comment