-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Connected/Disconnected events #28379
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Data; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
@@ -25,10 +26,9 @@ public class ServiceBusClient : IAsyncDisposable | |
private volatile bool _closed; | ||
|
||
/// <summary> | ||
/// The fully qualified Service Bus namespace that the connection is associated with. This is likely | ||
/// The fully qualified Service Bus namespace that the connection is associated with. This is likely | ||
/// to be similar to <c>{yournamespace}.servicebus.windows.net</c>. | ||
/// </summary> | ||
/// | ||
public virtual string FullyQualifiedNamespace => Connection.FullyQualifiedNamespace; | ||
|
||
/// <summary> | ||
|
@@ -44,6 +44,67 @@ public virtual bool IsClosed | |
private set => _closed = value; | ||
} | ||
|
||
/// <summary> | ||
/// An event that can be subscribed to for notification when the client connects to the service. | ||
/// </summary> | ||
#pragma warning disable AZC0002 | ||
#pragma warning disable AZC0003 | ||
public event Func<ServiceBusConnectionEventArgs, Task> ConnectedAsync; | ||
#pragma warning restore AZC0003 | ||
#pragma warning restore AZC0002 | ||
|
||
/// <summary> | ||
/// An event that can be subscribed to for notification when the client disconnects from the service. | ||
/// No action is required when the client temporarily disconnects due to a transient network or service issue, as the client will attempt | ||
/// to re-establish the connection automatically on the next operation. If <see cref="DisposeAsync"/> is | ||
/// called, then the connection will not be re-established. | ||
/// </summary> | ||
#pragma warning disable AZC0003 | ||
#pragma warning disable AZC0002 | ||
public event Func<ServiceBusConnectionEventArgs, Task> DisconnectedAsync; | ||
#pragma warning restore AZC0002 | ||
#pragma warning restore AZC0003 | ||
|
||
/// <summary> | ||
/// Gets whether or not the client is currently connected to the service. No action is required when the client temporarily | ||
/// disconnects due to a transient network or service issue, as the client will attempt to re-establish the connection | ||
/// automatically on the next operation. As such, it is not recommended that this property is checked before performing operations with the <see cref="ServiceBusClient"/> | ||
/// or any of the other Service Bus types. This property should be used only for diagnostic information. | ||
/// </summary> | ||
public virtual bool IsConnected => Connection.IsConnected; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should also consider noting that this property does not indicate that read/publish operations are actively taking place. |
||
|
||
/// <summary> | ||
/// Invokes the <see cref="ConnectedAsync"/> event handler when the client connects to the service. | ||
/// This method can be overridden to raise an event manually for testing purposes. | ||
/// </summary> | ||
/// <param name="args">The event args containing information related to the connection.</param> | ||
protected virtual async Task OnConnectedAsync(ServiceBusConnectionEventArgs args) | ||
{ | ||
var handler = ConnectedAsync; | ||
if (handler != null) | ||
{ | ||
await handler(args).ConfigureAwait(false); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Invokes the <see cref="DisconnectedAsync"/> event handler when the client disconnects from the service. | ||
/// This method can be overridden to raise an event manually for testing purposes. | ||
/// </summary> | ||
/// <param name="args">The event args containing information related to the connection.</param> | ||
protected virtual async Task OnDisconnectedAsync(ServiceBusConnectionEventArgs args) | ||
{ | ||
var handler = DisconnectedAsync; | ||
if (handler != null) | ||
{ | ||
await handler(args).ConfigureAwait(false); | ||
} | ||
} | ||
|
||
private async Task DisconnectHandlerAsync(ServiceBusConnectionEventArgs args) => await OnDisconnectedAsync(args).ConfigureAwait(false); | ||
|
||
private async Task ConnectedHandlerAsync(ServiceBusConnectionEventArgs args) => await OnConnectedAsync(args).ConfigureAwait(false); | ||
|
||
/// <summary> | ||
/// The transport type used for this <see cref="ServiceBusClient"/>. | ||
/// </summary> | ||
|
@@ -153,6 +214,8 @@ public ServiceBusClient(string connectionString, ServiceBusClientOptions options | |
Logger.ClientCreateStart(typeof(ServiceBusClient), FullyQualifiedNamespace); | ||
Identifier = DiagnosticUtilities.GenerateIdentifier(FullyQualifiedNamespace); | ||
TransportType = _options.TransportType; | ||
Connection.InnerClient.ConnectedAsync += ConnectedHandlerAsync; | ||
Connection.InnerClient.DisconnectedAsync += DisconnectHandlerAsync; | ||
Logger.ClientCreateComplete(typeof(ServiceBusClient), Identifier); | ||
} | ||
|
||
|
@@ -237,6 +300,8 @@ private ServiceBusClient( | |
credential, | ||
_options); | ||
TransportType = _options.TransportType; | ||
Connection.InnerClient.ConnectedAsync += ConnectedHandlerAsync; | ||
Connection.InnerClient.DisconnectedAsync += DisconnectHandlerAsync; | ||
Logger.ClientCreateComplete(typeof(ServiceBusClient), Identifier); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Net; | ||
|
||
namespace Azure.Messaging.ServiceBus | ||
{ | ||
/// <summary> | ||
/// The event args specified when the state of the connection with the service changes. | ||
/// </summary> | ||
public class ServiceBusConnectionEventArgs : EventArgs | ||
{ | ||
/// <inheritdoc cref="ServiceBusClient.FullyQualifiedNamespace"/> | ||
public string FullyQualifiedNamespace { get; } | ||
|
||
/// <summary> | ||
/// The proxy being used when communicating with the service. | ||
/// </summary> | ||
public IWebProxy Proxy { get; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Proxy and TransportType are interesting additions. What's the scenario where they'd be useful? I'd see the client identifier being more helpful to differentiate between multiple instances. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I considered that but was thinking it might be better to wait until we actually expose Identifier on the client. Mostly, I just wanted to add the event args type now so that we can evolve it in the future. |
||
|
||
/// <inheritdoc cref="ServiceBusClient.TransportType"/> | ||
public ServiceBusTransportType TransportType { get; } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of <see cref="ServiceBusConnectionEventArgs"/>. | ||
/// </summary> | ||
/// | ||
/// <param name="fullyQualifiedNamespace">The fully qualified namespace.</param> | ||
/// <param name="transportType">The transport type in use when communicating with the service.</param> | ||
/// <param name="proxy">The proxy being used when communicating with the service.</param> | ||
public ServiceBusConnectionEventArgs(string fullyQualifiedNamespace, ServiceBusTransportType transportType, IWebProxy proxy) | ||
{ | ||
FullyQualifiedNamespace = fullyQualifiedNamespace; | ||
TransportType = transportType; | ||
Proxy = proxy; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Comments for what these warnings are would be helpful.