Skip to content
This repository has been archived by the owner on Jan 3, 2024. It is now read-only.

Commit

Permalink
add workshop for 4.5, rename files and folders
Browse files Browse the repository at this point in the history
  • Loading branch information
paladique committed Jun 27, 2019
1 parent 48dc8da commit ade245c
Show file tree
Hide file tree
Showing 53 changed files with 313 additions and 52 deletions.
2 changes: 1 addition & 1 deletion Labs/3. Startup, Hosting and Middleware.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

## Create a new ASP.NET Core application

1. Open Visual Studio 29019
1. Open Visual Studio 2019
1. Create a new ASP.NET Core application:
1. File -> New -> Project -> ASP.NET Core Web Application (C#)
2. Name the project, set the dropdowns to .NET Core and ASP.NET Core 2.2 and select the Empty template.
Expand Down
2 changes: 1 addition & 1 deletion Labs/4. Dependency Injection & Unit Testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@

1. Run the application. You should see the logging messages from the middleware in the console output.

> **Note:** Completed code for this section is found [/Labs/Code/Lab4](/Labs/Code/Lab4).
> **Note:** Completed code for this section is found [/Labs/Code/Lab4A](/Labs/Code/Lab4A).

# Adding a unit test project

Expand Down
45 changes: 23 additions & 22 deletions Labs/4.5 Building Middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,12 @@

1. Run the app now and set the culture via the query string, e.g. http://localhost/?culture=no

## Move the middleware to its own type
1. Create a new class in the application `RequestCultureMiddleware`
1. Add a constructor that takes a parameter `RequestDelegate next` and assigns it to a private field `private readonly RequestDelegate _next`
1. Add a method `public Task Invoke(HttpContext context)`
1. Copy the code from the inline middleware delegate in the application's `Startup.cs` file to the `Invoke` method you just created and fix the `next` method name
1. Your middleware class should now look something like this:
1. Create a new class calles `RequestCultureMiddleware`
2. Add a constructor that takes a parameter `RequestDelegate next` and assigns it to a private field `private readonly RequestDelegate _next`
3. Add a method `public Task Invoke(HttpContext httpContext)`
4. Copy the code from the inline middleware delegate in the application's `Startup.cs` file to the `Invoke` method you just created and fix the `next` method name
5. Your middleware class should now look something like this:

``` C#
public class RequestCultureMiddleware
Expand All @@ -51,9 +50,9 @@
_next = next;
}

public Task Invoke(HttpContext context)
public Task Invoke(HttpContext httpContext)
{
var cultureQuery = context.Request.Query["culture"];
var cultureQuery = httpContext.Request.Query["culture"];
if (!string.IsNullOrWhiteSpace(cultureQuery))
{
var culture = new CultureInfo(cultureQuery);
Expand All @@ -62,12 +61,12 @@
CultureInfo.CurrentUICulture = culture;
}

return _next(context);
return _next(httpContext);
}
}
```

1. At the bottom of the file, add a class that exposes the middleware via an extension method on `IApplicationBuilder`.
6. At the bottom of the file, add a class that exposes the middleware via an extension method on `IApplicationBuilder`.

```C#
public static class RequestCultureMiddlewareExtensions
Expand All @@ -79,14 +78,14 @@
}
```

1. Back in the application's `Startup.cs` file, delete the inline middleware delegate
1. Add your new middleware class to the HTTP pipeline:
7. Back in the application's `Startup.cs` file, delete the inline middleware delegate
8. Add your new middleware class to the HTTP pipeline:

``` C#
app.UseRequestCulture();
```

1. Run the application again and see that the middleware is now running as a class
9. Run the application again and see that the middleware is now running as a class

## Adding options to middleware
1. Create a class called `RequestCultureOptions.cs` with a `CultureInfo` property called DefaultCulture.
Expand All @@ -97,7 +96,8 @@
public CultureInfo DefaultCulture { get; set; }
}
```
1. Add an overload to `UseRequestCulture` that takes those options and passes them into the `UseMiddleware<RequestCultureMiddleware>` call.

1. In the `RequestCultureMiddleware` class, add an overload to `UseRequestCulture` that takes those options and passes them into the `UseMiddleware<RequestCultureMiddleware>` call.

```C#
public static IApplicationBuilder UseRequestCulture(this IApplicationBuilder builder)
Expand Down Expand Up @@ -194,9 +194,9 @@
}
```

1. Add a new JSON file to the project called `config.json`
1. Add a new key/value pair to the `config.json` file: `"culture": "en-US"`
1. Change the code in `Startup.cs` to set the default culture using the configuration system:
2. Right click on the project -> Add -> New Item -> Data and add a JSON file to the project called `config.json`
3. Add a new key/value pair to the `config.json` file: `"culture": "en-US"`
4. Change the code in `Startup.cs` to set the default culture using the configuration system:

``` C#
app.UseRequestCulture(new RequestCultureOptions
Expand All @@ -205,11 +205,10 @@
});
```

1. Run the application and the default culture should be set from the configuration file.
1. Change the culture in the `config.json` file and refresh the page (without changing any other code). Note that the message hasn't changed as the configuration was only read when the application was started.
1. Go back to Visual Studio and touch and save the `Startup.cs` file to force the process to restart
1. Go back to the browser now and refresh the page and it should show the updated message

5. Run the application and the default culture should be set from the configuration file.
6. Change the culture in the `config.json` file and refresh the page (without changing any other code). Note that the message hasn't changed as the configuration was only read when the application was started.
7. Go back to Visual Studio and touch and save the `Startup.cs` file to force the process to restart
8. Go back to the browser now and refresh the page and it should show the updated message

## Flowing options from dependency injection system to middleware

Expand Down Expand Up @@ -258,3 +257,5 @@
```

