2

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

Sandhiya
  • 57
  • 1
  • 8

2 Answers2

1

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

LarryX
  • 591
  • 2
  • 7
  • So many Thanks for the info. Yes obviously this method solves my problem but I am just concerned about changing the values of WebRequest.DefaultWebProxy since it has few impacts on other features/functionalities. I tried to reset the WebRequest.DefaultWebProxy.Credentials to its original value once after the topics related functions are completed, and this is working fine when the application is in debug mode but when running in normal mode since the functions are called parallel it is affecting other features too. – Sandhiya Jul 19 '21 at 13:18
0

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);
Luke G
  • 46
  • 4