This document contains changes with .NET Core that have been changed after the book Professional C# 6 and .NET Core 1.0 was published, as well as typos.
Page 15 - .NET Native .NET Native didn't make it to the first release of .NET Core. Building native applications on Linux and Windows will be possible with a later release. Currently it's just possible to compile UWP applications to native code.
Page 19 - Note - check for future implementations for dotnet --template at Reimagine dotnet-new. Preview 2 of the dotnet tools offers the --type option as described in the book. You can use the values Console, Web, Lib, and xunittest.
Page 19 - dotnet repl With preview 2 of the dotnet tools, dotnet repl is not available. It will be available at a later time (probably via tools). See No executable found matching command "dotnet-repl"
Page 20 - The compilationOptions from project.json changed to buildOptions
Page 20 - The framework netstandardapp1.5 has been changed to netcoreapp1.0
Page 20: - using preview 2 of the dotnet tools, dotnet new produces this project.json:
{
"version": "1.0.0-*",
"buildOptions": {
"debugType": "portable",
"emitEntryPoint": true
},
"dependencies": { },
"frameworks": {
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
}
},
"imports": "dnxcore50"
}
}
}
Page 20 - Version 1.4 of NetStandard.Library now includes support for the Universal Windows Platform (UAP). See the .NET Platform Standard. New is version 1.6 which includes support for .NET 4.6.3 and .NET Core 1.0.
Page 21 - on top: netstandardapp1.5 should be netcoreapp1.0:
"frameworks": {
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
}
},
"imports": "dnxcore50"
},
"net46": {}
}
Page 22 - Note, inside the note dotnet compile is mentioned. This should be *dotnet build
Page 23 - the table lists 7.0 for Entity Framework. It should be Core 1.0 instead
Page 33 - Console Application (Package) changed to Console Application (.NET Core). You can find this template within Installed -> Templates -> Visual C# -> .NET Core
Page 33, Figure Update, Figure 2-3
Page 33, Figure Update, Figure 2-4
Page 34 - project.json change: netstandardapp1.5 should be netcoreapp1.0
tags, projectUrl, licenseUrl should not be within the root of project.json, but instead within packOptions. Form the Visual Studio template, these options are no longer added to project.json. I removed them from the RC2 sample files.
Page 35, the version of the build tools is no longer available with the application properties, but you can change it within global.json directly.
Page 35, Figure Update, Figure 2-5
Page 35, persistent files are created by default, this option is no longer available with the Build settings
Page 35, Figure Update, Figure 2-6
Page 36, the runtime version cannot be configured in the Debug tab
Page 36, Figure Update, Figure 2-8
Page 61, Figure Update, Figure 2-9
Page 76 - typo: _ missing with _firstName variable within get accessor
Page 84 - typo: new MySingleton(42) should by new Singleton(42)
Page 92 - Constructors for Structs: you cannot define default constructors for structs. This didn't make it into C# 6.
Page 124, Note at the end of the page: selecting a 32- or 64-bit build cannot be done by the Debug settings of the project properties. Instead, configure the runtimes section within project.json.
Page 125, Figure Update, Figure 5-1
Page 173 - Typo in the first note Within the method GetRectangles an underscore is missing accessing the variable _coll
Page 260 - Text error in the last paragraph. The text should be: ...the event is fired by invoking the method NewCar.
Page 264 - Wrong code in NewCarIsHere, the code should be like this:
public void NewCarIsHere(object sender, CarInfoEventArgs e)
{
WriteLine($"{_name}: car {_e.Car} is new");
}
Page 370, the text before the summary:
Expression<Func<TSource, bool>gt;
should be Expression<Func<TSource, bool>>
Page 400, the API for Bing Search is only available until Dec-2016. Source code is updated for Bing API v5. See Issue #25
Page 435 - You don't need to configure the project properties to produce outputs on build. Outputs on build are done with .NET Core projects.
Page 435, Figure Update, Figure 16-1
Page 435 (end of the page), to select whether to use .NET Core or the .NET Framework version, select the Profile in the Debug settings
Page 436, Figure Update, Figure 16-2
Page 437 - CompilationOptions has been changed to BuildOptions
Page 437/438 - Loading an assembly dynamically has become easier, the DirectoryLoader is no longer needed, use AssemblyLoadContext instead of PlatformServices
private static object GetCalculator()
{
Assembly assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(CalculatorLibPath);
Type type = assembly.GetType(CalculatorTypeName);
return Activator.CreateInstance(type);
}
Page 697, the InitProtection
method changed because of API changes with data protection. Configuration with the AddDataProtection
method instead of the ConfigureDataProtection
method:
public static MySafe InitProtection()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddDataProtection()
.PersistKeysToFileSystem(new DirectoryInfo("."))
.SetDefaultKeyLifetime(TimeSpan.FromDays(20))
.ProtectKeysWithDpapi();
IServiceProvider services = serviceCollection.BuildServiceProvider();
return ActivatorUtilities.CreateInstance<MySafe>(services);
}
Page 714, the WebListener changed
The UrlPrefixes need to be added using the Settings property:
foreach (var prefix in prefixes)
{
listener.Settings.UrlPrefixes.Add(prefix);
WriteLine($"\t{prefix}");
}
Instead of invoking GetContextAsync, invoke AcceptAsync:
RequestContext context = await listener.AcceptAsync()
Page 812, the Load method of the XDocument class supports only loading of local files (the .NET Framework version of this method supports loading files from HTTP servers as well). The sample code for the QueryFeed
method needs to be changed. The HttpClient class is used to make a HTTP GET requests to return a stream. The stream is passed to the XDocument.Load method:
public static async void QueryFeed()
{
try
{
var httpClient = new HttpClient();
using (Stream stream = await httpClient.GetStreamAsync("http://csharp.christiannagel.com/feed/atom/"))
{
XNamespace ns = "http://www.w3.org/2005/Atom";
XDocument doc = XDocument.Load(stream);
WriteLine($"Title: {doc.Root.Element(ns + "title").Value}");
Page 852, the minimum UWP application needs a change to use a custom class derived from the Application class. The Main method is now changed to this:
public static void Main()
{
Application.Start(p => new App());
}
The new App class contains part of the implementation of the previous Main method:
public class App : Application
{
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
var b = new Button
{
Content = "Click Me!"
};
b.Click += (sender, e) =>
{
b.Content = "clicked";
};
Window.Current.Content = b;
Window.Current.Activate();
}
}
Page 1166, the EF context with depdendency injection now needs a constructor with DbContextOptions<TContext>
:
public class BooksContext : DbContext
{
public BooksContext(DbContextOptions<BooksContext> options)
: base(options)
{
}
public DbSet<Book> Books { get; set; }
}
Page 1169, tools section: The Entity Framework Core tools need to be referenced using Microsoft.EntityFrameworkCore.Tools instead of dotnet-ef:
"tools": {
"Microsoft.EntityFrameworkCore.Tools": {
"version": "1.0.0-*",
"imports": "portable-net452+win81"
}
}
Page 1175, scaffold a model
dotnet instead of dnx: dotnet ef dbcontext scaffold instead of dnx ef dbcontext scaffold
Page 1222, Figure Update, Figure 40-1
Page 1223, global.json
The book shows global.json with an SDK version number 1.0.0.0 on page 1223. While .NET Core is already released, the .NET Core tools are not released yet. Currently we have preview 2 using Visual Studio 2015 and preview 3 using Visual Studio 2017 RC. See dot.net for actual downloads of the tools.
Be aware that using the preview of Visual Studio 2017 (Visual Studio 2017 RC) migrates the .NET Core project files to the new csproj format and you cannot open the projects with Visual Studio 2015 afterwards. Backup files are created, so you can go back to the previous state.
Page 1224, Figure Update, Figure 40-3
Page 1227, the Main method changed slightly with UseKestrel (the new Web host), UseIISIntegration (integration when used with IIS), and UseContentRoot (to define the static content for the Web site):
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseIISIntegration()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.Build();
host.Run();
}
Page 1228 Figure Update, Figure 40-5
Page 1229, after Adding Static Content, the text should be: ASP.NET Core 1.0 reduces the overhead as much as possible.
Page 1248, Session State:
Instead of using Microsoft.AspNet.Session, the package Microsoft.AspNetCore.Session needs to be added. Also, the package Microsoft.Extensions.Caching.Memory is needed. With ConfigureServices, you need to invoke the method AddDistributedMemoryCache instead of AddCaching:
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<ISampleService, DefaultSampleService>();
services.AddTransient<HomeController>();
services.AddDistributedMemoryCache();
services.AddSession(options => options.IdleTimeout = TimeSpan.FromMinutes(10));
}
Page 1250, the directory for the configuration is now configured with SetBasePath
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
//...
}
Page 1252 Figure Update, Figure 40-18
Page 1253 - API Change AddUserSecrets
AddUserSecrets() API changed from AddUserSecrets()
to AddUserSecrets<Startup>()
. The generic version is already available with .NET Core 1.0, the non-generic version is now obsolete. See UserSecrets: Obsolete APIs
Page 1272, ToString
instead of ToShortDateString
:
<td>@item.Day.ToString("d")</td>
Page 1284 (bottom of the page), the correct namespace for the tag helper is Microsoft.AspNetCore.Mvc.TagHelpers
Page 1289, the attribute TargetElement
is now HtmlTargetElement
Page 1291, the quotes need to be removed from @addTagHelper
(source file TagHelpers/CustomHelper.cshtml):
@addTagHelper *, MVCSampleApp
Page 1299, 1300 Action result names have been changed, the Http prefix removed: HttpBadRequest, HttpNotFound... renamed to BadRequest, NotFound. Action result naming changes
Page 1312, template name changed: you need to start with an ASP.NET Core Web Application (.NET Core) and select the ASP.NET Core Template "Web API".
Page 1312, Figure Update, Figure 42-1
Page 1317, Figure Update, Figure 42-2
Page 1332, the Swagger part of the implementation of ConfigureServices changed:
services.AddSwaggerGen();
services.ConfigureSwaggerGen(options =>
{
options.SingleApiVersion(new Info
{
Version = "v1",
Title = "Book Chapters",
Description = "A sample for Professional C# 6"
});
options.IgnoreObsoleteActions();
options.IgnoreObsoleteProperties();
options.DescribeAllEnumsAsStrings();
});
Page 1332, the Swagger part of the implementation of Configure changed:
app.UseSwagger();
app.UseSwaggerUi();