Chapter 2. Building your first .NET Core applications
Listing 2.1. Program.cs from the Hello World application
Listing 2.2. hwapp.csproj from the Hello World console application
Listing 2.3. hwwebapp.csproj package reference to Microsoft.AspNetCore.All
Listing 2.4. Program.cs for an ASP.NET Core web application
Listing 2.5. Trimmed-down Startup.cs file for a Hello World web application
Listing 2.6. Add RuntimeIdentifiers to the hwapp.csproj file
Listing 2.7. Dockerfile for Hello World console application
Listing 2.8. Command to build Hello World console application container image
Listing 2.9. Running the Hello World console application Docker container
Listing 2.10. Modifying Program.cs for Docker container deployment
Listing 2.11. Modifying Dockerfile for Docker container deployment
Chapter 3. How to build with .NET Core
Listing 3.1. csproj showing the TargetFramework property
Listing 3.2. Contents of the CsvReader.cs file
Listing 3.3. Program.cs for CsvParser
Listing 3.4. A test of how order of properties works in MSBuild
Listing 3.5. Target declaration with no AfterTargets attribute set
Listing 3.6. Removing code file from compilation
Listing 3.7. Default definition for the Compile ItemGroup
Listing 3.8. Marvel.csv—contents taken from string in Program.cs
Listing 3.9. Program.cs modified to load Marvel.csv
Listing 3.10. Modifying a csproj to copy a file to the build output
Listing 3.11. Embedding a file in an assembly
Listing 3.12. Modifying Program.cs to read the embedded file
Listing 3.13. Three types of dependencies shown in a project file
Listing 3.14. CsvParser.csproj with multiple target frameworks
Listing 3.15. .NET CLI commands to specify a build property that affects dependencies
Chapter 4. Unit testing with xUnit
Listing 4.1. Contents of Calculator.cs
Listing 4.2. Contents of IRule.cs
Listing 4.3. Contents of WeekendRule.cs
Listing 4.4. Modifying BizDayCalcTests.csproj to reference the BizDayCalc project
Listing 4.5. WeekendRuleTest.cs using Fact
Listing 4.6. WeekendRuleTest.cs using Theory
Listing 4.7. WeekendRuleTest.cs using InlineData with multiple parameters
Listing 4.8. WeekendRuleTests.cs using [MemberData]
Listing 4.9. Contents of HolidayRule.cs
Listing 4.10. Contents of USHolidayTest.cs
Listing 4.11. USHolidayTest with Console.WriteLines
Listing 4.12. CSV parsing code from chapter 3
Listing 4.13. CSV parsing code from chapter 3 modified to close the CSV file
Listing 4.14. CSV parser library unit test using dispose pattern
Listing 4.15. Example class fixture for the business-day calculator
Listing 4.16. Contents of USRegionTest.cs
Listing 4.17. USRegionFixture with a collection fixture
Listing 4.18. USRegionTest with collection fixture
Listing 4.19. Exclude other tests besides USRegionTest.cs
Listing 4.20. USRegionTest using ITestOutputHelper
Chapter 5. Working with relational databases
Listing 5.1. Including the SQLite dependency in the project file
Listing 5.3. Contents of SampleScmDataFixture.cs
Listing 5.4. Modifying UnitTest1.cs to use SampleScmDataFixture
Listing 5.5. Dependencies to add to the SqliteScmTest.csproj
Listing 5.6. Code for the PartType class that will hold the data for each part
Listing 5.7. Code for the ScmContext class that will get data from the data store
Listing 5.8. Modifying WidgetScmDataAccess.csproj to include System.Data.Common
Listing 5.9. Modify UnitTest1.cs to test for the part created in SampleScmDataFixture
Listing 5.10. Code to create tables in SampleScmDataFixture
Listing 5.11. InventoryItem class
Listing 5.12. Adding code to the ScmContext class for inventory items
Listing 5.13. Test method to verify InventoryItem objects are populated correctly
Listing 5.14. Using a LINQ expression to populate the PartType of an InventoryItem
Listing 5.15. Contents of PartCommand class
Listing 5.16. Method to add a part command to the database
Listing 5.17. AddParameter method creates DbParameter objects
Listing 5.18. GetPartCommands method: reads all the PartCommand rows in the table
Listing 5.19. Method to update the part count in the InventoryItem table
Listing 5.20. Method to delete the PartCommand row from the table
Listing 5.21. Creating the Inventory class
Listing 5.22. Updates the inventory counts based on the PartCommands
Listing 5.23. Test if the part commands correctly update the inventory count
Listing 5.24. Method to get a DbTransaction object from the ScmContext object
Listing 5.25. Update the inventory counts based on the PartCommands
Listing 5.26. Update DeletePartCommand and UpdateInventoryItem
Listing 5.28. The CreateOrder method
Listing 5.29. Unit test to verify that CreateOrder is transactional
Listing 5.30. Change project to .NET Core for System.Transactions
Listing 5.31. CreateOrder rewritten to use TransactionScope
Listing 5.33. Code to add in UpdateInventory
Listing 5.34. Reading the Order records from the table
Listing 5.35. OrderPart helper method to hydrate the Order object and create the order
Listing 5.36. Test that UpdateInventory creates an Order when part count is zero
Chapter 6. Simplify data access with object-relational mappers
Listing 6.1. Modify WidgetScmDataAccess.csproj to add Dapper dependency
Listing 6.2. ReadParts from chapter 5 compared with the Dapper version
Listing 6.3. ScmContext class rewritten to use Dapper
Listing 6.4. Creating a row in the Order table with System.Data and Dapper
Listing 6.5. CreateOrder method rewritten to use Dapper
Listing 6.6. Implementation of DataAccessFactory
Listing 6.7. Factory modified to use Type object
Listing 6.8. Factory modified to make the host supply the implementation object
Listing 6.9. Contents of the IScmContext interface
Listing 6.10. ScmDataAccess.csproj contents
Listing 6.11. SqliteDal.csproj contents
Listing 6.12. SqliteScmContext class—a SQLite implementation of IScmContext
Listing 6.13. Adding DI to SqliteScmTest.csproj
Listing 6.14. SampleScmDataFixture class using DI
Listing 6.15. Creating transient objects instead of using a singleton
Listing 6.16. Test SCM data-access layer using Dapper, SQLite, and DI
Listing 6.17. Changing the GetRequiredService extension method call to an equivalent
Listing 6.18. Add Microsoft.Extensions.Configuration reference to test project
Listing 6.19. SampleScmDataFixture using configuration to get the connection string
Listing 6.20. config.json file with the SQLite connection string
Listing 6.21. Copy the configuration file to the output folder
Listing 6.22. InventoryItem.cs modified for use with EF
Listing 6.23. Supplier.cs modified for use with EF
Listing 6.24. EfScmContext—ScmContext modified for use with EF
Listing 6.25. EfScmDataAccess.csproj modified for EF and EF migration tool
Listing 6.26. EfScmDalTest.csproj file modified to support the EF migration tool
Chapter 7. Creating a microservice
Listing 7.1. Adding Markdown Lite as a package reference
Listing 7.2. Test console application using Markdown Lite
Listing 7.3. Modifying the default web template project file
Listing 7.4. Program.cs for the Markdown Lite service starts the web server
Listing 7.5. A Startup.cs file for the Markdown Lite service that sets up MVC
Listing 7.6. MdlController accepts Markdown text and returns HTML
Listing 7.7. Copy test.md to project output, remove Markdown Lite reference
Listing 7.8. Using HttpClient to call a web service, POST a file, and read the response
Listing 7.9. Synchronous Convert method blocks a thread waiting for request content
Listing 7.10. Asynchronous Convert method that doesn’t block the thread
Listing 7.11. Creating IConfigurationRoot object to get the configuration on startup
Listing 7.12. Code to read the Azure storage account information from configuration
Listing 7.13. Example config.json file for the Markdown service
Listing 7.14. config.json for connecting to the Azure emulator
Listing 7.15. GetBlob converts Markdown content from Azure Blob Storage to HTML
Listing 7.16. Signature for the GetBlob method
Listing 7.17. Create an HttpRequestMessage GET BLOB request to Azure storage
Listing 7.18. Create authentication header for Azure storage using shared account key
Listing 7.19. GetBlob using helper methods to create HTTP request
Listing 7.20. Console application that calls Markdown service’s Azure storage operation
Listing 7.21. Operation to upload a BLOB to Markdown service’s BLOB storage account
Listing 7.22. GetAuthHeader method modified to allow content-length specification
Listing 7.23. CreateRequest method changed to allow content-length specification
Listing 7.24. Curl command to test the PutBlob operation
Listing 7.25. C# client code to test the PutBlob operation
Listing 7.26. HttpGet operation can also list containers and BLOBs
Listing 7.27. Modifying helper methods to support listing BLOBs and containers
Listing 7.28. DeleteBlob operation to delete a BLOB from a container
Chapter 8. Debugging
Listing 8.1. launch.json allows you to configure the debugger for your application
Listing 8.2. tasks.json defines how to perform tasks such as build and test
Listing 8.3. An example of a test that you’ll need to debug
Listing 8.4. Modifying catch to wrap the original exception
Listing 8.5. Contents of ScmConsole.csproj
Listing 8.6. Making SqliteDal.csproj a self-contained app on Windows
Listing 8.7. Program.cs for the ScmConsole app will create an order, like in the unit test
Listing 8.8. Output when hitting access violations and then the AggregateException
Listing 8.9. Printing an exception and an inner exception
Listing 8.10. Selected output from the !clrstack command
Listing 8.11. Viewing the Order object (elided)
Listing 8.12. Using the !dumpheap command with a type filter
Chapter 9. Performance and profiling
Listing 9.1. A SimpleWriter class that writes comma-separated values to a stream
Listing 9.2. Modifying the project file for CsvWriterUnitTests
Listing 9.3. Unit test for SimpleWriter
Listing 9.4. nuget.config file with local feed
Listing 9.5. Add reference to xUnit.Performance in the CsvWriterPerfTests project file
Listing 9.6. Program.cs modified to start the xUnit.Performance test harness
Listing 9.7. PerfTests class with a test of the SimpleWriter class
Listing 9.8. Measures the performance of writing to a file instead of memory
Chapter 10. Building world-ready applications
Listing 10.1. TempControl class holds temperature information
Listing 10.2. Program file to simulate writing a log message
Listing 10.3. String resources for the sample application
Listing 10.4. Generate a resource file from strings.restext and embed it
Listing 10.5. File to simulate writing a log message modified to use ResourceManager
Listing 10.6. String resources for the sample application for the Spanish-Mexico culture
Listing 10.7. Modify ResX and EmbeddedResource groups to support multiple files
Listing 10.8. Applying es-MX culture code with ResourceManager
Listing 10.9. Program.cs tests the Controller
Listing 10.10. Controller class
Listing 10.11. Add a reference to Microsoft logging extensions
Listing 10.12. Custom logger implementation for the air conditioner controller
Listing 10.13. RobustLoggerProvider—a custom logger provider implementation
Listing 10.14. Logger extension method for chaining
Listing 10.15. The air conditioner controller’s Telemetry class
Listing 10.16. Updated strings.restext
Listing 10.17. The air conditioner controller’s Telemetry class
Listing 10.18. strings.restext modified to use substitution for the temperature value
Listing 10.19. Setting the culture on the current thread
Listing 10.20. Adding a reference to the localization extension in ACController.csproj
Listing 10.21. Controller class modified to initialize the localizer factory
Listing 10.22. Telemetry class modified to use the localizer
Listing 10.23. Telemetry output using robust logger and string localizer
Listing 10.24. Resource file Telemetry.ar-SA.restext with Arabic version of telemetry logs
Listing 10.25. ACController.csproj modified to get resources from resources subfolder
Listing 10.26. Set the current culture to Arabic to test that the resources work
Listing 10.27. Modifying RobustLogger class’s Log method to use invariant culture
Listing 10.28. ACControllerEventSource is a custom event producer
Listing 10.29. Add singleton instance of ACControllerEventSource
Listing 10.30. Change the LogStatus method in Telemetry to emit events
Listing 10.31. EventSource.restext default resource file
Listing 10.32. EventSource.ar-SA.restext Arabic-localized resource file
Listing 10.33. Adding files to Resx and EmbeddedResource item groups
Listing 10.34. EventListener that writes to the console
Listing 10.35. Using the ConsoleEventListener in the main program
Listing 10.36. Listen for EventSource events, and write them to the logging extension
Listing 10.37. Telemetry class simplified to only use the EventSource
Listing 10.38. Controller class that uses LoggerEventListener
Chapter 11. Multiple frameworks and runtimes
Listing 11.1. Program.cs for your test of the .NET Portability Analyzer
Listing 11.2. New method that implements the suggestion from the Portability Analyzer
Listing 11.3. EventProvider .NET Framework sample
Listing 11.4. Writes events using EventSource
Listing 11.5. Program.cs refactored to use the new EventSource
Listing 11.6. csproj for sample with .NET Framework support
Listing 11.7. Program.cs rewritten to use preprocessor directives
Listing 11.8. csproj with net45 instead of net46
Listing 11.9. Program.cs using NET45 instead of NET46
Listing 11.10. Errors when building for .NET Framework 4.5
Listing 11.11. csproj with framework-specific buildOptions
Listing 11.12. Enumerating multiple runtimes in csproj
Listing 11.13. Interop.WindowsPid.cs—code to get the process ID on Windows
Listing 11.14. Interop.LinuxPid.cs—code to get the process ID on Linux
Listing 11.15. Contents of PidUtility.cs
Listing 11.16. Contents of Program.cs
Listing 11.17. Xplat.csproj modified to indicate support for only two runtimes
Chapter 12. Preparing for release
Listing 12.1. What Newtonsoft.Json.csproj might look like
Listing 12.2. Project Bar references project Foo
Listing 12.3. Contents of the Bar.nuspec file
Listing 12.4. Project Bar packaging its project references
Listing 12.5. Empty nuget.config file
Listing 12.6. nuget.config file with local feed
Listing 12.7. Full name of the System.Core assembly in the .NET Framework
Listing 12.8. Project file with signing options turned on
Listing 12.9. Project file with delay-signing options turned on
Appendix B. xUnit command-line options
3.144.252.201