| File: Realtime\RealtimeClientSessionExtensions.cs | |
| Project: ..\..\..\src\Libraries\Microsoft.Extensions.AI\Microsoft.Extensions.AI.csproj (Microsoft.Extensions.AI) |
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System; using System.Diagnostics.CodeAnalysis; using Microsoft.Shared.DiagnosticIds; using Microsoft.Shared.Diagnostics; namespace Microsoft.Extensions.AI; /// <summary>Provides a collection of static methods for extending <see cref="IRealtimeClientSession"/> instances.</summary> [Experimental(DiagnosticIds.Experiments.AIRealTime, UrlFormat = DiagnosticIds.UrlFormat)] public static class RealtimeClientSessionExtensions { /// <summary>Asks the <see cref="IRealtimeClientSession"/> for an object of type <typeparamref name="TService"/>.</summary> /// <typeparam name="TService">The type of the object to be retrieved.</typeparam> /// <param name="session">The session.</param> /// <param name="serviceKey">An optional key that can be used to help identify the target service.</param> /// <returns>The found object, otherwise <see langword="null"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="session"/> is <see langword="null"/>.</exception> /// <remarks> /// The purpose of this method is to allow for the retrieval of strongly typed services that may be provided by the <see cref="IRealtimeClientSession"/>, /// including itself or any services it might be wrapping. /// </remarks> public static TService? GetService<TService>(this IRealtimeClientSession session, object? serviceKey = null) { _ = Throw.IfNull(session); return session.GetService(typeof(TService), serviceKey) is TService service ? service : default; } /// <summary> /// Asks the <see cref="IRealtimeClientSession"/> for an object of the specified type <paramref name="serviceType"/> /// and throws an exception if one isn't available. /// </summary> /// <param name="session">The session.</param> /// <param name="serviceType">The type of object being requested.</param> /// <param name="serviceKey">An optional key that can be used to help identify the target service.</param> /// <returns>The found object.</returns> /// <exception cref="ArgumentNullException"><paramref name="session"/> is <see langword="null"/>.</exception> /// <exception cref="ArgumentNullException"><paramref name="serviceType"/> is <see langword="null"/>.</exception> /// <exception cref="InvalidOperationException">No service of the requested type for the specified key is available.</exception> /// <remarks> /// The purpose of this method is to allow for the retrieval of services that are required to be provided by the <see cref="IRealtimeClientSession"/>, /// including itself or any services it might be wrapping. /// </remarks> public static object GetRequiredService(this IRealtimeClientSession session, Type serviceType, object? serviceKey = null) { _ = Throw.IfNull(session); _ = Throw.IfNull(serviceType); return session.GetService(serviceType, serviceKey) ?? throw Throw.CreateMissingServiceException(serviceType, serviceKey); } /// <summary> /// Asks the <see cref="IRealtimeClientSession"/> for an object of type <typeparamref name="TService"/> /// and throws an exception if one isn't available. /// </summary> /// <typeparam name="TService">The type of the object to be retrieved.</typeparam> /// <param name="session">The session.</param> /// <param name="serviceKey">An optional key that can be used to help identify the target service.</param> /// <returns>The found object.</returns> /// <exception cref="ArgumentNullException"><paramref name="session"/> is <see langword="null"/>.</exception> /// <exception cref="InvalidOperationException">No service of the requested type for the specified key is available.</exception> /// <remarks> /// The purpose of this method is to allow for the retrieval of strongly typed services that are required to be provided by the <see cref="IRealtimeClientSession"/>, /// including itself or any services it might be wrapping. /// </remarks> public static TService GetRequiredService<TService>(this IRealtimeClientSession session, object? serviceKey = null) { _ = Throw.IfNull(session); if (session.GetService(typeof(TService), serviceKey) is not TService service) { throw Throw.CreateMissingServiceException(typeof(TService), serviceKey); } return service; } }