10 Days of .Net Aspire: Day 4 — Using Oracle Component in Visual Studio

The.Net Aspire framework is used to develop cloud and production-ready distributed applications. It consists of components to handle cloud-native concerns such as Redis, Postgres etc. The solution structure is divided into the following projects: DotnetAspireChallenge.AppHost, Dotnet aspire.Web, and Dotnet Aspire Oracle.


This content originally appeared on HackerNoon and was authored by Sukhpinder Singh

A Step-by-step guide on how to use the .Net Aspire Oracle component in Visual Studio.

.Net Aspire framework is used to develop cloud and production-ready distributed applications. It consists of components to handle cloud-native concerns such as Redis, Postgres, etc.

Prerequisites

:::tip 10 Day .Net Aspire Challenge

:::

Objectives

Learn how to create a starter project using .Net Aspire with the Oracle EF Core component.

Github Sample: The solution structure is divided into the following projects:-

  • DotnetAspireChallenge.ApiService
  • DotnetAspireChallenge.AppHost
  • DotnetAspireChallenge.ServiceDefaults
  • DotnetAspireChallenge.Web

Getting Started

Step 1: Install the following NuGet package

Install the following Nuget package into the subsequent project “DotnetAspireChallenge.AppHost

dotnet add package Aspire.Hosting.Oracle

\ In the above project, register a server database and consume the Oracle connection using the following code.

    var oracle = builder.AddOracle("oracle")
                        .AddDatabase("oracledb");

    var apiService = builder.AddProject<Projects.DotnetAspireChallenge_ApiService>("apiservice")
        .WithReference(oracle);

\

Step 2: Install another NuGet package

Install the following Nuget package into the subsequent project “DotnetAspireChallenge.ApiService

dotnet add package Aspire.Oracle.EntityFrameworkCore

\ then register the context into the Program.cs file as follows

    builder.AddOracleDatabaseDbContext<OracleDbContext>("oracledb");

\

Step 3: Create a “Customer” class

    public class Customer
    {
        public int Id { get; set; }

        [Required]
        public string Title { get; set; } = string.Empty;

        [Required]
        public string Description { get; set; } = string.Empty;
    }

\

Step 4: Create an extension class

Create an extension class and register a minimal API get method to demonstrate the Oracle EF context usage in the API Service

    public static class AspireOracleExtension
    {
        public static void MapOracleAspireEndpoint(this WebApplication app)
        {
            app.MapGet("/oracle", async (OracleDbContext oracleDbContext) =>
            {
                  await oracleDbContext.Customer.AddAsync(new Customer()
                  {
                      Title = "test@gmail.com",
                      Description = "sukh"
                  });
                  int rows = await oracleDbContext.SaveChangesAsync();
                  if (rows > 0)
                  {
                      return await oracleDbContext.Customer.FirstOrDefaultAsync();
                  }
                  else
                  {
                      return null;
                  }
            });

        }
    }


    internal class OracleDbContext(DbContextOptions options) : DbContext(options)
    {
        public DbSet<Customer> CustomersPgsql => Set<Customer>();
    }

\ and finally, register in the Program.cs file

    app.MapOracleAspireEndpoint();

\

Step 5: Hit the GET endpoint

Finally, navigate to the GET URL shown below in your browser. It will insert the specified customer into the Oracle database, retrieve the most recently inserted row, and display it as a response.

Add additional connection string properties using the JSON syntax

    {
      "Aspire": {
        "Oracle": {
          "EntityFrameworkCore": {
            "DisableHealthChecks": true,
            "DisableTracing": true,
            "DisableMetrics": false,
            "DisableRetry": false,
            "Timeout": 30
          }
        }
      }
    }

Congratulations..!! You’ve successfully integrated the Oracle component into the .Net Aspire project.

\

:::info Github Project: GitHub - ssukhpinder/DotnetAspireChallenge: 10 Day .Net Aspire Challenge

Cheatsheet: Cheat Sheets — .Net

:::

\ \


This content originally appeared on HackerNoon and was authored by Sukhpinder Singh


Print Share Comment Cite Upload Translate Updates
APA

Sukhpinder Singh | Sciencx (2024-09-02T12:09:55+00:00) 10 Days of .Net Aspire: Day 4 — Using Oracle Component in Visual Studio. Retrieved from https://www.scien.cx/2024/09/02/10-days-of-net-aspire-day-4-using-oracle-component-in-visual-studio/

MLA
" » 10 Days of .Net Aspire: Day 4 — Using Oracle Component in Visual Studio." Sukhpinder Singh | Sciencx - Monday September 2, 2024, https://www.scien.cx/2024/09/02/10-days-of-net-aspire-day-4-using-oracle-component-in-visual-studio/
HARVARD
Sukhpinder Singh | Sciencx Monday September 2, 2024 » 10 Days of .Net Aspire: Day 4 — Using Oracle Component in Visual Studio., viewed ,<https://www.scien.cx/2024/09/02/10-days-of-net-aspire-day-4-using-oracle-component-in-visual-studio/>
VANCOUVER
Sukhpinder Singh | Sciencx - » 10 Days of .Net Aspire: Day 4 — Using Oracle Component in Visual Studio. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/02/10-days-of-net-aspire-day-4-using-oracle-component-in-visual-studio/
CHICAGO
" » 10 Days of .Net Aspire: Day 4 — Using Oracle Component in Visual Studio." Sukhpinder Singh | Sciencx - Accessed . https://www.scien.cx/2024/09/02/10-days-of-net-aspire-day-4-using-oracle-component-in-visual-studio/
IEEE
" » 10 Days of .Net Aspire: Day 4 — Using Oracle Component in Visual Studio." Sukhpinder Singh | Sciencx [Online]. Available: https://www.scien.cx/2024/09/02/10-days-of-net-aspire-day-4-using-oracle-component-in-visual-studio/. [Accessed: ]
rf:citation
» 10 Days of .Net Aspire: Day 4 — Using Oracle Component in Visual Studio | Sukhpinder Singh | Sciencx | https://www.scien.cx/2024/09/02/10-days-of-net-aspire-day-4-using-oracle-component-in-visual-studio/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.