diff --git a/DependencyInjection.API/DependencyInjection.API.sln b/DependencyInjection.API/DependencyInjection.API.sln new file mode 100644 index 0000000..58ff94e --- /dev/null +++ b/DependencyInjection.API/DependencyInjection.API.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.12.35707.178 d17.12 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DependencyInjection.API", "DependencyInjection.API\DependencyInjection.API.csproj", "{28D4BBDF-9ACB-40C3-8560-0C4230A396C5}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {28D4BBDF-9ACB-40C3-8560-0C4230A396C5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {28D4BBDF-9ACB-40C3-8560-0C4230A396C5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {28D4BBDF-9ACB-40C3-8560-0C4230A396C5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {28D4BBDF-9ACB-40C3-8560-0C4230A396C5}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/DependencyInjection.API/DependencyInjection.API/Controllers/LifetimeDemoController.cs b/DependencyInjection.API/DependencyInjection.API/Controllers/LifetimeDemoController.cs new file mode 100644 index 0000000..5849f69 --- /dev/null +++ b/DependencyInjection.API/DependencyInjection.API/Controllers/LifetimeDemoController.cs @@ -0,0 +1,37 @@ +using DependencyInjection.API.Interface; +using Microsoft.AspNetCore.Mvc; + +namespace DependencyInjection.API.Controllers +{ + [ApiController] + [Route("[controller]")] + public class LifetimeDemoController : Controller + { + private readonly ITimestampService _timestampService; + private readonly IUserSessionService _userSessionService; + private readonly IGuidGeneratorService _guidGeneratorService; + private IRandomNumberService _randomNumberService; + + + public LifetimeDemoController(ITimestampService timestampService, IUserSessionService userSessionService, IGuidGeneratorService guidGeneratorService, IRandomNumberService randomNumberService) + { + _timestampService = timestampService; + _userSessionService = userSessionService; + _guidGeneratorService = guidGeneratorService; + _randomNumberService = randomNumberService; + } + + [HttpGet] + public IActionResult Get() + { + return Ok(new + { + Timestamp = _timestampService.GetTimeStamp(), + Session = _userSessionService.GetSessionId(), + RandNumber = _randomNumberService.GetRandomNumber(), + Guid = _guidGeneratorService.GenerateGuid(), + } + ); + } + } +} diff --git a/DependencyInjection.API/DependencyInjection.API/Controllers/WeatherForecastController.cs b/DependencyInjection.API/DependencyInjection.API/Controllers/WeatherForecastController.cs new file mode 100644 index 0000000..411f670 --- /dev/null +++ b/DependencyInjection.API/DependencyInjection.API/Controllers/WeatherForecastController.cs @@ -0,0 +1,33 @@ +using Microsoft.AspNetCore.Mvc; + +namespace DependencyInjection.API.Controllers +{ + [ApiController] + [Route("[controller]")] + public class WeatherForecastController : ControllerBase + { + private static readonly string[] Summaries = new[] + { + "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" + }; + + private readonly ILogger _logger; + + public WeatherForecastController(ILogger logger) + { + _logger = logger; + } + + [HttpGet(Name = "GetWeatherForecast")] + public IEnumerable Get() + { + return Enumerable.Range(1, 5).Select(index => new WeatherForecast + { + Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)), + TemperatureC = Random.Shared.Next(-20, 55), + Summary = Summaries[Random.Shared.Next(Summaries.Length)] + }) + .ToArray(); + } + } +} diff --git a/DependencyInjection.API/DependencyInjection.API/DependencyInjection.API.csproj b/DependencyInjection.API/DependencyInjection.API/DependencyInjection.API.csproj new file mode 100644 index 0000000..12f7735 --- /dev/null +++ b/DependencyInjection.API/DependencyInjection.API/DependencyInjection.API.csproj @@ -0,0 +1,13 @@ + + + + net9.0 + enable + enable + + + + + + + diff --git a/DependencyInjection.API/DependencyInjection.API/DependencyInjection.API.http b/DependencyInjection.API/DependencyInjection.API/DependencyInjection.API.http new file mode 100644 index 0000000..778f8d9 --- /dev/null +++ b/DependencyInjection.API/DependencyInjection.API/DependencyInjection.API.http @@ -0,0 +1,6 @@ +@DependencyInjection.API_HostAddress = http://localhost:5029 + +GET {{DependencyInjection.API_HostAddress}}/weatherforecast/ +Accept: application/json + +### diff --git a/DependencyInjection.API/DependencyInjection.API/Interface/IGuidGeneratorService.cs b/DependencyInjection.API/DependencyInjection.API/Interface/IGuidGeneratorService.cs new file mode 100644 index 0000000..2c16a0b --- /dev/null +++ b/DependencyInjection.API/DependencyInjection.API/Interface/IGuidGeneratorService.cs @@ -0,0 +1,7 @@ +namespace DependencyInjection.API.Interface +{ + public interface IGuidGeneratorService + { + Guid GenerateGuid(); + } +} diff --git a/DependencyInjection.API/DependencyInjection.API/Interface/IRandomNumberService.cs b/DependencyInjection.API/DependencyInjection.API/Interface/IRandomNumberService.cs new file mode 100644 index 0000000..a602c77 --- /dev/null +++ b/DependencyInjection.API/DependencyInjection.API/Interface/IRandomNumberService.cs @@ -0,0 +1,7 @@ +namespace DependencyInjection.API.Interface +{ + public interface IRandomNumberService + { + int GetRandomNumber(); + } +} diff --git a/DependencyInjection.API/DependencyInjection.API/Interface/ITimestampService.cs b/DependencyInjection.API/DependencyInjection.API/Interface/ITimestampService.cs new file mode 100644 index 0000000..64cf51f --- /dev/null +++ b/DependencyInjection.API/DependencyInjection.API/Interface/ITimestampService.cs @@ -0,0 +1,7 @@ +namespace DependencyInjection.API.Interface +{ + public interface ITimestampService + { + DateTime GetTimeStamp(); + } +} diff --git a/DependencyInjection.API/DependencyInjection.API/Interface/IUserSessionService.cs b/DependencyInjection.API/DependencyInjection.API/Interface/IUserSessionService.cs new file mode 100644 index 0000000..6081bd1 --- /dev/null +++ b/DependencyInjection.API/DependencyInjection.API/Interface/IUserSessionService.cs @@ -0,0 +1,7 @@ +namespace DependencyInjection.API.Interface +{ + public interface IUserSessionService + { + string GetSessionId(); + } +} diff --git a/DependencyInjection.API/DependencyInjection.API/Program.cs b/DependencyInjection.API/DependencyInjection.API/Program.cs new file mode 100644 index 0000000..71e7ac1 --- /dev/null +++ b/DependencyInjection.API/DependencyInjection.API/Program.cs @@ -0,0 +1,33 @@ +using DependencyInjection.API.Interface; +using DependencyInjection.API.Service; + +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. + +builder.Services.AddControllers(); +// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi +builder.Services.AddOpenApi(); + +builder.Services.AddScoped(); +builder.Services.AddScoped(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); + + + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.MapOpenApi(); +} + +app.UseHttpsRedirection(); + +app.UseAuthorization(); + +app.MapControllers(); + +app.Run(); diff --git a/DependencyInjection.API/DependencyInjection.API/Properties/launchSettings.json b/DependencyInjection.API/DependencyInjection.API/Properties/launchSettings.json new file mode 100644 index 0000000..c9b7048 --- /dev/null +++ b/DependencyInjection.API/DependencyInjection.API/Properties/launchSettings.json @@ -0,0 +1,23 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "applicationUrl": "http://localhost:5029", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "applicationUrl": "https://localhost:7029;http://localhost:5029", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/DependencyInjection.API/DependencyInjection.API/Service/GuidGeneratorService.cs b/DependencyInjection.API/DependencyInjection.API/Service/GuidGeneratorService.cs new file mode 100644 index 0000000..6537895 --- /dev/null +++ b/DependencyInjection.API/DependencyInjection.API/Service/GuidGeneratorService.cs @@ -0,0 +1,10 @@ +using DependencyInjection.API.Interface; + +namespace DependencyInjection.API.Service +{ + public class GuidGeneratorService : IGuidGeneratorService + { + + public Guid GenerateGuid() => Guid.NewGuid(); + } +} diff --git a/DependencyInjection.API/DependencyInjection.API/Service/RandomNumberService.cs b/DependencyInjection.API/DependencyInjection.API/Service/RandomNumberService.cs new file mode 100644 index 0000000..0a4a951 --- /dev/null +++ b/DependencyInjection.API/DependencyInjection.API/Service/RandomNumberService.cs @@ -0,0 +1,18 @@ +using DependencyInjection.API.Interface; + +namespace DependencyInjection.API.Service +{ + public class RandomNumberService : IRandomNumberService + { + private int _randomNumber; + + public RandomNumberService() + { + _randomNumber = new Random().Next(1, 200) + 1; + } + public int GetRandomNumber() + { + return _randomNumber; + } + } +} diff --git a/DependencyInjection.API/DependencyInjection.API/Service/TimestampService.cs b/DependencyInjection.API/DependencyInjection.API/Service/TimestampService.cs new file mode 100644 index 0000000..e6d054c --- /dev/null +++ b/DependencyInjection.API/DependencyInjection.API/Service/TimestampService.cs @@ -0,0 +1,17 @@ +using DependencyInjection.API.Interface; + +namespace DependencyInjection.API.Service +{ + public class TimestampService : ITimestampService + { + private readonly DateTime _timestamp; + public TimestampService() + { + + _timestamp = DateTime.UtcNow; + + } + + public DateTime GetTimeStamp() => _timestamp; + } +} diff --git a/DependencyInjection.API/DependencyInjection.API/Service/UserSessionService.cs b/DependencyInjection.API/DependencyInjection.API/Service/UserSessionService.cs new file mode 100644 index 0000000..11ebd7d --- /dev/null +++ b/DependencyInjection.API/DependencyInjection.API/Service/UserSessionService.cs @@ -0,0 +1,16 @@ +using DependencyInjection.API.Interface; + +namespace DependencyInjection.API.Service +{ + public class UserSessionService : IUserSessionService + { + private readonly string _userId; + + public UserSessionService() + { + _userId = $"Session_{DateTime.UtcNow.Ticks}"; + } + public string GetSessionId() => _userId; + + } +} diff --git a/DependencyInjection.API/DependencyInjection.API/WeatherForecast.cs b/DependencyInjection.API/DependencyInjection.API/WeatherForecast.cs new file mode 100644 index 0000000..cfd64c2 --- /dev/null +++ b/DependencyInjection.API/DependencyInjection.API/WeatherForecast.cs @@ -0,0 +1,13 @@ +namespace DependencyInjection.API +{ + public class WeatherForecast + { + public DateOnly Date { get; set; } + + public int TemperatureC { get; set; } + + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); + + public string? Summary { get; set; } + } +} diff --git a/DependencyInjection.API/DependencyInjection.API/appsettings.Development.json b/DependencyInjection.API/DependencyInjection.API/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/DependencyInjection.API/DependencyInjection.API/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/DependencyInjection.API/DependencyInjection.API/appsettings.json b/DependencyInjection.API/DependencyInjection.API/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/DependencyInjection.API/DependencyInjection.API/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +}