Page 33 - MSDN Magazine, September 2017
P. 33

look at the project’s contents to get a better understanding of how ASP.NETCoreworks.
Dependencies, Sources and Resources
The first file to look at is the project file itself—the .csproj file. This file tells MSBuild how to build the project—which packages it depends on, what version of .NET Core to target, and so forth. If you’ve looked at .csproj files in the past, you’ll notice this project file is much smaller. A lot of effort has gone into making .csproj files shorter and more human-readable. One notable change that helped with shrinking the project file is that source files no longer need to be listed explicitly. Instead, the .NET Core SDK will automatically compile any .cs files next to the project file or in any directories under the .csproj’s directory. Similarly, any .resx files found will be embedded as resources. If you’d prefer to not have all of these .cs files compiled, you can either remove them from the Compile ItemGroup or disable default compile items entirely bysettingtheEnableDefaultCompileItemspropertytofalse.
The .NET version that the app should run against is specified by the <TargetFramework> element. This is set to netcoreapp2.0
in order to take advantage of new ASP.NET Core 2.0 features and themuchlarger.NETCore2.0surfacearea.
The <PackageReference> element in the middle of the .csproj file indicates a NuGet package on which the project depends. You’ll notice in ASP.NET Core 2.0 that you now include just one meta- package by default (Microsoft.AspNetCore.All). This package includes all the other Microsoft.AspNetCore packages in one suc- cinct reference and makes ASP.NET Core 2.0 project files much smaller than project files from previous versions. Additional NuGet dependencies can be added by adding more <PackageReference> elements, by using the Visual Studio NuGet Package management UI, or with the .NET CLI dotnet add command.
If you were using an SPA template (angular, react or reactredux), there would also be custom targets defined in the .csproj file to make sure that Webpack was run when building the project.
Creating and Running the Web Host
Program.csrepresentstheentrypointoftheapplication.ASP.NET Core apps are console applications so, like all console apps, they have a Main method that starts when the app executes.
The contents of the ASP.NET Core 2.0 templates’ Main methods are pretty simple—they create an IWebHost object and call Run on it.
If you previously used ASP.NET Core 1.0, you’ll notice that this file is a bit simpler than the program.cs from those older templates. The reason for this is the new WebHost.CreateDefaultBuilder method. Previously, the ASP.NET Core app’s Main method would configure a WebHostBuilder in order to create the IWebHost instance. This configuration included steps such as specifying a Web server, setting the content root path and enabling IIS integration.
The new CreateDefaultBuilder method simplifies things by cre- ating a ready-to-go IWebHost with most common configuration already done. In addition to spec- ifying the items listed previously, CreateDefaultBuilder also takes care of some setup that was previ- ously handled in Startup.cs (setting up configuration information and registering default logging pro- viders). Because ASP.NET Core is open source, you can see all the details of what CreateDefault- Builder is doing by viewing its source on GitHub (bit.ly/2uR1Dar) if you’re interested.
September 2017 29
Figure 1 New .NET Core Project Templates
Figure 2 A Simple ASP.NET Core App Running msdnmagazine.com




















































































   31   32   33   34   35