Skip to content
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
Binary file added 2.1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2.2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions TaskHub/Api/Api.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
Expand All @@ -12,7 +12,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.2">
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
28 changes: 28 additions & 0 deletions TaskHub/Api/Middleware/ResponseTimeMiddleware.cs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Diagnostics;

namespace Api.Middleware
{
public class ResponseTimeMiddleware
{
private readonly RequestDelegate _next;

public ResponseTimeMiddleware(RequestDelegate next)
{
_next = next;
}

public async Task InvokeAsync(HttpContext context)
{
var stopwatch = Stopwatch.StartNew();

context.Response.OnStarting(() =>
{
stopwatch.Stop();
context.Response.Headers["X-Response-Time-Ms"] = stopwatch.ElapsedMilliseconds.ToString();
return Task.CompletedTask;
});

await _next(context);
}
}
}
24 changes: 24 additions & 0 deletions TaskHub/Api/Middleware/StudentInfoMiddleware.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace Api.Middleware
{
public class StudentInfoMiddleware
{
private readonly RequestDelegate _next;

public StudentInfoMiddleware(RequestDelegate next)
{
_next = next;
}

public async Task InvokeAsync(HttpContext context)
{
context.Response.OnStarting(() =>
{
context.Response.Headers["X-Student-Name"] = "Tumashova Marina";
context.Response.Headers["X-Student-Group"] = "RI-240912";
return Task.CompletedTask;
});

await _next(context);
}
}
}
18 changes: 18 additions & 0 deletions TaskHub/Api/Services/DisposedService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Api.Services
{
public abstract class DisposedService : IHasInstanceId, IDisposable
{
public Guid InstanceId { get; } = Guid.NewGuid();

protected DisposedService()
{
Console.WriteLine($"{GetType().Name} CREATED:{InstanceId}");
}

public void Dispose()
{
Console.WriteLine($"{GetType().Name} DISPOSED: {InstanceId}");
}
}
}

7 changes: 7 additions & 0 deletions TaskHub/Api/Services/IHasInstanceId.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Api.Services
{
public interface IHasInstanceId
{
Guid InstanceId { get; }
}
}
17 changes: 17 additions & 0 deletions TaskHub/Api/Services/ServiceProviderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Api.Services
{
public static class ServiceProviderExtensions
{
public static void CompareServices<T>(this IServiceProvider provider)
where T : IHasInstanceId
{
var first = provider.GetRequiredService<T>();
var second = provider.GetRequiredService<T>();

Console.WriteLine($"Service: {typeof(T).Name}");
Console.WriteLine($"First: {first.InstanceId}");
Console.WriteLine($"Second: {second.InstanceId}");
Console.WriteLine($"Same instance: {ReferenceEquals(first, second)}");
}
}
}
20 changes: 20 additions & 0 deletions TaskHub/Api/Services/Services.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Api.Services
{
public interface ISingletonService1 : IHasInstanceId { }
public interface ISingletonService2 : IHasInstanceId { }

public interface IScopedService1 : IHasInstanceId { }
public interface IScopedService2 : IHasInstanceId { }

public interface ITransientService1 : IHasInstanceId { }
public interface ITransientService2 : IHasInstanceId { }

public class SingletonService1 : DisposedService, ISingletonService1 { }
public class SingletonService2 : DisposedService, ISingletonService2 { }

public class ScopedService1 : DisposedService, IScopedService1 { }
public class ScopedService2 : DisposedService, IScopedService2 { }

public class TransientService1 : DisposedService, ITransientService1 { }
public class TransientService2 : DisposedService, ITransientService2 { }
}
46 changes: 46 additions & 0 deletions TaskHub/Api/StartUp.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Api.Middleware;
using Api.Services;
using Api.UseCases.Users;
using Api.UseCases.Users.Interfaces;
using Dal;
Expand Down Expand Up @@ -61,6 +63,15 @@ public void ConfigureServices(IServiceCollection services)
Version = "v1"
});
});

services.AddSingleton<ISingletonService1, SingletonService1>();
services.AddSingleton<ISingletonService2, SingletonService2>();

services.AddScoped<IScopedService1, ScopedService1>();
services.AddScoped<IScopedService2, ScopedService2>();

services.AddTransient<ITransientService1, TransientService1>();
services.AddTransient<ITransientService2, TransientService2>();
}

/// <summary>
Expand All @@ -69,6 +80,38 @@ public void ConfigureServices(IServiceCollection services)
/// <param name="app">Построитель приложения</param>
public void Configure(IApplicationBuilder app)
{
using (var scope1 = app.ApplicationServices.CreateScope())
{
var provider = scope1.ServiceProvider;

Console.WriteLine("SCOPE 1");

provider.CompareServices<ISingletonService1>();
provider.CompareServices<ISingletonService2>();

provider.CompareServices<IScopedService1>();
provider.CompareServices<IScopedService2>();

provider.CompareServices<ITransientService1>();
provider.CompareServices<ITransientService2>();
}

using (var scope2 = app.ApplicationServices.CreateScope())
{
var provider = scope2.ServiceProvider;

Console.WriteLine("SCOPE 2");

provider.CompareServices<ISingletonService1>();
provider.CompareServices<ISingletonService2>();

provider.CompareServices<IScopedService1>();
provider.CompareServices<IScopedService2>();

provider.CompareServices<ITransientService1>();
provider.CompareServices<ITransientService2>();
}

if (Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
Expand All @@ -81,6 +124,9 @@ public void Configure(IApplicationBuilder app)

app.UseRouting();

app.UseMiddleware<ResponseTimeMiddleware>();
app.UseMiddleware<StudentInfoMiddleware>();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
Expand Down
6 changes: 3 additions & 3 deletions TaskHub/Dal/Dal.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.2" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions TaskHub/Libs/DatabaseLibrary/DatabaseLibrary.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.2" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0" />
</ItemGroup>

</Project>
6 changes: 3 additions & 3 deletions TaskHub/Libs/LoggingLibrary/LoggingLibrary.csproj
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.2" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Serilog" Version="4.3.0" />
<PackageReference Include="Serilog.AspNetCore" Version="10.0.0" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.0" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions TaskHub/Logic/Logic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.2" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
</ItemGroup>

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down