Senpawaii
Senpawaii
CC#
Created by Senpawaii on 6/5/2023 in #help
βœ… Weird values on accessing Database using EF DbContext under throughput load test
Hello! I'm testing an ASP.NET application that uses an SQL Database. To access this database I define a dependency service, that I add to my set of services in the following manner:
services.AddEntityFrameworkSqlServer()
.AddDbContext<DiscountContext>(options => {
options.UseSqlServer(configuration["ConnectionString"],
sqlServerOptionsAction: sqlOptions => {
sqlOptions.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name);
sqlOptions.EnableRetryOnFailure(maxRetryCount: 15, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
});
});
services.AddEntityFrameworkSqlServer()
.AddDbContext<DiscountContext>(options => {
options.UseSqlServer(configuration["ConnectionString"],
sqlServerOptionsAction: sqlOptions => {
sqlOptions.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name);
sqlOptions.EnableRetryOnFailure(maxRetryCount: 15, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
});
});
From my understanding a Db Connection is opened and closed in the scope of each request to my application, that is, each request uses its own connection to the Db. Upon testing my application, by varying the throughput of requests to the application, I notice that for lower throughput values the average access to the database is around 20ms. Under load, this value drops to 6ms/req. The executed queries are the same. I would assume that since each request uses its own connection (from the connection pool), the average latency would increase as I increase the throughput. Am I missing any "special" feature of EF that optimizes the accesses to the Db transparently in the background as the load increases? If this question isn't clear, please feel free to note it, I will try to explain it better πŸ˜„ thanks
12 replies
CC#
Created by Senpawaii on 5/26/2023 in #help
❔ Visual Studio 2022 - Debugging error on trying to launch Performance Profiler
Hey! I'm trying to find the issue behind an error message I'm getting when I try to launch Visual Studio's Performance Profiler over my docker-compose C# project. The error I get is: "Debugging Error: Unable to find the target operating system for the project <my project name>. My docker-compose project launches multiple projects (microservices), and I'm only getting this error for 2 of them. Where does Visual Studio typically search for the target operating system associated with the project? Any ideas on where should I check the target operating system for each project? This is my docker-compose:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" Sdk="Microsoft.Docker.Sdk">
<PropertyGroup Label="Globals">
<ProjectGuid>fea0c318-ffed-4d39-8781-265718ca43dd</ProjectGuid>
<DockerLaunchBrowser>True</DockerLaunchBrowser>
<DockerServiceUrl>http://host.docker.internal:5100</DockerServiceUrl>
<DockerServiceName>webmvc</DockerServiceName>
<DockerTargetOS>Linux</DockerTargetOS>
<ProjectVersion>2.1</ProjectVersion>
<DockerLaunchAction>LaunchBrowser</DockerLaunchAction>
</PropertyGroup>
<ItemGroup>
<None Include=".dockerignore" />
<None Include="docker-compose.override.yml">
<DependentUpon>docker-compose.yml</DependentUpon>
</None>
<None Include="docker-compose.prod.yml">
<DependentUpon>docker-compose.yml</DependentUpon>
</None>
<None Include="docker-compose.yml" />
</ItemGroup>
</Project>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" Sdk="Microsoft.Docker.Sdk">
<PropertyGroup Label="Globals">
<ProjectGuid>fea0c318-ffed-4d39-8781-265718ca43dd</ProjectGuid>
<DockerLaunchBrowser>True</DockerLaunchBrowser>
<DockerServiceUrl>http://host.docker.internal:5100</DockerServiceUrl>
<DockerServiceName>webmvc</DockerServiceName>
<DockerTargetOS>Linux</DockerTargetOS>
<ProjectVersion>2.1</ProjectVersion>
<DockerLaunchAction>LaunchBrowser</DockerLaunchAction>
</PropertyGroup>
<ItemGroup>
<None Include=".dockerignore" />
<None Include="docker-compose.override.yml">
<DependentUpon>docker-compose.yml</DependentUpon>
</None>
<None Include="docker-compose.prod.yml">
<DependentUpon>docker-compose.yml</DependentUpon>
</None>
<None Include="docker-compose.yml" />
</ItemGroup>
</Project>
Thanks! ^~^
3 replies
CC#
Created by Senpawaii on 3/28/2023 in #help
❔ Accessing HTTP Context at DbCommandInterceptor [.NET 7]
Hi! I'm having some issues trying to access the HTTP Context at my DbCommand Interceptor class. What I'm trying to achieve is similar to the one presented in: https://stackoverflow.com/questions/75033870/getting-httpcontext-or-just-user-information-into-dbconnectioninterceptor-in I have tried following advices as present in https://weblogs.asp.net/ricardoperes/accessing-the-httpcontext-from-a-dbcontext , however, when I try to access the HttpContext from the IHttpContextAccessor, I always get a Null object. Ultimately, what I want to do is inject the IHttpContextAccessor class through the DbContext class’ constructor, so that I can provide it to the Interceptor. Can someone lend me some much appreciated advice on how to solve this? This is the implementation of my Interceptor.
public class DemoInterceptor : DbCommandInterceptor {
private readonly IHttpContextAccessor _contextAccessor;
public DemoInterceptor(IHttpContextAccessor contextAccessor) {
_contextAccessor = contextAccessor;
}

[omitted sync override]

public override ValueTask<InterceptionResult<DbDataReader>> ReaderExecutingAsync(
DbCommand command,
CommandEventData eventData,
InterceptionResult<DbDataReader> result,
CancellationToken cancellationToken = default) {

ModifyCommand(command);

return new ValueTask<InterceptionResult<DbDataReader>>(result);
}

private void ModifyCommand(DbCommand command) {
Console.WriteLine(_contextAccessor.HttpContext.Request.Path);
}
}
public class DemoInterceptor : DbCommandInterceptor {
private readonly IHttpContextAccessor _contextAccessor;
public DemoInterceptor(IHttpContextAccessor contextAccessor) {
_contextAccessor = contextAccessor;
}

[omitted sync override]

public override ValueTask<InterceptionResult<DbDataReader>> ReaderExecutingAsync(
DbCommand command,
CommandEventData eventData,
InterceptionResult<DbDataReader> result,
CancellationToken cancellationToken = default) {

ModifyCommand(command);

return new ValueTask<InterceptionResult<DbDataReader>>(result);
}

private void ModifyCommand(DbCommand command) {
Console.WriteLine(_contextAccessor.HttpContext.Request.Path);
}
}
5 replies