Edition 0 Updated to asp. Net core 0



Yüklə 11,82 Mb.
Pdf görüntüsü
səhifə118/288
tarix12.07.2023
ölçüsü11,82 Mb.
#136458
1   ...   114   115   116   117   118   119   120   121   ...   288
NET-Microservices-Architecture-for-Containerized-NET-Applications

Additional resources 

Run the SQL Server Docker image on Linux, Mac, or Windows
https://learn.microsoft.com/sql/linux/sql-server-linux-setup-docker
 

Connect and query SQL Server on Linux with sqlcmd
https://learn.microsoft.com/sql/linux/sql-server-linux-connect-and-query-sqlcmd
 
Seeding with test data on Web application startup 
To add data to the database when the application starts up, you can add code like the following to 
the 
Main
method in the 
Program
class of the Web API project: 
public
static
int
Main
(
string
[] args) 

var
configuration = 
GetConfiguration
(); 
Log.
Logger

CreateSerilogLogger
(configuration); 
try

Log.
Information
(
"Configuring web host ({ApplicationContext})..."
, AppName); 
var
host = 
CreateHostBuilder
(configuration, args); 
Log.
Information
(
"Applying migrations ({ApplicationContext})..."
, AppName); 
host.
MigrateDbContext
((context, services) => 

var
env = services.
GetService
(); 
var
settings = services.
GetService
>(); 
var
logger = services.
GetService
>(); 
new
CatalogContextSeed
() 
.
SeedAsync
(context, env, settings, logger) 
.
Wait
(); 
}) 
.
MigrateDbContext
((_, __) => { }); 
Log.
Information
(
"Starting web host ({ApplicationContext})..."
, AppName); 
host.
Run
(); 
return
0


catch
(Exception ex) 

Log.
Fatal
(ex, 
"Program terminated unexpectedly ({ApplicationContext})!"
, AppName); 
return
1


finally

Log.
CloseAndFlush
(); 




130 
CHAPTER 5 | Designing and Developing Multi-Container and Microservice-Based .NET Applications 
There’s an important caveat when applying 
migrations and seeding a database during container 
startup. Since the database server might not be available for whatever reason, you must handle retries 
while waiting for the server to be available. This retry logic is handled by the 
MigrateDbContext()
extension method, as shown in the following code: 
public
static
IWebHost MigrateDbContext
this
IWebHost host, 
ActionIServiceProvider> seeder) 
where TContext : DbContext 

var
underK8s = host.
IsInKubernetes
(); 
using
(
var
scope = host.
Services
.
CreateScope
()) 

var
services = scope.
ServiceProvider

var
logger = services.
GetRequiredService
>(); 
var
context = services.
GetService
(); 
try

logger.
LogInformation
(
"Migrating database associated with context 
{DbContextName}"

typeof
(TContext).
Name
); 
if
(underK8s) 

InvokeSeeder
(seeder, context, services); 

else

var
retry = Policy.
Handle
() 
.
WaitAndRetry
(
new
TimeSpan[] 

TimeSpan.
FromSeconds
(
3
), 
TimeSpan.
FromSeconds
(
5
), 
TimeSpan.
FromSeconds
(
8
), 
}); 
//if the sql server container is not created on run docker compose this
//migration can't fail for network related exception. The retry options for 
DbContext only
//apply to transient exceptions
// Note that this is NOT applied when running some orchestrators (let the 
orchestrator to recreate the failing service)
retry.
Execute
(() => 
InvokeSeeder
(seeder, context, services)); 

logger.
LogInformation
(
"Migrated database associated with context 
{DbContextName}"


Yüklə 11,82 Mb.

Dostları ilə paylaş:
1   ...   114   115   116   117   118   119   120   121   ...   288




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin