Azure Service Bus access error: System.Net.WebSockets.Client: Unable to connect to the remote server

142 views Asked by At

While following the Microsoft Documentation to create Azure Service Bus,

https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-dotnet-get-started-with-queues?tabs=passwordless

and Trying to send some msg from the Azure function (HTTP Trigger SendMsg_HttpTrigger ) getting the below error:

Executed 'SendMsg_HttpTrigger' (Failed, Id=37c685cb-51b5-41ca-9d58-786508099c9b, Duration=78ms)
[2023-12-18T01:14:24.257Z] System.Private.CoreLib: Exception while executing function: SendMsg_HttpTrigger. System.Net.WebSockets.Client: Unable to connect to the remote server. System.Net.Http: The requested name is valid, but no data of the requested type was found. (alz-csp-sbus.servicebus.windows.net:443). System.Net.Sockets: The requested name is valid, but no data of the requested type was found.

Following the Q&A section (Troubling shooting) https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-troubleshooting-guide

But not help for me.

Any help would be great.

1

There are 1 answers

1
Sampath On

The below code for an Azure Functions HTTP trigger written in C# that sends a message to an Azure Service Bus queue.

  • Code Taken from MSDOC.
  • I used a static message content ("Hello"). you want to extract the message content from the HTTP request body or parameters.
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;
using Azure.Messaging.ServiceBus;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.WebJobs.Extensions.Http;
using System.Threading.Tasks;
using System;

public static class SendMsg_HttpTrigger
{
    [FunctionName("SendMsg_HttpTrigger")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
        ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");

        try
        {
            // Set a sample message content
            string messageContent = "Hello";

            
            string serviceBusConnectionString = "Azure Service Bus connection string ";

           
            var clientOptions = new ServiceBusClientOptions
            {
                TransportType = ServiceBusTransportType.AmqpTcp
            };

            log.LogInformation($"Connecting to Service Bus using connection string...");

            var serviceBusClient = new ServiceBusClient(serviceBusConnectionString, clientOptions);

            
            var sender = serviceBusClient.CreateSender(" Azure  Service QueueName ");

          
            var message = new ServiceBusMessage(messageContent);

           
            await sender.SendMessageAsync(message);

            log.LogInformation("Message sent to Service Bus queue");

            
            await sender.DisposeAsync();
            await serviceBusClient.DisposeAsync();

            return new OkObjectResult($"Message '{messageContent}' sent to Service Bus queue");
        }
        catch (Exception ex)
        {
            log.LogError($"An error occurred: {ex.Message}");
            return new ObjectResult($"Error: {ex.Message}") { StatusCode = 500 };
        }
    }
}

enter image description here

Azure: enter image description here