Oracle Hosting integration reference
このコンテンツはまだ日本語訳がありません。
To get started with the Aspire Oracle integrations, follow the Get started with Oracle integrations guide.
This article includes full details about the Aspire Oracle Hosting integration, which models Oracle server and database resources as the OracleDatabaseServerResource and OracleDatabaseResource types. To access these types and APIs, you need to install the Oracle Hosting integration in your AppHost project.
Installation
Section titled “Installation”The Aspire Oracle hosting integration models various 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 oracleAspire CLI は対話的です。求められたら適切な結果を選択してください:
Select an integration to add:
> oracle (Aspire.Hosting.Oracle)> Other results listed as selectable options...#: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 the returned resource builder to AddDatabase, 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/free image, it creates a new Oracle server on your local machine. A reference to your Oracle resource builder (the oracle variable) is used to add a database. The database is named oracledb and then added to the ExampleProject. The Oracle resource includes a random password generated using the CreateDefaultPasswordParameter method.
The WithReference method configures a connection in the ExampleProject named "oracledb". For more information, see Container resource lifecycle.
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 by using the environment variable ORACLE_PWD. When you want to provide a password explicitly, you can provide 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...Using with non-.NET applications
Section titled “Using with non-.NET applications”When you use the WithReference method to pass an Oracle database resource to a non-.NET application (such as Python or JavaScript), Aspire automatically injects environment variables that describe the connection information.
For example, if you reference an Oracle database resource named oracledb:
var oracle = builder.AddOracle("oracle");var oracledb = oracle.AddDatabase("oracledb");
var pythonApp = builder.AddUvicornApp("api", "./api", "main.app") .WithReference(oracledb);The following environment variables are available in the Python application:
ConnectionStrings__oracledb- The connection string for the Oracle databaseORACLEDB_HOST- The hostname of the Oracle serverORACLEDB_PORT- The port numberORACLEDB_USERNAME- The username for authenticationORACLEDB_PASSWORD- The password for authenticationORACLEDB_URI- The connection URI in oracle:// formatORACLEDB_JDBCCONNECTIONSTRING- JDBC-format connection stringORACLEDB_DATABASENAME- The database name
You can access these environment variables in your application code:
import oracledbimport os
# Get connection propertieshost = os.getenv("ORACLEDB_HOST")port = os.getenv("ORACLEDB_PORT")username = os.getenv("ORACLEDB_USERNAME")password = os.getenv("ORACLEDB_PASSWORD")service_name = os.getenv("ORACLEDB_DATABASENAME")
# Create connectionconnection = oracledb.connect( user=username, password=password, dsn=f"{host}:{port}/{service_name}")const oracledb = require('oracledb');
// Get connection propertiesconst host = process.env.ORACLEDB_HOST;const port = process.env.ORACLEDB_PORT;const user = process.env.ORACLEDB_USERNAME;const password = process.env.ORACLEDB_PASSWORD;const serviceName = process.env.ORACLEDB_DATABASENAME;
// Create connectionconst connection = await oracledb.getConnection({ user: user, password: password, connectString: `${host}:${port}/${serviceName}`});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.