1. Run the application and see that options are now being configured from the dependency injection system.

> **Note:** Completed code for this section is found [/Labs/Code/Lab4B](/Labs/Code/Lab4B).
32 changes: 10 additions & 22 deletions Labs/Code/Lab4A/Lab4.sln
Original file line number Diff line number Diff line change
@@ -1,42 +1,30 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26020.0
# Visual Studio Version 16
VisualStudioVersion = 16.0.28917.182
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{14917E84-0349-44AA-9E50-EECB3DAB9976}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{665FCCEE-47C5-4936-ADA0-F7E3A0430E87}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lab4", "src\Lab4\Lab4.csproj", "{3B55DFDA-79ED-4938-903F-5ED05C76ACB7}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lab4A", "src\Lab4A\Lab4A.csproj", "{091CD54A-0190-4CFC-A20E-BABD61799536}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{3B55DFDA-79ED-4938-903F-5ED05C76ACB7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3B55DFDA-79ED-4938-903F-5ED05C76ACB7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3B55DFDA-79ED-4938-903F-5ED05C76ACB7}.Debug|x64.ActiveCfg = Debug|Any CPU
{3B55DFDA-79ED-4938-903F-5ED05C76ACB7}.Debug|x64.Build.0 = Debug|Any CPU
{3B55DFDA-79ED-4938-903F-5ED05C76ACB7}.Debug|x86.ActiveCfg = Debug|Any CPU
{3B55DFDA-79ED-4938-903F-5ED05C76ACB7}.Debug|x86.Build.0 = Debug|Any CPU
{3B55DFDA-79ED-4938-903F-5ED05C76ACB7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3B55DFDA-79ED-4938-903F-5ED05C76ACB7}.Release|Any CPU.Build.0 = Release|Any CPU
{3B55DFDA-79ED-4938-903F-5ED05C76ACB7}.Release|x64.ActiveCfg = Release|Any CPU
{3B55DFDA-79ED-4938-903F-5ED05C76ACB7}.Release|x64.Build.0 = Release|Any CPU
{3B55DFDA-79ED-4938-903F-5ED05C76ACB7}.Release|x86.ActiveCfg = Release|Any CPU
{3B55DFDA-79ED-4938-903F-5ED05C76ACB7}.Release|x86.Build.0 = Release|Any CPU
{091CD54A-0190-4CFC-A20E-BABD61799536}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{091CD54A-0190-4CFC-A20E-BABD61799536}.Debug|Any CPU.Build.0 = Debug|Any CPU
{091CD54A-0190-4CFC-A20E-BABD61799536}.Release|Any CPU.ActiveCfg = Release|Any CPU
{091CD54A-0190-4CFC-A20E-BABD61799536}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{3B55DFDA-79ED-4938-903F-5ED05C76ACB7} = {14917E84-0349-44AA-9E50-EECB3DAB9976}
{091CD54A-0190-4CFC-A20E-BABD61799536} = {665FCCEE-47C5-4936-ADA0-F7E3A0430E87}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {BBA096C2-42FC-405B-BEC2-CB4972578542}
SolutionGuid = {A48721F1-1019-474D-A0B3-01473772CC2D}
EndGlobalSection
EndGlobal
9 changes: 6 additions & 3 deletions Labs/Code/Lab4/Lab4.sln → Labs/Code/Lab4A/Lab4A.sln
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26020.0
# Visual Studio Version 16
VisualStudioVersion = 16.0.28917.182
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{665FCCEE-47C5-4936-ADA0-F7E3A0430E87}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lab4", "src\Lab4\Lab4.csproj", "{091CD54A-0190-4CFC-A20E-BABD61799536}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lab4A", "src\Lab4A\Lab4A.csproj", "{091CD54A-0190-4CFC-A20E-BABD61799536}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -24,4 +24,7 @@ Global
GlobalSection(NestedProjects) = preSolution
{091CD54A-0190-4CFC-A20E-BABD61799536} = {665FCCEE-47C5-4936-ADA0-F7E3A0430E87}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A48721F1-1019-474D-A0B3-01473772CC2D}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;

