Class NearbyConnectivityServiceCollectionExtensions
- Namespace
- Plugin.Maui.NearbyConnectivity
- Assembly
- Plugin.Maui.NearbyConnectivity.dll
Provides extension methods for adding nearby connectivity to an IServiceCollection.
public static class NearbyConnectivityServiceCollectionExtensions
- Inheritance
-
NearbyConnectivityServiceCollectionExtensions
- Inherited Members
Methods
AddNearbyConnectivity(IServiceCollection, Action<NearbyOptions>)
Registers INearbyConnectivity and its configuration with the service collection.
public static IServiceCollection AddNearbyConnectivity(this IServiceCollection services, Action<NearbyOptions> configure)
Parameters
servicesIServiceCollectionThe IServiceCollection to add the services to.
configureAction<NearbyOptions>A delegate that configures NearbyOptions. It must set ServiceId, which has no default.
Returns
- IServiceCollection
The same IServiceCollection instance, so that multiple calls can be chained.
Examples
The following example registers the plugin on a bare service collection and sets the service identifier.
services.AddNearbyConnectivity(options =>
{
options.ServiceId = "nearbychat";
});
Remarks
INearbyConnectivity is registered as a singleton, because the underlying resources are singular: one radio and one native session per device. It is constructed lazily, on first resolution.
configure is applied and validated synchronously, before this method
returns, so an unusable ServiceId fails at the call site.
The validated snapshot is registered as a singleton NearbyOptions. The
configure delegate is the only way to supply options: configuration
binding and IOptions<T> do not apply.
The first registration wins. A second call still validates its options, then registers nothing, so its ServiceId and every other option are ignored.
A MAUI application calls AddNearbyConnectivity(MauiAppBuilder, Action<NearbyOptions>) on the builder instead, which forwards to this method. This method is for a host that has no MauiAppBuilder, such as a unit test.
Logging is optional. A host that never called AddLogging() still works, with log
output discarded. A TimeProvider is registered here if the host has not
registered one.
Neither advertising nor discovery starts automatically. Call StartAsync(CancellationToken) on Advertiser or Discoverer once the application is ready and NearbyPermission has been granted.
An application that resolves this service late still sees every connection already open and every request still outstanding, in Devices.
Exceptions
- ArgumentNullException
servicesorconfigureis null.- ArgumentException
The configured NearbyOptions is unusable. Leaving ServiceId unset throws, and the message suggests a valid identifier derived from the application's own name. A malformed value throws too: empty, longer than 15 characters, containing anything but ASCII lowercase letters, digits and hyphens, or written in the
_name._tcpBonjour form.
- See Also