Skip to content

Unable to connect to Oracle database from Azure Function in VS Code #431

@Mounesh1881

Description

@Mounesh1881

I am trying to connect to an on-premises Oracle database from an Azure Function using Visual Studio Code. I have installed the necessary packages (Oracle.ManagedDataAccess, System.Configuration.ConfigurationManager, System.Security.Permissions) and configured my connection string as follows:
Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=XXXXXXX)(PORT=1648))(CONNECT_DATA=(SERVICE_NAME=XXXXXX)));User ID=XXXXX;Password=XXXXX;

However, I am encountering a connection timeout error (ORA-50000: Connection request timed out). I have verified that the host is up and running, and I can connect to the database using the Oracle SQL Developer extension in VS Code.

Here is the code I am using:

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using Oracle.ManagedDataAccess.Client;
using System.Data;
using System.Threading.Tasks;

namespace Company.Function
{
public class HttpTrigger1
{
private readonly ILogger _logger;

    public HttpTrigger1(ILogger<HttpTrigger1> logger)
    {
        _logger = logger;
    }

    [Function("HttpTrigger1")]
    public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequest req)
    {
        _logger.LogInformation("C# HTTP trigger function processed a request.");

        string connectionString = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=XXXX)(PORT=1648))(CONNECT_DATA=(SERVICE_NAME=XXXX)));User ID=XXXX;Password=XXXX;Validate Connection=true;";

        OracleConnection conn = new OracleConnection(connectionString);
        try
        {
            await conn.OpenAsync();
            _logger.LogInformation("Connected to Oracle database.");

            OracleCommand cmd = new OracleCommand("SELECT * FROM dual", conn);
            OracleDataReader reader = await cmd.ExecuteReaderAsync();
            while (await reader.ReadAsync())
            {
                string result = reader.GetString(0);
                _logger.LogInformation($"Query Result: {result}");
            }
        }
        catch (Exception ex)
        {
            _logger.LogError($"Error connecting to Oracle database: {ex.Message}");
            return new StatusCodeResult(StatusCodes.Status500InternalServerError);
        }
        finally
        {
            await conn.CloseAsync();
        }

        return new OkObjectResult("Oracle database query executed successfully.");
    }
}

}

Error :
[2024-12-24T16:45:45.711Z] Error connecting to Oracle database: ORA-50000: Connection request timed out

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions