E.Z. Hart
E.Z. Hart

Reputation: 5747

Socket error sending message to Service Bus 1.0 queue

I'm following this tutorial, and I keep getting an exception calling QueueClient.Send().

First off, here's my connection string setting in the App.Config (with {computername} replaced by the actual machine name):

<add key="Microsoft.ServiceBus.ConnectionString" value="Endpoint=sb://{computername}/ServiceBusDefaultNamespace;StsEndpoint=https://{computername}:9355/ServiceBusDefaultNamespace;RuntimePort=9354;ManagementPort=9355" />

Here's the code I'm running:

NamespaceManager namespaceManager = NamespaceManager.Create();

TokenProvider nameSpaceManagerTokenProvider = TokenProvider.CreateWindowsTokenProvider(
    new List<Uri>() { namespaceManager.Address }, new NetworkCredential(user, password));

TokenProvider messagingToken = TokenProvider.CreateWindowsTokenProvider(
    new List<Uri>() { namespaceManager.Address }, new NetworkCredential(user, password));

namespaceManager.Settings.TokenProvider = nameSpaceManagerTokenProvider;

MessagingFactorySettings messageFactorySettings = new MessagingFactorySettings {TokenProvider = messagingToken};

MessagingFactory messagingFactory = MessagingFactory.Create(namespaceManager.Address, messageFactorySettings);

if (namespaceManager.QueueExists(QueueName))
{
    namespaceManager.DeleteQueue(QueueName);
}

QueueDescription qd = new QueueDescription(QueueName);

namespaceManager.CreateQueue(qd);

QueueClient myQueueClient = messagingFactory.CreateQueueClient(QueueName);

BrokeredMessage sendMessage = new BrokeredMessage("Hello, World!");

myQueueClient.Send(sendMessage); // <---- This is where I'm getting the exception

The queue is deleted/created without a problem. Calling the .Send() method gives me the following error:

Microsoft.ServiceBus.Messaging.MessagingCommunicationException

"The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue. Local socket timeout was '00:00:59.9579976'."

The inner exception is simply "An existing connection was forcibly closed by the remote host"

Here's the stack trace:

   at Microsoft.ServiceBus.Messaging.Sbmp.SbmpMessageSender.EndSendCommand(IAsyncResult result)
   at Microsoft.ServiceBus.Messaging.Sbmp.SbmpMessageSender.OnEndSend(IAsyncResult result)
   at Microsoft.ServiceBus.Messaging.Sbmp.SbmpMessageSender.OnSend(TrackingContext trackingContext, IEnumerable`1 messages, TimeSpan timeout)
   at Microsoft.ServiceBus.Messaging.MessageSender.Send(TrackingContext trackingContext, IEnumerable`1 messages, TimeSpan timeout)
   at Microsoft.ServiceBus.Messaging.MessageSender.Send(BrokeredMessage message)
   at Microsoft.ServiceBus.Messaging.QueueClient.Send(BrokeredMessage message)
   at SBDemo.Program.Main(String[] args) in c:\Users\hartez\Documents\bitbucket\SBDemo\SBDemo\Program.cs:line 51
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

I'm currently running both the client code and the Service Bus on a Windows 7 64-bit dev box. I originally ran Service Bus on a 2012 Server machine and had the same problem.

The problem occurs with both the WindowsTokenProvider and the OauthTokenProvider. The user account is an administrator (in hopes that this was just a permissions issue); that doesn't seem to help. I've also tried this with the Windows Firewall deactivated, but that didn't help, either.

I enabled the Analytic and Debug logs in the Event Viewer, but I'm not seeing an anything in those logs to suggest what the problem might be.

If anyone has any suggestions on what might be wrong, or on other ways to debug this, I'd very much appreciate it.

Upvotes: 3

Views: 4313

Answers (3)

RichIntellect
RichIntellect

Reputation: 281

I had an issue with sending messages to a topic even though I could read messages with the error: 'An existing connection was forcibly closed by the remote host, Error Code: Connection Reset'.

After hours of despair I found that I did not set the TransportType specifically at startup to use AmqpWebSockets. I expect that you need to explicitly specify the Transport Type rather than rely on the default.

clientBuilder.AddServiceBusClient(.....).WithCredential(...)
.ConfigureOptions(options => { options.TransportType = AmqpWebSockets ...

Upvotes: 0

Hillary Caituiro Monge
Hillary Caituiro Monge

Reputation: 160

Glad you have figured this out.

MessagingFactory messagingFactory = MessagingFactory.Create(namespaceManager.Address, messageFactorySettings);

The issue you had in your original code is that it is using the NamespaceManager address for the MessagingFactory. The MessagingFactory uses a different port than the NamespaceManager.

NamespaceManager is used for management (CRUD) operations and SB has a management endpoint for it.

MessagingFactory is used for runtime operations (Send/Receive/..) and SB has a runtime endoiunt for it.

QueueClient.Create(QueueName) internally creates a messaging factory and uses the default address and port for runtime operations.

Upvotes: 3

E.Z. Hart
E.Z. Hart

Reputation: 5747

Finally figured this out, putting this here so anyone else in the same boat can avoid wrestling with this problem:

Apparently, the problem is somewhere in the use of MessagingFactory in the examples. I'm not really sure what it's even for (maybe handling things like AMQP, which isn't really supported by Service Bus yet?), but you don't need it. You can create the client using QueueClient.Create(). I revised the code to look like this, and now it works just fine:

NamespaceManager namespaceManager = NamespaceManager.Create();

TokenProvider nameSpaceManagerTokenProvider = TokenProvider.CreateWindowsTokenProvider(
    new List<Uri>() { namespaceManager.Address }, new NetworkCredential(user, password));

namespaceManager.Settings.TokenProvider = nameSpaceManagerTokenProvider;

if (namespaceManager.QueueExists(QueueName))
{
    namespaceManager.DeleteQueue(QueueName);
}

QueueDescription qd = new QueueDescription(QueueName);

namespaceManager.CreateQueue(qd);

QueueClient myQueueClient = QueueClient.Create(QueueName);

BrokeredMessage sendMessage = new BrokeredMessage("Hello, World!");

myQueueClient.Send(sendMessage);

Upvotes: 1

Related Questions