Sandhiya
Sandhiya

Reputation: 65

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#.

Upvotes: 2

Views: 2181

Answers (2)

Luke G
Luke G

Reputation: 56

To enable proxy connections, you must use web sockets. Without web sockets, specifying a proxy is not a valid option. MS Article.

The main pieces to get the Proxy working are:

var proxy = new WebProxy("http://proxy.com:8080", true);
proxy.Credentials = new NetworkCredential("username", "password"); //if auth is required (407)

var options = new ServiceBusClientOptions();
        options.WebProxy = proxy;
        options.TransportType = ServiceBusTransportType.AmqpWebSockets; //enable WebSockets

var client = new ServiceBusClient(connectionString, options);

Upvotes: 1

LarryX
LarryX

Reputation: 641

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

Upvotes: 1

Related Questions