Skip to content

Commit 5fa317f

Browse files
author
Borja García Rodríguez
committed
net 5 webapps
1 parent 2dab60f commit 5fa317f

26 files changed

Lines changed: 184 additions & 246 deletions

src/Web/WebMVC/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base
1+
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
22
WORKDIR /app
33
EXPOSE 80
44

5-
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
5+
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
66
WORKDIR /src
77

88
# It's important to keep lines from here down to "COPY . ." identical in all Dockerfiles

src/Web/WebMVC/Program.cs

Lines changed: 59 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,77 @@
11
using Microsoft.AspNetCore;
22
using Microsoft.AspNetCore.Hosting;
3+
using Microsoft.eShopOnContainers.WebMVC;
34
using Microsoft.Extensions.Configuration;
45
using Microsoft.Extensions.Logging;
56
using Serilog;
67
using System;
78
using System.IO;
89

9-
namespace Microsoft.eShopOnContainers.WebMVC
10-
{
11-
public class Program
12-
{
13-
public static readonly string Namespace = typeof(Program).Namespace;
14-
public static readonly string AppName = Namespace.Substring(Namespace.LastIndexOf('.', Namespace.LastIndexOf('.') - 1) + 1);
10+
var configuration = GetConfiguration();
1511

16-
public static int Main(string[] args)
17-
{
18-
var configuration = GetConfiguration();
12+
Log.Logger = CreateSerilogLogger(configuration);
1913

20-
Log.Logger = CreateSerilogLogger(configuration);
14+
try
15+
{
16+
Log.Information("Configuring web host ({ApplicationContext})...", Program.AppName);
17+
var host = BuildWebHost(configuration, args);
2118

22-
try
23-
{
24-
Log.Information("Configuring web host ({ApplicationContext})...", AppName);
25-
var host = BuildWebHost(configuration, args);
19+
Log.Information("Starting web host ({ApplicationContext})...", Program.AppName);
20+
host.Run();
2621

27-
Log.Information("Starting web host ({ApplicationContext})...", AppName);
28-
host.Run();
22+
return 0;
23+
}
24+
catch (Exception ex)
25+
{
26+
Log.Fatal(ex, "Program terminated unexpectedly ({ApplicationContext})!", Program.AppName);
27+
return 1;
28+
}
29+
finally
30+
{
31+
Log.CloseAndFlush();
32+
}
33+
34+
IWebHost BuildWebHost(IConfiguration configuration, string[] args) =>
35+
WebHost.CreateDefaultBuilder(args)
36+
.CaptureStartupErrors(false)
37+
.ConfigureAppConfiguration(x => x.AddConfiguration(configuration))
38+
.UseStartup<Startup>()
39+
.UseSerilog()
40+
.Build();
2941

30-
return 0;
31-
}
32-
catch (Exception ex)
33-
{
34-
Log.Fatal(ex, "Program terminated unexpectedly ({ApplicationContext})!", AppName);
35-
return 1;
36-
}
37-
finally
38-
{
39-
Log.CloseAndFlush();
40-
}
41-
}
42+
Serilog.ILogger CreateSerilogLogger(IConfiguration configuration)
43+
{
44+
var seqServerUrl = configuration["Serilog:SeqServerUrl"];
45+
var logstashUrl = configuration["Serilog:LogstashgUrl"];
46+
var cfg = new LoggerConfiguration()
47+
.ReadFrom.Configuration(configuration)
48+
.Enrich.WithProperty("ApplicationContext", Program.AppName)
49+
.Enrich.FromLogContext()
50+
.WriteTo.Console();
51+
if (!string.IsNullOrWhiteSpace(seqServerUrl))
52+
{
53+
cfg.WriteTo.Seq(seqServerUrl);
54+
}
55+
if (!string.IsNullOrWhiteSpace(logstashUrl))
56+
{
57+
cfg.WriteTo.Http(logstashUrl);
58+
}
59+
return cfg.CreateLogger();
60+
}
4261

43-
private static IWebHost BuildWebHost(IConfiguration configuration, string[] args) =>
44-
WebHost.CreateDefaultBuilder(args)
45-
.CaptureStartupErrors(false)
46-
.ConfigureAppConfiguration(x => x.AddConfiguration(configuration))
47-
.UseStartup<Startup>()
48-
.UseSerilog()
49-
.Build();
62+
IConfiguration GetConfiguration()
63+
{
64+
var builder = new ConfigurationBuilder()
65+
.SetBasePath(Directory.GetCurrentDirectory())
66+
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
67+
.AddEnvironmentVariables();
5068

51-
private static Serilog.ILogger CreateSerilogLogger(IConfiguration configuration)
52-
{
53-
var seqServerUrl = configuration["Serilog:SeqServerUrl"];
54-
var logstashUrl = configuration["Serilog:LogstashgUrl"];
55-
var cfg = new LoggerConfiguration()
56-
.ReadFrom.Configuration(configuration)
57-
.Enrich.WithProperty("ApplicationContext", AppName)
58-
.Enrich.FromLogContext()
59-
.WriteTo.Console();
60-
if (!string.IsNullOrWhiteSpace(seqServerUrl)) {
61-
cfg.WriteTo.Seq(seqServerUrl);
62-
}
63-
if (!string.IsNullOrWhiteSpace(logstashUrl)) {
64-
cfg.WriteTo.Http(logstashUrl);
65-
}
66-
return cfg.CreateLogger();
67-
}
69+
return builder.Build();
70+
}
6871

69-
private static IConfiguration GetConfiguration()
70-
{
71-
var builder = new ConfigurationBuilder()
72-
.SetBasePath(Directory.GetCurrentDirectory())
73-
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
74-
.AddEnvironmentVariables();
7572

76-
return builder.Build();
77-
}
78-
}
73+
public class Program
74+
{
75+
private static readonly string _namespace = typeof(Startup).Namespace;
76+
public static readonly string AppName = _namespace.Substring(_namespace.LastIndexOf('.', _namespace.LastIndexOf('.') - 1) + 1);
7977
}

src/Web/WebMVC/Services/ModelDTOs/BasketDTO.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,35 @@
33

44
namespace WebMVC.Services.ModelDTOs
55
{
6-
public class BasketDTO
6+
public record BasketDTO
77
{
88
[Required]
9-
public string City { get; set; }
9+
public string City { get; init; }
1010
[Required]
11-
public string Street { get; set; }
11+
public string Street { get; init; }
1212
[Required]
13-
public string State { get; set; }
13+
public string State { get; init; }
1414
[Required]
15-
public string Country { get; set; }
15+
public string Country { get; init; }
1616

17-
public string ZipCode { get; set; }
17+
public string ZipCode { get; init; }
1818
[Required]
19-
public string CardNumber { get; set; }
19+
public string CardNumber { get; init; }
2020
[Required]
21-
public string CardHolderName { get; set; }
21+
public string CardHolderName { get; init; }
2222

2323
[Required]
24-
public DateTime CardExpiration { get; set; }
24+
public DateTime CardExpiration { get; init; }
2525

2626
[Required]
27-
public string CardSecurityNumber { get; set; }
27+
public string CardSecurityNumber { get; init; }
2828

29-
public int CardTypeId { get; set; }
29+
public int CardTypeId { get; init; }
3030

31-
public string Buyer { get; set; }
31+
public string Buyer { get; init; }
3232

3333
[Required]
34-
public Guid RequestId { get; set; }
34+
public Guid RequestId { get; init; }
3535
}
3636
}
3737

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
namespace WebMVC.Services.ModelDTOs
22
{
3-
public class LocationDTO
3+
public record LocationDTO
44
{
5-
public double Longitude { get; set; }
6-
public double Latitude { get; set; }
5+
public double Longitude { get; init; }
6+
public double Latitude { get; init; }
77
}
88
}

src/Web/WebMVC/Services/ModelDTOs/OrderDTO.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace WebMVC.Services.ModelDTOs
44
{
5-
public class OrderDTO
5+
public record OrderDTO
66
{
77
[Required]
8-
public string OrderNumber { get; set; }
8+
public string OrderNumber { get; init; }
99
}
1010
}

src/Web/WebMVC/Services/ModelDTOs/OrderProcessAction.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
namespace WebMVC.Services.ModelDTOs
22
{
3-
public class OrderProcessAction
3+
public record OrderProcessAction
44
{
5-
public string Code { get; private set; }
6-
public string Name { get; private set; }
5+
public string Code { get; }
6+
public string Name { get; }
77

88
public static OrderProcessAction Ship = new OrderProcessAction(nameof(Ship).ToLowerInvariant(), "Ship");
99

src/Web/WebMVC/Startup.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,16 @@
77
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
88
using Microsoft.AspNetCore.Hosting;
99
using Microsoft.AspNetCore.Http;
10-
using Microsoft.AspNetCore.Mvc;
1110
using Microsoft.eShopOnContainers.WebMVC.Services;
1211
using Microsoft.eShopOnContainers.WebMVC.ViewModels;
1312
using Microsoft.Extensions.Configuration;
1413
using Microsoft.Extensions.DependencyInjection;
1514
using Microsoft.Extensions.Diagnostics.HealthChecks;
1615
using Microsoft.Extensions.Hosting;
17-
using Microsoft.Extensions.Logging;
1816
using Microsoft.IdentityModel.Logging;
1917
using StackExchange.Redis;
2018
using System;
2119
using System.IdentityModel.Tokens.Jwt;
22-
using System.Net.Http;
2320
using WebMVC.Infrastructure;
2421
using WebMVC.Infrastructure.Middlewares;
2522
using WebMVC.Services;

src/Web/WebMVC/ViewModels/Basket.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using System.Threading.Tasks;
54

65
namespace Microsoft.eShopOnContainers.WebMVC.ViewModels
76
{
8-
public class Basket
7+
public record Basket
98
{
109
// Use property initializer syntax.
1110
// While this is often more useful for read only
1211
// auto implemented properties, it can simplify logic
1312
// for read/write properties.
14-
public List<BasketItem> Items { get; set; } = new List<BasketItem>();
15-
public string BuyerId { get; set; }
13+
public List<BasketItem> Items { get; init; } = new List<BasketItem>();
14+
public string BuyerId { get; init; }
1615

1716
public decimal Total()
1817
{
Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
5-
6-
namespace Microsoft.eShopOnContainers.WebMVC.ViewModels
1+
namespace Microsoft.eShopOnContainers.WebMVC.ViewModels
72
{
8-
public class BasketItem
3+
public record BasketItem
94
{
10-
public string Id { get; set; }
11-
public string ProductId { get; set; }
12-
public string ProductName { get; set; }
13-
public decimal UnitPrice { get; set; }
14-
public decimal OldUnitPrice { get; set; }
15-
public int Quantity { get; set; }
16-
public string PictureUrl { get; set; }
5+
public string Id { get; init; }
6+
public string ProductId { get; init; }
7+
public string ProductName { get; init; }
8+
public decimal UnitPrice { get; init; }
9+
public decimal OldUnitPrice { get; init; }
10+
public int Quantity { get; init; }
11+
public string PictureUrl { get; init; }
1712
}
1813
}
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
namespace Microsoft.eShopOnContainers.WebMVC.ViewModels
2-
{
3-
using System.Collections.Generic;
1+
using System.Collections.Generic;
42

5-
public class Campaign
3+
namespace Microsoft.eShopOnContainers.WebMVC.ViewModels
4+
{
5+
public record Campaign
66
{
7-
public int PageIndex { get; set; }
8-
public int PageSize { get; set; }
9-
public int Count { get; set; }
10-
public List<CampaignItem> Data { get; set; }
7+
public int PageIndex { get; init; }
8+
public int PageSize { get; init; }
9+
public int Count { get; init; }
10+
public List<CampaignItem> Data { get; init; }
1111
}
1212
}

0 commit comments

Comments
 (0)