繁体   English   中英

如何通过代理连接到 Azure 服务总线主题 - C#?

[英]How to connect to Azure Service Bus Topic through proxy - C#?

我正在开发一个 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.ServiceBus程序集。 但是,当我通过代理使用该应用程序时,我无法获取主题的详细信息,而不是获取异常,因为The remote server returned an error: (407) Proxy Authentication Required在线if (namespaceManager.TopicExists(path)) 但是我已经指定了一个出站规则来允许从 chrome 建立的连接。 在其他一些资源中,我看到解决方案是将代理详细信息设置为 WebRequest.DefaultWebProxy,例如:

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

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

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

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

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

谢谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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