c# - 如何使用 C# 设置 azure 中继混合连接以连接到本地 SQL Server?

标签 c# azure azure-servicebusrelay azure-hybrid-connections

我尝试测试提供的样本 here使用 Azure 混合连接连接到本地 SQL Server。我的服务器上安装了“混合连接管理器”,我可以通过 Azure 应用服务计划和 Azure 功能连接到数据库。

但是,我想使用 C# 代码从驻留在不同网络上的另一台计算机使用相同的混合连接。虽然我在该示例中将正确的连接字符串放入“PortBridgeClientAgent”程序中,但应用程序无法连接到本地 SQL 服务器。在 Azure 外部建立此类连接的正确方法是什么?

这是我的代码:

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);
        }
    }
}

最佳答案

您可以在这个问题中了解为什么这不起作用:Connecting to an Hybrid connection served by the Hybrid connection manager

除了连接到服务总线并请求 SQL 连接之外,应用服务中还发生了更多事情。这意味着目前无法使用它来创建从外部应用服务到您的本地服务器的连接。

关于c# - 如何使用 C# 设置 azure 中继混合连接以连接到本地 SQL Server?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55210781/

相关文章:

azure - 如何在azure功能中浏览或下载/home的内容?

azure - KQL查询获取VMSS中的实例数量

azure - 在 Windows Phone 8 中使用 Azure 服务总线主题

c# - 在 IIS Express 或其他 WebServer 上部署 ASP.NET MVC 应用程序?

c# - UWP BarcodeScanner 预览版 : CaptureElement doesn't show any Preview

c# - 取消订阅子对象 Prop 更改事件

c# - 具有双重身份验证的应用程序 : custom and Active Directory

node.js - npm -g 在 Windows Azure 远程服务器上安装 weinre

azure - CRM 2016 在线和 Azure 服务总线中继 - 50200 : Bad Gateway