简体   繁体   中英

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

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

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.

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

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.

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

Thanks

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