Hi guys 👋🏾, so you know that Aspire Aspire Aspire has been the buzz in the DotNet world, and it does simplify setting up your development environment.
I tried bootstrapping an ASP.NET cohosted Orleans project using the Orleans and the PostgreSQL integration. When I tried to add a new migration, errors were thrown because the Orleans integration required some environment variables to be available. Those variables are only available and injected when running your project through the Aspire AppHost project.
The code below is a snippet of the initial state of the code that resorted to errors when I tried to run my entity framework migrations.
builder.Services.AddDbContextPool<AppDbContext>(options =>
{
options.UseNpgsql(builder.Configuration.GetConnectionString("postgresdb")
,npgsqlOptionsAction: handleDbRetry()).UseExceptionProcessor();
});
builder.AddKeyedRedisClient("redis");
builder.UseOrleans(siloBuilder =>
{
siloBuilder.Services.AddPooledDbContextFactory<AppDbContext>(options =>
{
options.UseNpgsql(builder.Configuration.GetConnectionString("postgresdb"),
npgsqlOptionsAction: handleDbRetry());
});
});
When I execute dotnet ef migrations add [name of migration], the error snippet below is thrown
An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Orleans.Runtime.MembershipService.MembershipTableManager Lifetime: Singleton ImplementationType: Orleans.Runtime.MembershipService.MembershipTableManager':
To prevent this, I resorted to always commenting out the builder.UseOrleans block when I want to run migrations. And it worked but I couldn’t see myself always doing this moving forward. So I did some research and discovered that the migration command doesn’t inject any special environment variable when trying to add a migration.
I also discovered that you could add an environment variable which could be analysed before the builder.UseOrleans block runs. This works but I didn’t want to always add extra commands aside from executing the normal dotnet ef migration add command.
Finally, I found a way on Stackoverflow to detect if the dotnet ef migrations add command is executed 💡.
All I needed to do was check if a particular letter combination was available in the executable file name or command line arguments tied to the dotnet ef migrations add process. The Environment.GetCommandLineArgs() method allows you to retrieve this and perform desired checks.
My final code block that does this seamlessly with the same dotnet ef migrations add command can be seen below.
var isMigrations = Environment.GetCommandLineArgs()[0].Contains("ef.dll");
builder.Services.AddDbContextPool<AppDbContext>(options =>
{
options.UseNpgsql(builder.Configuration.GetConnectionString("postgresdb")
,npgsqlOptionsAction: handleDbRetry()).UseExceptionProcessor();
});
builder.AddKeyedRedisClient("redis");
if (!isMigrations)
{
builder.UseOrleans(siloBuilder =>
{
siloBuilder.Services.AddPooledDbContextFactory<AppDbContext>(options =>
{
options.UseNpgsql(builder.Configuration.GetConnectionString("postgresdb"),
npgsqlOptionsAction: handleDbRetry());
});
});
}
I just needed to check if the executable file name contained (“ef.dll”), if it did then that meant I was running a migration and there was no need to configure or set up the Orleans silo.
Thanks for reading. Bye ✌🏾
Ticket- SENDING 0,75406835 BTC. Receive => https://graph.org/GET-BITCOIN-TRANSFER-02-23-2?hs=b540c367fb6e7c063008248de0e5deb6&
ttt2ku