[QUERY] ServiceBus TopicClient sync Close()
See original GitHub issueQuery/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:
- Created 4 years ago
- Comments:5 (4 by maintainers)
Top GitHub Comments
Thank you for opening this issue! We are routing it to the appropriate team for follow up.
@jfggdl @AlexGhiondea this can be closed. Thank you.