简体   繁体   中英

How to setup azure relay hybrid connections using C# to connect to on-premises SQL Server?

I tried to test the sample provided here to use an Azure hybrid connection to connect to an on-premises SQL Server. My server has the "Hybrid Connection Manager" installed on it and I can connect to the database via Azure App Service Plan and an Azure Function.

However, I'd like to use the same hybrid connection from another machine resides on a different network using C# code. While I put the correct connection string in the "PortBridgeClientAgent" program in that example, the application is not able to connect to the on-premises SQL server. What is the correct way to establish this sort of connections outside of Azure?

This is my code:

using Microsoft.Azure.Relay;
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Data.SqlClient;

namespace TestConsoleApp
{
    public static class Program
    {
        private const string RelayNamespace = "{NameSpace}.servicebus.windows.net";
        private const string ConnectionName = "{ConnectionName}";
        private const string KeyName = "RootManageSharedAccessKey";
        private const string Key = "{Key}";

        public static void Main() => RunAsync().GetAwaiter().GetResult();

        private static async Task RunAsync()
        {
            var tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(KeyName, Key);

            var client = new HybridConnectionClient(new Uri($"sb://{RelayNamespace}/{ConnectionName}"), tokenProvider);
            var hybridConnectionStream = await client.CreateConnectionAsync();

            var connectionStringBuilder = new SqlConnectionStringBuilder
            {
                DataSource = "{SqlServerHostName},1433",
                InitialCatalog = "{DatabaseName}",
                IntegratedSecurity = false,
                UserID = "{SqlLogin}",
                Password = "{Password}",
            };

            try
            {
                using (var connection = new SqlConnection(connectionStringBuilder.ToString()))
                {
                    connection.Open();

                    var command1 = new SqlCommand("Query", connection);
                    using (var dataReader = await command1.ExecuteReaderAsync())
                    {
                        var result = dataReader;
                    }
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.ToString());
            }

            Console.ReadLine();
            await hybridConnectionStream.CloseAsync(CancellationToken.None);
        }
    }
}

You can read up on why this does not work in this question: Connecting to an Hybrid connection served by the Hybrid connection manager

There is more going on within app services beyond just connecting to Service Bus and asking for a SQL connection. That means that at this time it is not possible to use this to create a connection from outside App Services to your on premise servers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM