【问题标题】:How to connect to Azure Service Bus Topic through proxy - C#?如何通过代理连接到 Azure 服务总线主题 - C#?
【发布时间】:2021-07-12 15:47:49
【问题描述】:

我正在开发一个 Web 应用程序,当我们提供命名空间连接字符串和主题名称时,它会显示 Azure 服务总线主题的详细信息。这是我用于此的代码:

//To Check whether the topic is available in Azure Service Bus
private bool IsTopicAvailable(string connectionString, string path)
        {
            try
            {
                var servicebusConnectionString = new ServiceBusConnectionStringBuilder(connectionString)
                {
                    TransportType = Microsoft.ServiceBus.Messaging.TransportType.Amqp
                };
                NamespaceManager namespaceManager = NamespaceManager.CreateFromConnectionString(servicebusConnectionString.ToString());
                if (namespaceManager.TopicExists(path))
                    return true;
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }


//To Get the Topic details
    public TopicDescription GetTopic(string connectionString, string topicName)
        {
            var servicebusConnectionString = new ServiceBusConnectionStringBuilder(connectionString)
            {
                TransportType = Microsoft.ServiceBus.Messaging.TransportType.Amqp
            };
            NamespaceManager namespaceManager = NamespaceManager.CreateFromConnectionString(servicebusConnectionString.ToString());
            var topic = namespaceManager.GetTopic(topicName);
            return topic;
        }

为此,我使用了Microsoft.ServiceBusAssembly。 但是当我通过代理使用该应用程序时,我无法获取主题的详细信息,而是在if (namespaceManager.TopicExists(path)) 行获得The remote server returned an error: (407) Proxy Authentication Required 的异常。但是我已经指定了一个出站规则以允许从 chrome 建立连接。 在少数其他资源中,我看到解决方案是将代理详细信息设置为 WebRequest.DefaultWebProxy,例如:

var proxy = new WebProxy(data.ProxyUri);
proxy.Credentials = new NetworkCredential(data.ProxyUsername, data.ProxyPassword);
WebRequest.DefaultWebProxy = proxy;

但是这种方法覆盖了整个应用程序中使用的默认代理,并且在其他领域也有所体现。但我只想为服务总线主题调用应用代理值。

有人可以帮助我使用 C# 为 Azure 服务总线代理配置代理吗?

【问题讨论】:

    标签: c# azure webrequest azure-servicebus-topics webproxy


    【解决方案1】:

    错误代码 407 表明代理已被使用。只是存在代理身份验证问题。它必须可以使用系统代理设置,并且比您创建的没有绕过列表等的设置更好。

    要继续使用现有代理,您可以尝试以下操作: WebRequest.DefaultWebProxy.Credentials = new NetworkCredential(data.ProxyUsername, data.ProxyPassword);

    谢谢

    【讨论】:

    • 非常感谢您提供的信息。是的,显然这种方法解决了我的问题,但我只关心更改 WebRequest.DefaultWebProxy 的值,因为它对其他特性/功能几乎没有影响。在主题相关功能完成后,我尝试将 WebRequest.DefaultWebProxy.Credentials 重置为其原始值,这在应用程序处于调试模式时工作正常,但在正常模式下运行时,因为函数被称为并行它正在影响还有其他功能。
    猜你喜欢
    • 1970-01-01
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    • 2019-03-05
    • 2018-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多