如何通过代理连接到 Azure 服务总线主题 - C#? [英] How to connect to Azure Service Bus Topic through proxy - C#?

查看:31
本文介绍了如何通过代理连接到 Azure 服务总线主题 - C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on a web application that displays the Azure Service Bus Topics details when we provide the namespace connection string and topic name. Here is the code I used for this:

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

For this purpose, I used Microsoft.ServiceBus Assembly. But when I use the application through a proxy, I couldn't get the details of the topics instead of getting the exception as The remote server returned an error: (407) Proxy Authentication Required at the line if (namespaceManager.TopicExists(path)). But I have specified an outbound rule to allow connections made from chrome. In few other resources I have seen that the solution for this is to set the proxy details to the WebRequest.DefaultWebProxy for eg:

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

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

But this approach is overriding the default proxy used in the entire application and has been reflected in other areas too. But I want to apply the proxy values only for the service bus topic call.

谁能帮我使用 C# 为 Azure 服务总线代理配置代理.

Can someone help me in configuring proxy for azure service bus proxy using C#.

推荐答案

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

The error code 407 shows that a proxy is already being used. Just that there is proxy authentication issue. It essential could use the system proxy settings and better than the one you created without bypass list etc.

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

To keep use the existing proxy you can try the following: WebRequest.DefaultWebProxy.Credentials = new NetworkCredential(data.ProxyUsername, data.ProxyPassword);

谢谢

这篇关于如何通过代理连接到 Azure 服务总线主题 - C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