When starting new project does basic configuration matters? .NET Core configuration differs greatly from the standard .NET projects. We don’t have any more web.config file but instead, we use built-in Configuration framework that comes with .NET Core.

Therefore having a good understanding of how to configure your project and how to configure services, which you will use until the end of the development process is a must.

In this post, I will explain to you how to use configuration methods in a Startup class. Also, you will learn how to register different services and how to use extension methods to help you achieve this.

If you want to see all the basic instructions and complete navigation for this series, please click on the following link: Introduction page for this tutorial.

For the previous part click on this link: Part 1 – MySQL, creating database data

This post is divided into several parts:

Creating new project and modifying lunchSettings.json file

After we have finished creating and populating the database, in the previous post, we are going to create Visual Studio project for our server part of the application. Open Visual Studio 2017 and create new ASP.NET Core Web Application with name AccountOwnerServer.

creating-netcore-project .net core configuration

In the next window choose Web API and from the left drop-down list choose .NET Core. Also, from the right drop-down choose ASP.NET Core 2.0. After all that, just click the OK button and your project will load.

choosing-webapi .net core configuration

After the project has been created, we are going to modify lunchSettings.json file. Let’s change the applicationUrl property and launchBrowser property to false, to prevent web browser to start when the project starts.

In Solution Explorer expand Properties and double click on launchSettings.json file. Modify it like this:

Program.cs and Startup.cs explanations

In Program.cs you will find this piece of code:

If you are familiar with .NET Core 1.0, you will find that this code is considerably smaller than it used to be. Also, you could think that many parts are missing like UseKestrel() or UseIISIntegration()… but that is not the situation. Method CreateDefaultBuilder(args) encapsulates all that stuff inside, making this code more readable and quite smaller, but still keeping all the magic present.

You can also notice this part of code: UseStartup<Startup>(). The Startup class is a mandatory for .NET Core, in which we configure embedded or custom services that our application needs. When you open Startup class, you can see the constructor and the two methods which content we will change during application development. In a method ConfigureServices, you will do exactly that, configure your services. Furthermore, in the method Configure you are going to add different middleware components to application’s request pipeline.

All of our configuration code could be written inside ConfigureServices method, but large applications could potentially contain many services. As a result, it could make this method unreadable and hard to maintain. Therefore we will create extension methods for each configuration and place configuration code inside those methods.

Extension methods and CORS configuration

An extension method is inherently the static method. What makes it different from other static methods is that it accepts “this” as the first parameter, and “this” represents the data type of the object which uses that extension method. An extension method must be inside a static class. This kind of methods extends the behavior of types in .NET.

So, let’s start with writing some code.

Create new folder “Extensions” in our project and create a new class inside that folder, with a name ServiceExtensions. We are going to make that class static and inside that class, we will add our service extension methods.

First, we need to configure CORS in our application. CORS (Cross-Origin Resource Sharing) is a mechanism that gives rights to the user to access resources from the server on a different domain. Because we will use Angular as client-side on a different domain than server’s domain, configuring CORS is mandatory. So add this code to the ServiceExtensions class:

We are using the basic settings for adding CORS policy because for this project allowing any origin, method, and header is quite enough. But you can be more restrictive with those settings if you want. Instead of AllowAnyOrigin() method which allows requests from any source, you could use WithOrigins(“http://www.something.com”) which will allow requests just from the specified source. Also, instead of AllowAnyMethod() that allows all HTTP methods,  you can use WithMethods(“POST”, “GET”) that will allow only specified HTTP methods. Furthermore, you can make the same changes for AllowAnyHeader() method by using for example WithHeaders(“accept”, “content-type”) method to allow only specified headers.

IIS configuration

Additionally, you need to configure IIS integration which will help us with IIS deployment. Add the following code to the ServiceExtensions class:

We are not setting up any of the properties inside the options parameter because we are just fine with their default values. For more information about those properties, look at the picture below.

iis-integration-options .net core configuration

In a Startup class we will change ConfigureServices and Configure methods as follows:

In a ConfigureServices method, CORS and IIS configuration have been added. Furthermore, CORS configuration has been added to application’s pipeline inside Configuration method. But as you can see, there is a little more code. Therefore, I will explain it now.

  • app.UseForwardedHeaders will forward proxy headers onto current request. This will help us during the Linux deployment.
  • app.Use(async (context, next) => … will point on the index page in Angular project.
  • app.UseStaticFiles() enables using static files for request. If we don’t set a path of static files, it will use a wwwroot folder in our solution explorer by default.

Conclusion

That is all for the Part 2 of this tutorial. Now you know how to modify lunchSetting.json file, the purpose of Program and Startup classes, how to configure services and finally how to create and use extension methods.

Thank you for reading and check out the next part in which we will create our first service, logger service, and use it for logging our messages.

For any additional question, please do not hesitate to ask by leaving the comment below.