question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUERY] ServiceBus TopicClient sync Close()

See original GitHub issue

Query/Question

We currently have a service bus helper class that publishes messages to service bus using the .net standard strongly type topicClient class. We have a static readonly ConcurrentDictionary of topicClients. Basically for each Topic we have, we open and connect (via TopicClient) on start of the application (per recommendations that I have read). We do this for the lifetime of the application (for us this is a hosted background service). We do not allow closing of the topicClients nor do we have code where we use using(var ourUtilityClass). We want keep the topicClients connections open and avoid closing, opening frequently. All this is in place and works properly. The hosted background service is running in a .net core api application.

The TopicClient now only has a CloseAsync() method, the older library had both Close() and CloseAsync().

So my question is how do you recommend we handle Closing the Connections when a process exits or say is in a process shuts down (e.g. app pool recycle)?

Async and Dispose() don’t seem to play nicely together. We have been experiencing what I believe are race conditions which are challenging to identify and debug and this is one of the areas I am looking at.

Currently we implemented IDisposable in our helper class (see code below): Basically, we are looping thru our static dictionary of topicClients and calling CloseAsyc().

Do you think this might cause issues between IIS and kesteral on an app pool recycle and possible race conditions (or cause the app to crash)? Note the GetAwaiter().GetResult() in our dispose class. We can’t change the method signature of void Dispose to Task Dispose.

If we just remove the code below and do not implement IDisposable my fear is we may leave connections open during a process restart/recycle.

        public void Dispose()
        {
            Dispose(true).GetAwaiter().GetResult();

            // Use SuppressFinalize in case a subclass 
            // of this type implements a finalizer.
            GC.SuppressFinalize(this);
        }

        protected virtual Task Dispose(bool disposing)
        {
            if (disposing)
            {
                foreach (var topicClient in _topicClients.Values)
                {
                    return topicClient.CloseAsync();
                }
                _topicClients.Clear();
            }
            return Task.CompletedTask;
        }

Why is this not a Bug or a feature Request? From my reading of other issues and questions concerning (IDispose and async) it is sounds like the Azure Service Bus library leaves it up to client applications to manage the resources (opening connections, closing connections). So this is more of a question of "how do we handle dispose, when app pool is recycling or a process is shutting down)?

Setup (please complete the following information if applicable):

  • OS: Windows IIS -> .net Core api
  • IDE : Visual Studio 2017/2019
  • Version of the Library used

Information Checklist Kindly make sure that you have added all the following information above and checkoff the required fields otherwise we will treat the issuer as an incomplete report

  • Query Added
  • Setup information Added

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
kurtzeborncommented, Jun 10, 2019

Thank you for opening this issue! We are routing it to the appropriate team for follow up.

0reactions
SeanFeldmancommented, Jul 22, 2019

@jfggdl @AlexGhiondea this can be closed. Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Best practices for improving performance using Azure ...
Describes how to use Service Bus to optimize performance when exchanging brokered messages.
Read more >
Get started with Azure Service Bus queues (Python)
This tutorial shows you how to send messages to and receive messages from Azure Service Bus queues using the Python programming language.
Read more >
Azure Service Bus send messages synchronously
The Service Bus client library does not support synchronous operations. In order to use it synchronously, you will need to resort to ...
Read more >
Introducing Queues and Topics in Azure Service Bus
MessageSender/MessageReceiver: Used in the runtime context and allow you to send and receive messages over a Queue, Topic or both. Let's take a ......
Read more >
Adopting Asynchronous Messaging With Azure Service Bus
Azure Service Bus is Microsoft's asynchronous message broker offering and enables async communication through their queues and topics.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found