Set up Oracle in the AppHost
This article is the reference for the Aspire Oracle Hosting integration. It enumerates the AppHost APIs — with C# examples — that you use to model Oracle server and database resources in your AppHost project.
If you’re new to the Oracle EF Core integration, start with the Get started with Oracle EF Core integrations guide. For how consuming C# apps connect to the database this page exposes, see Connect to Oracle with EF Core.
Installation
Section titled “Installation”The Aspire Oracle hosting integration models Oracle resources as the following types:
OracleDatabaseServerResourceOracleDatabaseResource
To access these types and APIs for expressing them as resources in your AppHost project, install the 📦 Aspire.Hosting.Oracle NuGet package:
aspire add oracleOr, choose a manual installation approach:
#:package Aspire.Hosting.Oracle@*<PackageReference Include="Aspire.Hosting.Oracle" Version="*" />Add Oracle server and database resources
Section titled “Add Oracle server and database resources”In the AppHost project, call AddOracle to add and return an Oracle server resource builder. Chain a call to AddDatabase on the returned resource builder to add an Oracle database to the server resource:
var builder = DistributedApplication.CreateBuilder(args);
var oracle = builder.AddOracle("oracle") .WithLifetime(ContainerLifetime.Persistent);
var oracledb = oracle.AddDatabase("oracledb");
builder.AddProject<Projects.ExampleProject>() .WithReference(oracledb) .WaitFor(oracledb);
// After adding all resources, run the app...-
When Aspire adds a container image to the AppHost, as shown in the preceding example with the
container-registry.oracle.com/database/freeimage, it creates a new Oracle server instance on your local machine. -
The Oracle database container can be slow to start, so it’s best to use a persistent lifetime with
ContainerLifetime.Persistentto avoid unnecessary restarts. For more information, see Container resource lifetime. -
The Oracle server resource includes default credentials with a randomly generated
passwordusing theCreateDefaultPasswordParametermethod. -
The
WithReferencecall configures a connection in the consuming project named after the referenced database resource —"oracledb"in the preceding example.
Add Oracle resource with password parameter
Section titled “Add Oracle resource with password parameter”The Oracle resource includes default credentials with a random password. Oracle supports configuration-based default passwords using the ORACLE_PWD environment variable. When you want to provide a password explicitly, pass it as a parameter:
var builder = DistributedApplication.CreateBuilder(args);
var password = builder.AddParameter("password", secret: true);
var oracle = builder.AddOracle("oracle", password) .WithLifetime(ContainerLifetime.Persistent);
var oracledb = oracle.AddDatabase("oracledb");
var myService = builder.AddProject<Projects.ExampleProject>() .WithReference(oracledb) .WaitFor(oracledb);The preceding code gets a parameter to pass to the AddOracle API, and internally assigns the parameter to the ORACLE_PWD environment variable of the Oracle container. The password parameter is usually specified as a user secret:
{ "Parameters": { "password": "Non-default-P@ssw0rd" }}For more information, see External parameters.
Add Oracle resource with data volume
Section titled “Add Oracle resource with data volume”To add a data volume to the Oracle resource, call the WithDataVolume method:
var builder = DistributedApplication.CreateBuilder(args);
var oracle = builder.AddOracle("oracle") .WithDataVolume() .WithLifetime(ContainerLifetime.Persistent);
var oracledb = oracle.AddDatabase("oracledb");
builder.AddProject<Projects.ExampleProject>() .WithReference(oracledb) .WaitFor(oracledb);
// After adding all resources, run the app...The data volume is used to persist the Oracle data outside the lifecycle of its container. The data volume is mounted at the /opt/oracle/oradata path in the Oracle container and, when a name parameter isn’t provided, the name is generated at random. For more information on data volumes and details on why they’re preferred over bind mounts, see Docker docs: Volumes.
Add Oracle resource with data bind mount
Section titled “Add Oracle resource with data bind mount”To add a data bind mount to the Oracle resource, call the WithDataBindMount method:
var builder = DistributedApplication.CreateBuilder(args);
var oracle = builder.AddOracle("oracle") .WithDataBindMount(source: @"C:\Oracle\Data");
var oracledb = oracle.AddDatabase("oracledb");
builder.AddProject<Projects.ExampleProject>() .WithReference(oracledb) .WaitFor(oracledb);
// After adding all resources, run the app...Hosting integration health checks
Section titled “Hosting integration health checks”The Oracle hosting integration automatically adds a health check for the Oracle resource. The health check verifies that the Oracle server is running and that a connection can be established to it.
The hosting integration relies on the 📦 AspNetCore.HealthChecks.Oracle NuGet package.