Class DBusConnection
Represents a client connection to a D-Bus message bus or peer.
public sealed class DBusConnection : IDisposable
- Inheritance
-
DBusConnection
- Implements
- Inherited Members
Constructors
DBusConnection(string)
Initializes a new instance of the DBusConnection class.
public DBusConnection(string address)
Parameters
addressstringThe D-Bus address to connect to.
DBusConnection(DBusConnectionOptions)
Initializes a new instance of the DBusConnection class.
public DBusConnection(DBusConnectionOptions connectionOptions)
Parameters
connectionOptionsDBusConnectionOptionsThe connection options.
Properties
Session
Gets a shared connection to the session bus.
public static DBusConnection Session { get; }
Property Value
System
Gets a shared connection to the system bus.
public static DBusConnection System { get; }
Property Value
UniqueName
Gets the unique name assigned to this connection by the bus.
public string? UniqueName { get; }
Property Value
Methods
AddMatchAsync(MatchRule, Action<Notification>, bool, ObserverFlags, object?)
Adds an observer to receive D-Bus messages matching the specified criteria.
public ValueTask<IDisposable> AddMatchAsync(MatchRule rule, Action<Notification> handler, bool emitOnCapturedContext = true, ObserverFlags flags = ObserverFlags.None, object? state = null)
Parameters
ruleMatchRuleThe match rule defining which messages to receive.
handlerAction<Notification>Callback invoked for matched messages and completion notifications.
emitOnCapturedContextboolWhether to invoke the handler on the current synchronization context.
flagsObserverFlagsObserver behavior flags.
stateobjectOptional state object.
Returns
- ValueTask<IDisposable>
IDisposable that stops the observer when disposed.
Remarks
The handler must not throw exceptions. If the handler throws, the connection will be disconnected.
AddMatchAsync(MatchRule, Action<Notification>, SynchronizationContext?, ObserverFlags, object?)
Adds an observer to receive D-Bus messages matching the specified criteria.
public ValueTask<IDisposable> AddMatchAsync(MatchRule rule, Action<Notification> handler, SynchronizationContext? synchronizationContext, ObserverFlags flags, object? state)
Parameters
ruleMatchRuleThe match rule defining which messages to receive.
handlerAction<Notification>Callback invoked for matched messages and completion notifications.
synchronizationContextSynchronizationContextThe synchronization context to invoke the handler on.
flagsObserverFlagsObserver behavior flags.
stateobjectOptional state object.
Returns
- ValueTask<IDisposable>
IDisposable that stops the observer when disposed.
Remarks
The handler must not throw exceptions. If the handler throws, the connection will be disconnected.
AddMatchAsync<T>(MatchRule, MessageValueReader<T>, Action<Exception?, T, object?, object?>, object?, object?, bool, ObserverFlags)
Adds an observer to receive D-Bus messages matching the specified criteria.
[Obsolete("Use the overload that accepts Action<Notification<T>> instead.")]
public ValueTask<IDisposable> AddMatchAsync<T>(MatchRule rule, MessageValueReader<T> reader, Action<Exception?, T, object?, object?> handler, object? readerState, object? handlerState, bool emitOnCapturedContext, ObserverFlags flags)
Parameters
ruleMatchRuleThe match rule defining which messages to receive.
readerMessageValueReader<T>Delegate to read values from matched messages.
handlerAction<Exception, T, object, object>Callback invoked for matched messages and completion notifications.
readerStateobjectOptional state passed to the reader delegate.
handlerStateobjectOptional state passed to the handler delegate.
emitOnCapturedContextboolWhether to invoke the handler on the current synchronization context.
flagsObserverFlagsObserver behavior flags.
Returns
- ValueTask<IDisposable>
IDisposable that stops the observer when disposed.
Type Parameters
TThe type of value read from matched messages.
AddMatchAsync<T>(MatchRule, MessageValueReader<T>, Action<Exception?, T, object?, object?>, object?, object?, SynchronizationContext?, ObserverFlags)
Adds an observer to receive D-Bus messages matching the specified criteria.
[Obsolete("Use the overload that accepts Action<Notification<T>> instead.")]
public ValueTask<IDisposable> AddMatchAsync<T>(MatchRule rule, MessageValueReader<T> reader, Action<Exception?, T, object?, object?> handler, object? readerState, object? handlerState, SynchronizationContext? synchronizationContext, ObserverFlags flags)
Parameters
ruleMatchRuleThe match rule defining which messages to receive.
readerMessageValueReader<T>Delegate to read values from matched messages.
handlerAction<Exception, T, object, object>Callback invoked for matched messages and completion notifications.
readerStateobjectOptional state passed to the reader delegate.
handlerStateobjectOptional state passed to the handler delegate.
synchronizationContextSynchronizationContextThe synchronization context to invoke the handler on.
flagsObserverFlagsObserver behavior flags.
Returns
- ValueTask<IDisposable>
IDisposable that stops the observer when disposed.
Type Parameters
TThe type of value read from matched messages.
AddMatchAsync<T>(MatchRule, MessageValueReader<T>, Action<Exception?, T, object?, object?>, ObserverFlags, object?, object?, bool)
Adds an observer to receive D-Bus messages matching the specified criteria.
[Obsolete("Use the overload that accepts Action<Notification<T>> instead.")]
public ValueTask<IDisposable> AddMatchAsync<T>(MatchRule rule, MessageValueReader<T> reader, Action<Exception?, T, object?, object?> handler, ObserverFlags flags, object? readerState = null, object? handlerState = null, bool emitOnCapturedContext = true)
Parameters
ruleMatchRuleThe match rule defining which messages to receive.
readerMessageValueReader<T>Delegate to read values from matched messages.
handlerAction<Exception, T, object, object>Callback invoked for matched messages and completion notifications.
flagsObserverFlagsObserver behavior flags.
readerStateobjectOptional state passed to the reader delegate.
handlerStateobjectOptional state passed to the handler delegate.
emitOnCapturedContextboolWhether to invoke the handler on the current synchronization context.
Returns
- ValueTask<IDisposable>
IDisposable that stops the observer when disposed.
Type Parameters
TThe type of value read from matched messages.
AddMatchAsync<T>(MatchRule, MessageValueReader<T>, Action<Notification<T>>, bool, ObserverFlags, object?)
Adds an observer to receive D-Bus messages matching the specified criteria.
public ValueTask<IDisposable> AddMatchAsync<T>(MatchRule rule, MessageValueReader<T> reader, Action<Notification<T>> handler, bool emitOnCapturedContext = true, ObserverFlags flags = ObserverFlags.None, object? state = null)
Parameters
ruleMatchRuleThe match rule defining which messages to receive.
readerMessageValueReader<T>Delegate to read values from matched messages.
handlerAction<Notification<T>>Callback invoked for matched messages and completion notifications.
emitOnCapturedContextboolWhether to invoke the handler on the current synchronization context.
flagsObserverFlagsObserver behavior flags.
stateobjectOptional state object.
Returns
- ValueTask<IDisposable>
IDisposable that stops the observer when disposed.
Type Parameters
TThe type of value read from matched messages.
Remarks
The handler must not throw exceptions. If the handler throws, the connection will be disconnected.
AddMatchAsync<T>(MatchRule, MessageValueReader<T>, Action<Notification<T>>, SynchronizationContext?, ObserverFlags, object?)
Adds an observer to receive D-Bus messages matching the specified criteria.
public ValueTask<IDisposable> AddMatchAsync<T>(MatchRule rule, MessageValueReader<T> reader, Action<Notification<T>> handler, SynchronizationContext? synchronizationContext, ObserverFlags flags, object? state)
Parameters
ruleMatchRuleThe match rule defining which messages to receive.
readerMessageValueReader<T>Delegate to read values from matched messages.
handlerAction<Notification<T>>Callback invoked for matched messages and completion notifications.
synchronizationContextSynchronizationContextThe synchronization context to invoke the handler on.
flagsObserverFlagsObserver behavior flags.
stateobjectOptional state object.
Returns
- ValueTask<IDisposable>
IDisposable that stops the observer when disposed.
Type Parameters
TThe type of value read from matched messages.
Remarks
The handler must not throw exceptions. If the handler throws, the connection will be disconnected.
AddMethodHandler(IPathMethodHandler)
Adds an IPathMethodHandler to handle incoming method calls.
public void AddMethodHandler(IPathMethodHandler methodHandler)
Parameters
methodHandlerIPathMethodHandlerThe method handler to add.
AddMethodHandlers(IReadOnlyList<IPathMethodHandler>)
Adds multiple IPathMethodHandler instances to handle incoming method calls.
public void AddMethodHandlers(IReadOnlyList<IPathMethodHandler> methodHandlers)
Parameters
methodHandlersIReadOnlyList<IPathMethodHandler>The method handlers to add.
BecomeMonitorAsync(Action<Exception?, DisposableMessage>, IEnumerable<MatchRule>?)
Becomes a monitor that receives all messages on the bus.
public Task BecomeMonitorAsync(Action<Exception?, DisposableMessage> handler, IEnumerable<MatchRule>? rules = null)
Parameters
handlerAction<Exception, DisposableMessage>The handler invoked for each message received.
rulesIEnumerable<MatchRule>Optional match rules to filter which messages to receive.
Returns
CallMethodAsync(MessageBuffer)
Calls a D-Bus method asynchronously without returning a response value.
public Task CallMethodAsync(MessageBuffer message)
Parameters
messageMessageBufferThe method call message.
Returns
Exceptions
- DBusOwnerChangedException
The message destination uses an owner identifier from NameOwnerWatcher and the owner of the name has changed.
CallMethodAsync<T>(MessageBuffer, MessageValueReader<T>, object?)
Calls a D-Bus method asynchronously and returns a response value.
public Task<T> CallMethodAsync<T>(MessageBuffer message, MessageValueReader<T> reader, object? readerState = null)
Parameters
messageMessageBufferThe method call message.
readerMessageValueReader<T>The delegate to read the return value from the reply.
readerStateobjectOptional state passed to the reader delegate.
Returns
- Task<T>
Value read from the reply.
Type Parameters
TThe type of the return value.
Exceptions
- DBusOwnerChangedException
The message destination uses an owner identifier from NameOwnerWatcher and the owner of the name has changed.
ConnectAsync()
Establishes the connection.
public ValueTask ConnectAsync()
Returns
DisconnectedAsync()
Returns a task that completes when the connection has disconnected.
public Task<Exception?> DisconnectedAsync()
Returns
Dispose()
Disposes the connection and releases all resources.
public void Dispose()
GetMessageWriter()
Gets a message writer for creating D-Bus messages.
public MessageWriter GetMessageWriter()
Returns
- MessageWriter
A new MessageWriter instance.
ListActivatableServicesAsync()
Gets all service names that can be activated on the bus.
public Task<string[]> ListActivatableServicesAsync()
Returns
ListServicesAsync()
Lists all currently registered service names on the bus.
public Task<string[]> ListServicesAsync()
Returns
MonitorBusAsync(string, IEnumerable<MatchRule>?, CancellationToken)
Monitors a D-Bus bus and returns an IAsyncEnumerable<T> for the observed messages.
public static IAsyncEnumerable<DisposableMessage> MonitorBusAsync(string address, IEnumerable<MatchRule>? rules = null, CancellationToken ct = default)
Parameters
addressstringThe D-Bus address to connect to.
rulesIEnumerable<MatchRule>Optional match rules to filter which messages to receive.
ctCancellationTokenCancellation token to stop monitoring.
Returns
QueueNameRequestAsync(string, RequestNameOptions)
Enqueues for ownership of a name.
public Task QueueNameRequestAsync(string name, RequestNameOptions options)
Parameters
namestringThe name to request.
optionsRequestNameOptionsOptions for requesting the name.
Returns
QueueNameRequestAsync(string, RequestNameOptions, Action<string, object?>?, Action<string, object?>?, object?, bool)
Enqueues for ownership of a name with callbacks for acquisition and loss notifications.
public Task QueueNameRequestAsync(string name, RequestNameOptions options = RequestNameOptions.Default, Action<string, object?>? onAcquired = null, Action<string, object?>? onLost = null, object? actionState = null, bool emitOnCapturedContext = true)
Parameters
namestringThe well-known name to request.
optionsRequestNameOptionsOptions for requesting the name.
onAcquiredAction<string, object>Callback invoked when the name is acquired.
onLostAction<string, object>Callback invoked when the name is lost to another bus user.
actionStateobjectState object passed to the callbacks.
emitOnCapturedContextboolWhether to invoke callbacks on the captured synchronization context.
Returns
ReleaseNameAsync(string)
Releases ownership of a name.
public Task<bool> ReleaseNameAsync(string serviceName)
Parameters
serviceNamestringThe well-known name to release.
Returns
- Task<bool>
A Task containing true if the name was released/dequeued; false if the name was not requested by this connection.
RemoveMethodHandler(string)
Removes a method handler for the specified path.
public void RemoveMethodHandler(string path)
Parameters
pathstringThe object path of the handler to remove.
RemoveMethodHandlers(IEnumerable<string>)
Removes multiple method handlers for the specified paths.
public void RemoveMethodHandlers(IEnumerable<string> paths)
Parameters
pathsIEnumerable<string>The object paths of the handlers to remove.
RequestNameAsync(string, RequestNameOptions)
Requests ownership of a name.
public Task RequestNameAsync(string name, RequestNameOptions options)
Parameters
namestringThe name to request.
optionsRequestNameOptionsOptions for requesting the name.
Returns
RequestNameAsync(string, RequestNameOptions, Action<string, object?>?, object?, bool)
Requests ownership of a name with callback for name loss notification.
public Task RequestNameAsync(string name, RequestNameOptions options = RequestNameOptions.Default, Action<string, object?>? onLost = null, object? actionState = null, bool emitOnCapturedContext = true)
Parameters
namestringThe name to request.
optionsRequestNameOptionsOptions for requesting the name.
onLostAction<string, object>Callback invoked when the name is lost to another connection.
actionStateobjectState object passed to the callback.
emitOnCapturedContextboolWhether to invoke the callback on the captured synchronization context.
Returns
TryRequestNameAsync(string, RequestNameOptions)
Tries to request ownership of a name.
public Task<bool> TryRequestNameAsync(string name, RequestNameOptions options)
Parameters
namestringThe name to request.
optionsRequestNameOptionsOptions for requesting the name.
Returns
TryRequestNameAsync(string, RequestNameOptions, Action<string, object?>?, object?, bool)
Tries to request ownership of a name with callback for name loss notification.
public Task<bool> TryRequestNameAsync(string name, RequestNameOptions options = RequestNameOptions.Default, Action<string, object?>? onLost = null, object? actionState = null, bool emitOnCapturedContext = true)
Parameters
namestringThe name to request.
optionsRequestNameOptionsOptions for requesting the name.
onLostAction<string, object>Callback invoked when the name is lost to another connection.
actionStateobjectState object passed to the callback.
emitOnCapturedContextboolWhether to invoke the callback on the captured synchronization context.
Returns
TrySendMessage(MessageBuffer)
Sends a D-Bus message.
public bool TrySendMessage(MessageBuffer message)
Parameters
messageMessageBufferThe message to send.
Returns
Remarks
Owner identifier destinations from NameOwnerWatcher are not supported by this API.
WatchNameOwnerAsync(string)
Watches for the owner of a D-Bus well-known name.
public Task<NameOwnerWatcher> WatchNameOwnerAsync(string name)
Parameters
namestringThe well-known name to watch.
Returns
- Task<NameOwnerWatcher>
A NameOwnerWatcher that can be used to wait for the name owner.
Exceptions
- ArgumentException
nameis a unique connection name or an owner identifier.- InvalidOperationException
The connection is not connected to a bus, or
nameis the D-Bus service name.
WatchPropertiesChangedAsync<T>(string?, string?, string, MessageValueReader<T>, Action<Exception?, T>, object?, bool, ObserverFlags)
Watches for property changes.
[Obsolete("Use the overload that accepts Action<Notification<T>> instead.")]
public ValueTask<IDisposable> WatchPropertiesChangedAsync<T>(string? sender, string? path, string @interface, MessageValueReader<T> reader, Action<Exception?, T> handler, object? readerState, bool emitOnCapturedContext, ObserverFlags flags)
Parameters
senderstringThe sender to match (optional).
pathstringThe object path to match (optional).
interfacestringThe interface name.
readerMessageValueReader<T>Delegate to read values from matched messages.
handlerAction<Exception, T>Callback invoked for matched signals and completion notifications.
readerStateobjectOptional state passed to the reader delegate.
emitOnCapturedContextboolWhether to invoke the handler on the current synchronization context.
flagsObserverFlagsObserver behavior flags.
Returns
- ValueTask<IDisposable>
IDisposable that stops the observer when disposed.
Type Parameters
T
WatchPropertiesChangedAsync<T>(string?, string?, string, MessageValueReader<T>, Action<Notification<T>>, SynchronizationContext?, ObserverFlags, object?)
Watches for property changes.
public ValueTask<IDisposable> WatchPropertiesChangedAsync<T>(string? sender, string? path, string @interface, MessageValueReader<T> reader, Action<Notification<T>> handler, SynchronizationContext? synchronizationContext, ObserverFlags flags, object? state)
Parameters
senderstringThe sender to match (optional).
pathstringThe object path to match (optional).
interfacestringThe interface name.
readerMessageValueReader<T>Delegate to read values from matched messages.
handlerAction<Notification<T>>Callback invoked for matched signals and completion notifications.
synchronizationContextSynchronizationContextThe synchronization context to invoke the handler on.
flagsObserverFlagsObserver behavior flags.
stateobjectOptional state object.
Returns
- ValueTask<IDisposable>
IDisposable that stops the observer when disposed.
Type Parameters
T
Remarks
The handler must not throw exceptions. If the handler throws, the connection will be disconnected.
WatchPropertiesChangedAsync<T>(string?, string?, string, MessageValueReader<T>, Action<Notification<T>>, ObserverFlags, bool, object?)
Watches for property changes.
public ValueTask<IDisposable> WatchPropertiesChangedAsync<T>(string? sender, string? path, string @interface, MessageValueReader<T> reader, Action<Notification<T>> handler, ObserverFlags flags, bool emitOnCapturedContext = true, object? state = null)
Parameters
senderstringThe sender to match (optional).
pathstringThe object path to match (optional).
interfacestringThe interface name.
readerMessageValueReader<T>Delegate to read values from matched messages.
handlerAction<Notification<T>>Callback invoked for matched signals and completion notifications.
flagsObserverFlagsObserver behavior flags.
emitOnCapturedContextboolWhether to invoke the handler on the current synchronization context.
stateobjectOptional state object.
Returns
- ValueTask<IDisposable>
IDisposable that stops the observer when disposed.
Type Parameters
T
Remarks
The handler must not throw exceptions. If the handler throws, the connection will be disconnected.
WatchSignalAsync(string?, string?, string, string, Action<Exception?>, object?, bool, ObserverFlags)
Watches for a D-Bus signal.
[Obsolete("Use the overload that accepts Action<Notification> instead.")]
public ValueTask<IDisposable> WatchSignalAsync(string? sender, string? path, string @interface, string signal, Action<Exception?> handler, object? readerState, bool emitOnCapturedContext, ObserverFlags flags)
Parameters
senderstringThe sender to match (optional).
pathstringThe object path to match (optional).
interfacestringThe interface name.
signalstringThe signal name.
handlerAction<Exception>Callback invoked for matched signals and completion notifications.
readerStateobjectOptional state passed to the reader delegate.
emitOnCapturedContextboolWhether to invoke the handler on the current synchronization context.
flagsObserverFlagsObserver behavior flags.
Returns
- ValueTask<IDisposable>
IDisposable that stops the observer when disposed.
WatchSignalAsync(string?, string?, string, string, Action<Notification>, SynchronizationContext?, ObserverFlags, object?)
Watches for a D-Bus signal.
public ValueTask<IDisposable> WatchSignalAsync(string? sender, string? path, string @interface, string signal, Action<Notification> handler, SynchronizationContext? synchronizationContext, ObserverFlags flags, object? state)
Parameters
senderstringThe sender to match (optional).
pathstringThe object path to match (optional).
interfacestringThe interface name.
signalstringThe signal name.
handlerAction<Notification>Callback invoked for matched signals and completion notifications.
synchronizationContextSynchronizationContextThe synchronization context to invoke the handler on.
flagsObserverFlagsObserver behavior flags.
stateobjectOptional state object.
Returns
- ValueTask<IDisposable>
IDisposable that stops the observer when disposed.
Remarks
The handler must not throw exceptions. If the handler throws, the connection will be disconnected.
WatchSignalAsync(string?, string?, string, string, Action<Notification>, ObserverFlags, bool, object?)
Watches for a D-Bus signal.
public ValueTask<IDisposable> WatchSignalAsync(string? sender, string? path, string @interface, string signal, Action<Notification> handler, ObserverFlags flags, bool emitOnCapturedContext = true, object? state = null)
Parameters
senderstringThe sender to match (optional).
pathstringThe object path to match (optional).
interfacestringThe interface name.
signalstringThe signal name.
handlerAction<Notification>Callback invoked for matched signals and completion notifications.
flagsObserverFlagsObserver behavior flags.
emitOnCapturedContextboolWhether to invoke the handler on the current synchronization context.
stateobjectOptional state object.
Returns
- ValueTask<IDisposable>
IDisposable that stops the observer when disposed.
Remarks
The handler must not throw exceptions. If the handler throws, the connection will be disconnected.
WatchSignalAsync<T>(string?, string?, string, string, MessageValueReader<T>, Action<Exception?, T>, object?, bool, ObserverFlags)
Watches for a D-Bus signal.
[Obsolete("Use the overload that accepts Action<Notification<T>> instead.")]
public ValueTask<IDisposable> WatchSignalAsync<T>(string? sender, string? path, string @interface, string signal, MessageValueReader<T> reader, Action<Exception?, T> handler, object? readerState, bool emitOnCapturedContext, ObserverFlags flags)
Parameters
senderstringThe sender to match (optional).
pathstringThe object path to match (optional).
interfacestringThe interface name.
signalstringThe signal name.
readerMessageValueReader<T>Delegate to read values from matched messages.
handlerAction<Exception, T>Callback invoked for matched signals and completion notifications.
readerStateobjectOptional state passed to the reader delegate.
emitOnCapturedContextboolWhether to invoke the handler on the current synchronization context.
flagsObserverFlagsObserver behavior flags.
Returns
- ValueTask<IDisposable>
IDisposable that stops the observer when disposed.
Type Parameters
T
WatchSignalAsync<T>(string?, string?, string, string, MessageValueReader<T>, Action<Notification<T>>, SynchronizationContext?, ObserverFlags, object?)
Watches for a D-Bus signal.
public ValueTask<IDisposable> WatchSignalAsync<T>(string? sender, string? path, string @interface, string signal, MessageValueReader<T> reader, Action<Notification<T>> handler, SynchronizationContext? synchronizationContext, ObserverFlags flags, object? state)
Parameters
senderstringThe sender to match (optional).
pathstringThe object path to match (optional).
interfacestringThe interface name.
signalstringThe signal name.
readerMessageValueReader<T>Delegate to read values from matched messages.
handlerAction<Notification<T>>Callback invoked for matched signals and completion notifications.
synchronizationContextSynchronizationContextThe synchronization context to invoke the handler on.
flagsObserverFlagsObserver behavior flags.
stateobjectOptional state object.
Returns
- ValueTask<IDisposable>
IDisposable that stops the observer when disposed.
Type Parameters
T
Remarks
The handler must not throw exceptions. If the handler throws, the connection will be disconnected.
WatchSignalAsync<T>(string?, string?, string, string, MessageValueReader<T>, Action<Notification<T>>, ObserverFlags, bool, object?)
Watches for a D-Bus signal.
public ValueTask<IDisposable> WatchSignalAsync<T>(string? sender, string? path, string @interface, string signal, MessageValueReader<T> reader, Action<Notification<T>> handler, ObserverFlags flags, bool emitOnCapturedContext = true, object? state = null)
Parameters
senderstringThe sender to match (optional).
pathstringThe object path to match (optional).
interfacestringThe interface name.
signalstringThe signal name.
readerMessageValueReader<T>Delegate to read values from matched messages.
handlerAction<Notification<T>>Callback invoked for matched signals and completion notifications.
flagsObserverFlagsObserver behavior flags.
emitOnCapturedContextboolWhether to invoke the handler on the current synchronization context.
stateobjectOptional state object.
Returns
- ValueTask<IDisposable>
IDisposable that stops the observer when disposed.
Type Parameters
T
Remarks
The handler must not throw exceptions. If the handler throws, the connection will be disconnected.