0

I've got an Azure Function running with a Hybrid Connection to an on premise server. Everything works nicely. In the future we will have multiple hybrid connections in multiple locations, and its possible they might have the same server name and port combination.

Is there a way to verify (or debug) the service bus namespace or UserMetadata properties of the hybrid connection being used, before the SQL operation is executed?

Here is run.csx from the function app:

#r "System.Data"


using System.Net;
using System.Collections.Generic;
using System.Configuration;
using Newtonsoft.Json;
using System.Data.SqlClient;
using System.Text;


public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
    log.Info("C# HTTP trigger function processed a request.");
    string connectionString = "Server=MyServerName;Initial Catalog=BillingDB;";
    string queryString = "select top 2 * from Billing";

    List<Billing> bills = new List<Billing>();
    SqlConnection conn = new SqlConnection(connectionString);

    conn.Open();

    /***************
    Here is where I would like to be able to verify/validate 
    the namespace or UserMetadata of the hybrid connection so that I'm sure
    we're connecting to the desired database. "Server" in the connection string
    is not enough in our case.
    ******************/


    SqlCommand DBCmd = new SqlCommand(queryString, conn);
    SqlDataReader myDataReader;
    myDataReader = DBCmd.ExecuteReader();

    while (myDataReader.Read())
        {
            bills.Add(new Billing
            {
                Student_ID = Convert.ToInt32(myDataReader[0]),
                Transaction_Number = Convert.ToInt32(myDataReader[1]),
                Log = myDataReader[2].ToString(),
                Amount_Owed = Convert.ToDecimal(myDataReader[3])
            }
            );
        }

    myDataReader.Close();
    conn.Close();

    var json = JsonConvert.SerializeObject(bills);
    log.Info("json: " + json);

    return req.CreateResponse(HttpStatusCode.OK,json);

}




public class Billing {
    public int Student_ID { get; set; }
    public int Transaction_Number { get; set; }
    public string Log { get; set; }
    public decimal Amount_Owed { get; set; }
}
SteveC
  • 15,808
  • 23
  • 102
  • 173
Greg J
  • 3
  • 1

1 Answers1

0

I was eventually able to solve this by making a GET Request to the Azure Resource Manager REST API (by clicking on Resource Manager in the Application Settings, it provides the url needed to make a callout to as well as the expected response body). In addition to this an Active Directory application needs to be created to acquire a token to access the resources.

https://management.azure.com/subscriptions/{subscriptionid}/resourceGroups/{resourcegroupname}/providers/Microsoft.Web/sites/{functionname}/hybridConnectionRelays?api-version=2016-08-01

This returns a JSON object listing the properties of the Hybrid Connections which are 'Connected' to the individual application/function app.

Greg J
  • 3
  • 1