Skip to content

Commit

Permalink
Convert ConfigureShipmentsModule to use IApplicationBuilder.
Browse files Browse the repository at this point in the history
During startup, ApplicationServices was not able to resolve the reference to ShipmentsDbContext. Using IApplicationBuilder and creating a new scope from app.ApplicationServices seems to have to addressed the issue.
  • Loading branch information
BradKnowles authored and oskardudycz committed Aug 3, 2021
1 parent 004323c commit ac84570
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Sample/ECommerce/Shipments/Shipments.Api/Startup.cs
Expand Up @@ -64,7 +64,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
c.RoutePrefix = string.Empty;
});

app.ApplicationServices.ConfigureShipmentsModule();
app.ConfigureShipmentsModule();
}
}
}
6 changes: 4 additions & 2 deletions Sample/ECommerce/Shipments/Shipments/Config.cs
@@ -1,4 +1,5 @@
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -24,13 +25,14 @@ private static IServiceCollection AddEntityFramework(this IServiceCollection ser
options => options.UseNpgsql("name=ConnectionStrings:ShipmentsDatabase"));
}

public static void ConfigureShipmentsModule(this IServiceProvider serviceProvider)
public static void ConfigureShipmentsModule(this IApplicationBuilder app)
{
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Development";

if (environment == "Development")
{
serviceProvider.GetRequiredService<ShipmentsDbContext>().Database.Migrate();
using var serviceScope = app.ApplicationServices.CreateScope();
serviceScope.ServiceProvider.GetRequiredService<ShipmentsDbContext>().Database.Migrate();
}
}
}
Expand Down

0 comments on commit ac84570

Please sign in to comment.