1

I am trying to get started with Azure Service Bus queues. following this article https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-dotnet-get-started-with-queues the only difference is that I am trying to do this from within a web api.
The error I get is: No connection could be made because the target machine actively refused it 40.84.xxx.xx:443

I'd appreciate any help or pointers!

Note: Console app works just fine following the above guide.

Updated 7/24, this is the code in my action method:

   try
        {
            var connectionString =
                "Endpoint=sb://xxxxx-test.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=shared_access_key";
            var queueName = "testqueue1";

            var client = 
    QueueClient.CreateFromConnectionString(connectionString, queueName);

            var message = new BrokeredMessage(eventMessage);
            client.Send(message);
        }
        catch (Exception e)
        {
          //log exception
        }

Update 7/25. I was able to make it work by setting defaultConfig entry as enabled in web.config:

 <system.net>
   <defaultProxy enabled="true"/>
 </system.net>
the_sheikh
  • 21
  • 1
  • 7
  • Related question : https://stackoverflow.com/q/2972600/2525304 – Maxime Jul 21 '17 at 12:05
  • @Maxime if this was a network issue or firewall issue, the console app should also get this same error - which is not the case. – the_sheikh Jul 21 '17 at 12:39
  • @the_sheikh creating client is an expensive operation. You should look into changing your code to keep the client for a longer time. – Sean Feldman Jul 24 '17 at 15:58
  • Aside from that, your code looks fine. It's an infrastructure issue like others pointed out. – Sean Feldman Jul 24 '17 at 15:59
  • @SeanFeldman are you suggesting to keep the QueueClient instance static, like httpClient? my azure code worked after adding defaultProxy entry in web.config. i am still trying to figure out why this is not needed in case of a console app. – the_sheikh Jul 25 '17 at 08:11
  • Anyway you want. The thing is that establishing a connection with the broker is expensive. If you do that on each request, you're slowing down things. – Sean Feldman Jul 25 '17 at 20:16
  • Please post the solution as an answer and mark it as such. Do not edit your question to include the solution as it's confusing for future readers. Thank you. – tom redfern Sep 04 '17 at 13:40
  • @tomredfern thanks for pointing this out. Have posted the 7/25 update as answer – the_sheikh Sep 06 '17 at 09:15

2 Answers2

1

No connection could be made because the target machine actively refused it 40.84.xxx.xx:443

Please check whether the outbound 443 port is blocked by your firewall.

Note: Console app works just fine following the above guide.

The default value of connectivity mode for Service Bus is AutoDetect. It will automatically selects between the Tcp and Http modes based on an auto-detection mechanism that probes whether either connectivity option is available for the current network environment. It maybe choose different modes for your Console App and Web API application. Try to set it to TCP explicitly in your Web API application before using the Service Bus Queue.

ServiceBusEnvironment.SystemConnectivity.Mode = ConnectivityMode.Tcp;
Amor
  • 8,325
  • 2
  • 19
  • 21
  • setting connectivity mode to TCP does not work from within my Web API. Question: have you been able to publish to azure queue from a web api or mvc app? – the_sheikh Jul 24 '17 at 10:02
  • Yes, I just tested with my web api. I can add a message to a queue. – Amor Jul 24 '17 at 10:51
  • There is no problem with your code and the error is not related to code. Does your machine use any proxyes? – Amor Jul 24 '17 at 12:25
  • you proxy question lead me to this [this](https://stackoverflow.com/questions/186800/is-it-possible-to-specify-proxy-credentials-in-your-web-config). Looks like all i needed was a web.config entry. have updated my question above with this. – the_sheikh Jul 25 '17 at 07:35
1

I was able to make it work by setting defaultConfig entry as enabled in web.config:

<system.net>
  <defaultProxy enabled="true"/>
</system.net>
the_sheikh
  • 21
  • 1
  • 7