From a44b7dfa8aaeb51bec3aaf5d21824b2f45b52aad Mon Sep 17 00:00:00 2001 From: meysam Date: Thu, 21 Mar 2024 17:35:14 +0400 Subject: [PATCH] Upgraded the .NET version to 8.0 Remove the startup.cs file Remove dependencies were reintegrated in the new Program.cs Update the Project file and launchSettings --- ResourceServer/Program.cs | 71 +++++++++--- ResourceServer/Properties/launchSettings.json | 5 +- ResourceServer/ResourceServer.csproj | 10 +- ResourceServer/Startup.cs | 101 ------------------ 4 files changed, 60 insertions(+), 127 deletions(-) delete mode 100644 ResourceServer/Startup.cs diff --git a/ResourceServer/Program.cs b/ResourceServer/Program.cs index d9356fe..205d6a9 100644 --- a/ResourceServer/Program.cs +++ b/ResourceServer/Program.cs @@ -1,20 +1,61 @@ -using Microsoft.AspNetCore; +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Authlete.Api; +using Authlete.Conf; using Microsoft.AspNetCore.Hosting; +// Initialize a WebApplication builder with the provided command-line arguments +var builder = WebApplication.CreateBuilder(args); -namespace ResourceServer +// Configure the WebHost to listen on all network interfaces on port 5001 +builder.WebHost.UseUrls("http://*:5001"); + +// Adds controllers to the services collection. +// This is required to use MVC controller features in .NET 8.0, replacing the older AddMvc. +builder.Services.AddControllers(); + +// Adds a singleton service of type IAuthleteApi. This is a custom setup for integrating Authlete, +// a service for OAuth 2.0 and OpenID Connect 1.0 implementation. +// It uses a specific configuration loader 'AuthletePropertiesConfiguration' +// assumed to load necessary settings for Authlete. +builder.Services.AddSingleton(provider => +{ + // Create a configuration instance + var conf = new AuthletePropertiesConfiguration(); + // Return a new instance of AuthleteApi with the loaded configuration + return new AuthleteApi(conf); +}); + +// Build the WebApplication instance using the configured builder +var app = builder.Build(); + +// Configure the HTTP request pipeline +if (app.Environment.IsDevelopment()) { - public class Program - { - public static void Main(string[] args) - { - BuildWebHost(args).Run(); - } - - public static IWebHost BuildWebHost(string[] args) => - WebHost.CreateDefaultBuilder(args) - .UseUrls("http://*:5001") - .UseStartup() - .Build(); - } + // Use the Developer Exception Page middleware during development + // for detailed exception stack traces + app.UseDeveloperExceptionPage(); } + +// Middleware to serve default files. +// It will search for default files like index.html in the wwwroot folder. +app.UseDefaultFiles(); + +// Middleware to serve static files. Allows serving files from wwwroot folder. +app.UseStaticFiles(); + +// Setup endpoint routing, necessary for ASP.NET Core 3.0 onwards for routing HTTP requests +app.UseRouting(); + +// Configures endpoints for MVC controller actions. +// It maps incoming requests to controller actions. +app.UseEndpoints(endpoints => +{ + // Setup routing to map requests to controller actions + endpoints.MapControllers(); +}); + +// Run the application +app.Run(); + diff --git a/ResourceServer/Properties/launchSettings.json b/ResourceServer/Properties/launchSettings.json index c409fe8..c34591a 100644 --- a/ResourceServer/Properties/launchSettings.json +++ b/ResourceServer/Properties/launchSettings.json @@ -3,7 +3,7 @@ "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { - "applicationUrl": "http://localhost:55556/", + "applicationUrl": "http://localhost:55556", "sslPort": 0 } }, @@ -19,11 +19,10 @@ "ResourceServer": { "commandName": "Project", "launchBrowser": true, - "launchUrl": "api/values", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" }, - "applicationUrl": "http://localhost:5000/" + "applicationUrl": "http://localhost:5001" } } } diff --git a/ResourceServer/ResourceServer.csproj b/ResourceServer/ResourceServer.csproj index 5526d62..841e669 100644 --- a/ResourceServer/ResourceServer.csproj +++ b/ResourceServer/ResourceServer.csproj @@ -1,7 +1,7 @@ - netcoreapp2.0 + net8.0 @@ -10,13 +10,7 @@ - - - - - - - + diff --git a/ResourceServer/Startup.cs b/ResourceServer/Startup.cs deleted file mode 100644 index 476184b..0000000 --- a/ResourceServer/Startup.cs +++ /dev/null @@ -1,101 +0,0 @@ -// -// Copyright (C) 2018 Authlete, Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, -// either express or implied. See the License for the specific -// language governing permissions and limitations under the -// License. -// - - -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Authlete.Api; -using Authlete.Conf; - - -namespace ResourceServer -{ - public class Startup - { - public Startup(IConfiguration configuration) - { - Configuration = configuration; - } - - - public IConfiguration Configuration { get; } - - - public void ConfigureServices(IServiceCollection services) - { - // AddWebApiConventions() is added by WebApiCompatShim. - // Calling the method enables Web API implementations - // to return an HttpResponseMessage instance directly. - services.AddMvc().AddWebApiConventions(); - - // Register an instance of IAuthleteApi so controllers - // can refer to it in order to call Authlete Web APIs. - // IAuthleteApi instance will be passed to constructors - // of controllers by 'Dependency Injection'. Read the - // following article for details. - // - // Introduction to Dependency Injection in ASP.NET Core - // https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection - // - services.AddSingleton(CreateAuthleteApi()); - } - - - public void Configure(IApplicationBuilder app, IHostingEnvironment env) - { - if (env.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - } - - app.UseDefaultFiles(); - app.UseStaticFiles(); - app.UseMvc(); - } - - - static IAuthleteApi CreateAuthleteApi() - { - // Create an instance of IAuthleteConfiguration. - var conf = CreateAuthleteConfiguration(); - - // Create an instance of IAuthleteApi. - return new AuthleteApi(conf); - } - - - static IAuthleteConfiguration CreateAuthleteConfiguration() - { - // Load a configuration file and build an instance of - // IAuthleteConfiguration interface. By default, - // "authlete.properties" will be loaded. The name of a - // configuration file can be specified by the - // environment variable, AUTHLETE_CONFIGURATION_FILE. - // - // AuthetePropertiesConfiguration class has three - // constructors one of which explicitly takes the name - // of a configuration file. - // - // In Authlete.Conf namespace, there exist some other - // implementations of IAuthleteConfiguration interface. - - return new AuthletePropertiesConfiguration(); - } - } -}