namespace Lab4
namespace Lab4A
{
public class Program
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;

namespace Lab4
namespace Lab4A
{
public class Startup
{
Expand Down
25 changes: 25 additions & 0 deletions Labs/Code/Lab4B/Lab4B.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.28917.182
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lab4B", "src\Lab4B.csproj", "{1B524C9F-B407-4B89-A53A-B89BE5B371FE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{1B524C9F-B407-4B89-A53A-B89BE5B371FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1B524C9F-B407-4B89-A53A-B89BE5B371FE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1B524C9F-B407-4B89-A53A-B89BE5B371FE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1B524C9F-B407-4B89-A53A-B89BE5B371FE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8FF55818-A41B-4C88-BF8C-64D74EECEA0B}
EndGlobalSection
EndGlobal
13 changes: 13 additions & 0 deletions Labs/Code/Lab4B/src/Lab4B.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
</ItemGroup>

</Project>
22 changes: 22 additions & 0 deletions Labs/Code/Lab4B/src/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;

namespace Lab4B
{
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((builderContext, config) =>
{
config.AddJsonFile("config.json");
})
.UseStartup<Startup>();
}
}
27 changes: 27 additions & 0 deletions Labs/Code/Lab4B/src/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:58029",
"sslPort": 44334
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"WebApplication2": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
59 changes: 59 additions & 0 deletions Labs/Code/Lab4B/src/RequestCultureMiddleware.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;

namespace Lab4B
{
public class RequestCultureMiddleware
{
private readonly RequestDelegate _next;
private readonly RequestCultureOptions _options;

public RequestCultureMiddleware(RequestDelegate next, IOptions<RequestCultureOptions> options)
{
_next = next;
_options = options.Value;
}

public Task Invoke(HttpContext httpContext)
{
CultureInfo requestCulture = null;

var cultureQuery = httpContext.Request.Query["culture"];
if (!string.IsNullOrWhiteSpace(cultureQuery))
{
requestCulture = new CultureInfo(cultureQuery);
}
else
{
requestCulture = _options.DefaultCulture;
}

if (requestCulture != null)
{
CultureInfo.CurrentCulture = requestCulture;
CultureInfo.CurrentUICulture = requestCulture;
}

return _next(httpContext);
}
}

public static class RequestCultureMiddlewareExtensions
{
public static IApplicationBuilder UseRequestCulture(this IApplicationBuilder builder)
{
return builder.UseMiddleware<RequestCultureMiddleware>();
}

public static IApplicationBuilder UseRequestCulture(this IApplicationBuilder builder, RequestCultureOptions options)
{
return builder.UseMiddleware<RequestCultureMiddleware>(Options.Create(options));
}
}
}
16 changes: 16 additions & 0 deletions Labs/Code/Lab4B/src/RequestCultureOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.AspNetCore.Builder;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;

namespace Lab4B
{
public class RequestCultureOptions
{
public CultureInfo DefaultCulture { get; set; }

}

}
Loading

0 comments on commit ade245c

Please sign in to comment.