Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update project templates use primary constructors #59712

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@
app.UseHttpsRedirection();

#endif

app.UseAntiforgery();

app.MapStaticAssets();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ public static void Main(string[] args)

app.Run();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
using Grpc.Core;
using GrpcService_CSharp;

namespace GrpcService_CSharp.Services;

public class GreeterService : Greeter.GreeterBase
{
private readonly ILogger<GreeterService> _logger;
public GreeterService(ILogger<GreeterService> logger)
{
_logger = logger;
}

public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
{
return Task.FromResult(new HelloReply
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,10 @@ namespace Company.RazorClassLibrary1;
// This class can be registered as scoped DI service and then injected into Blazor
// components for use.

public class ExampleJsInterop : IAsyncDisposable
public class ExampleJsInterop(IJSRuntime jsRuntime) : IAsyncDisposable
{
private readonly Lazy<Task<IJSObjectReference>> moduleTask;

public ExampleJsInterop(IJSRuntime jsRuntime)
{
moduleTask = new (() => jsRuntime.InvokeAsync<IJSObjectReference>(
private readonly Lazy<Task<IJSObjectReference>> moduleTask = new(() => jsRuntime.InvokeAsync<IJSObjectReference>(
"import", "./_content/Company.RazorClassLibrary1/exampleJsInterop.js").AsTask());
}

public async ValueTask<string> Prompt(string message)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@

namespace Company.WebApplication1.Data;

public class ApplicationDbContext : IdentityDbContext
public class ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : IdentityDbContext(options)
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@ public class ErrorModel : PageModel

public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);

private readonly ILogger<ErrorModel> _logger;

public ErrorModel(ILogger<ErrorModel> logger)
{
_logger = logger;
}

public void OnGet()
{
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,18 @@ namespace Company.WebApplication1.Pages;
#if (GenerateApiOrGraph)
[AuthorizeForScopes(ScopeKeySection = "DownstreamApi:Scopes")]
#endif
#if (GenerateApi)
public class IndexModel(IDownstreamApi downstreamApi) : PageModel
#elseif (GenerateGraph)
public class IndexModel(GraphServiceClient graphServiceClient) : PageModel
#else
public class IndexModel : PageModel
#endif
{
private readonly ILogger<IndexModel> _logger;

#if (GenerateApi)
private readonly IDownstreamApi _downstreamApi;

public IndexModel(ILogger<IndexModel> logger,
IDownstreamApi downstreamApi)
{
_logger = logger;
_downstreamApi = downstreamApi;
}

public async Task OnGet()
{
using var response = await _downstreamApi.CallApiForUserAsync("DownstreamApi").ConfigureAwait(false);
using var response = await downstreamApi.CallApiForUserAsync("DownstreamApi").ConfigureAwait(false);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
var apiResult = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
Expand All @@ -45,27 +40,13 @@ public async Task OnGet()
}
}
#elseif (GenerateGraph)
private readonly GraphServiceClient _graphServiceClient;

public IndexModel(ILogger<IndexModel> logger,
GraphServiceClient graphServiceClient)
{
_logger = logger;
_graphServiceClient = graphServiceClient;
}

public async Task OnGet()
{
var user = await _graphServiceClient.Me.GetAsync();
var user = await graphServiceClient.Me.GetAsync();

ViewData["ApiResult"] = user?.DisplayName;
}
#else
public IndexModel(ILogger<IndexModel> logger)
{
_logger = logger;
}

public void OnGet()
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@ namespace Company.WebApplication1.Pages;

public class PrivacyModel : PageModel
{
private readonly ILogger<PrivacyModel> _logger;

public PrivacyModel(ILogger<PrivacyModel> logger)
{
_logger = logger;
}

public void OnGet()
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,19 @@ namespace Company.WebApplication1.Controllers;
#if (OrganizationalAuth)
[Authorize]
#endif
#if (GenerateApi)
public class HomeController(IDownstreamApi downstreamApi) : Controller
#elseif (GenerateGraph)
public class HomeController(GraphServiceClient graphServiceClient) : Controller
#else
public class HomeController : Controller
#endif
{
private readonly ILogger<HomeController> _logger;

#if (GenerateApi)
private readonly IDownstreamApi _downstreamApi;

public HomeController(ILogger<HomeController> logger,
IDownstreamApi downstreamApi)
{
_logger = logger;
_downstreamApi = downstreamApi;
}

[AuthorizeForScopes(ScopeKeySection = "DownstreamApi:Scopes")]
public async Task<IActionResult> Index()
{
using var response = await _downstreamApi.CallApiForUserAsync("DownstreamApi").ConfigureAwait(false);
using var response = await downstreamApi.CallApiForUserAsync("DownstreamApi").ConfigureAwait(false);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
var apiResult = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
Expand All @@ -51,29 +46,15 @@ public async Task<IActionResult> Index()
return View();
}
#elseif (GenerateGraph)
private readonly GraphServiceClient _graphServiceClient;

public HomeController(ILogger<HomeController> logger,
GraphServiceClient graphServiceClient)
{
_logger = logger;
_graphServiceClient = graphServiceClient;
}

[AuthorizeForScopes(ScopeKeySection = "DownstreamApi:Scopes")]
public async Task<IActionResult> Index()
{
var user = await _graphServiceClient.Me.GetAsync();
var user = await graphServiceClient.Me.GetAsync();
ViewData["ApiResult"] = user?.DisplayName;

return View();
}
#else
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}

public IActionResult Index()
{
return View();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@

namespace Company.WebApplication1.Data;

public class ApplicationDbContext : IdentityDbContext
public class ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : IdentityDbContext(options)
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,28 @@ namespace Company.WebApplication1.Controllers;
#if (OrganizationalAuth || IndividualB2CAuth)
[RequiredScope(RequiredScopesConfigurationKey = "AzureAd:Scopes")]
#endif
#if (GenerateApi)
public class WeatherForecastController(IDownstreamApi downstreamApi) : ControllerBase
#elseif (GenerateGraph)
public class WeatherForecastController(GraphServiceClient graphServiceClient) : ControllerBase
#else
public class WeatherForecastController : ControllerBase
#endif
{
private static readonly string[] Summaries =
[
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
];

private readonly ILogger<WeatherForecastController> _logger;

#if (GenerateApi)
private readonly IDownstreamApi _downstreamApi;

public WeatherForecastController(ILogger<WeatherForecastController> logger,
IDownstreamApi downstreamApi)
{
_logger = logger;
_downstreamApi = downstreamApi;
}

#if (EnableOpenAPI)
[HttpGet(Name = "GetWeatherForecast")]
#else
[HttpGet]
#endif
public async Task<IEnumerable<WeatherForecast>> Get()
{
using var response = await _downstreamApi.CallApiForUserAsync("DownstreamApi").ConfigureAwait(false);
using var response = await downstreamApi.CallApiForUserAsync("DownstreamApi").ConfigureAwait(false);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
var apiResult = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
Expand All @@ -74,23 +69,14 @@ public async Task<IEnumerable<WeatherForecast>> Get()
}

#elseif (GenerateGraph)
private readonly GraphServiceClient _graphServiceClient;

public WeatherForecastController(ILogger<WeatherForecastController> logger,
GraphServiceClient graphServiceClient)
{
_logger = logger;
_graphServiceClient = graphServiceClient;
}

#if (EnableOpenAPI)
[HttpGet(Name = "GetWeatherForecast")]
#else
[HttpGet]
#endif
public async Task<IEnumerable<WeatherForecast>> Get()
{
var user = await _graphServiceClient.Me.GetAsync();
var user = await graphServiceClient.Me.GetAsync();

return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Expand All @@ -101,11 +87,6 @@ public async Task<IEnumerable<WeatherForecast>> Get()
.ToArray();
}
#else
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}

#if (EnableOpenAPI)
[HttpGet(Name = "GetWeatherForecast")]
#else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public static void Main(string[] args)

var app = builder.Build();

var sampleTodos = new Todo[] {
var sampleTodos = new Todo[]
{
new(1, "Walk the dog"),
new(2, "Do the dishes", DateOnly.FromDateTime(DateTime.Now)),
new(3, "Do the laundry", DateOnly.FromDateTime(DateTime.Now.AddDays(1))),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

var app = builder.Build();

var sampleTodos = new Todo[] {
var sampleTodos = new Todo[]
{
new(1, "Walk the dog"),
new(2, "Do the dishes", DateOnly.FromDateTime(DateTime.Now)),
new(3, "Do the laundry", DateOnly.FromDateTime(DateTime.Now.AddDays(1))),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ public static void Main(string[] args)
var host = builder.Build();
host.Run();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
namespace Company.Application1;

public class Worker : BackgroundService
public class Worker(ILogger<Worker> logger) : BackgroundService
{
private readonly ILogger<Worker> _logger;

public Worker(ILogger<Worker> logger)
{
_logger = logger;
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
if (_logger.IsEnabled(LogLevel.Information))
if (logger.IsEnabled(LogLevel.Information))
{
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
}
await Task.Delay(1000, stoppingToken);
}
Expand Down
Loading