-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ffd9613
commit a838b7c
Showing
18 changed files
with
286 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
37 changes: 37 additions & 0 deletions
37
DependencyInjection.API/DependencyInjection.API/Controllers/LifetimeDemoController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(), | ||
} | ||
); | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
DependencyInjection.API/DependencyInjection.API/Controllers/WeatherForecastController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<WeatherForecastController> _logger; | ||
|
||
public WeatherForecastController(ILogger<WeatherForecastController> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
[HttpGet(Name = "GetWeatherForecast")] | ||
public IEnumerable<WeatherForecast> 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(); | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
DependencyInjection.API/DependencyInjection.API/DependencyInjection.API.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net9.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.2" /> | ||
</ItemGroup> | ||
|
||
</Project> |
6 changes: 6 additions & 0 deletions
6
DependencyInjection.API/DependencyInjection.API/DependencyInjection.API.http
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
@DependencyInjection.API_HostAddress = http://localhost:5029 | ||
|
||
GET {{DependencyInjection.API_HostAddress}}/weatherforecast/ | ||
Accept: application/json | ||
|
||
### |
7 changes: 7 additions & 0 deletions
7
DependencyInjection.API/DependencyInjection.API/Interface/IGuidGeneratorService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace DependencyInjection.API.Interface | ||
{ | ||
public interface IGuidGeneratorService | ||
{ | ||
Guid GenerateGuid(); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
DependencyInjection.API/DependencyInjection.API/Interface/IRandomNumberService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace DependencyInjection.API.Interface | ||
{ | ||
public interface IRandomNumberService | ||
{ | ||
int GetRandomNumber(); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
DependencyInjection.API/DependencyInjection.API/Interface/ITimestampService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace DependencyInjection.API.Interface | ||
{ | ||
public interface ITimestampService | ||
{ | ||
DateTime GetTimeStamp(); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
DependencyInjection.API/DependencyInjection.API/Interface/IUserSessionService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace DependencyInjection.API.Interface | ||
{ | ||
public interface IUserSessionService | ||
{ | ||
string GetSessionId(); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
DependencyInjection.API/DependencyInjection.API/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<IRandomNumberService, RandomNumberService>(); | ||
builder.Services.AddScoped<ITimestampService, TimestampService>(); | ||
builder.Services.AddTransient<IUserSessionService, UserSessionService>(); | ||
builder.Services.AddTransient<IGuidGeneratorService, GuidGeneratorService>(); | ||
|
||
|
||
|
||
var app = builder.Build(); | ||
|
||
// Configure the HTTP request pipeline. | ||
if (app.Environment.IsDevelopment()) | ||
{ | ||
app.MapOpenApi(); | ||
} | ||
|
||
app.UseHttpsRedirection(); | ||
|
||
app.UseAuthorization(); | ||
|
||
app.MapControllers(); | ||
|
||
app.Run(); |
23 changes: 23 additions & 0 deletions
23
DependencyInjection.API/DependencyInjection.API/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
DependencyInjection.API/DependencyInjection.API/Service/GuidGeneratorService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using DependencyInjection.API.Interface; | ||
|
||
namespace DependencyInjection.API.Service | ||
{ | ||
public class GuidGeneratorService : IGuidGeneratorService | ||
{ | ||
|
||
public Guid GenerateGuid() => Guid.NewGuid(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
DependencyInjection.API/DependencyInjection.API/Service/RandomNumberService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
DependencyInjection.API/DependencyInjection.API/Service/TimestampService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
DependencyInjection.API/DependencyInjection.API/Service/UserSessionService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
DependencyInjection.API/DependencyInjection.API/WeatherForecast.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; } | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
DependencyInjection.API/DependencyInjection.API/appsettings.Development.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
DependencyInjection.API/DependencyInjection.API/appsettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |