| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using IceRpc.Transports; |
| | 4 | | using IceRpc.Transports.Tcp; |
| | 5 | | using Microsoft.Extensions.DependencyInjection; |
| | 6 | | using Microsoft.Extensions.Logging; |
| | 7 | | using Microsoft.Extensions.Options; |
| | 8 | |
|
| | 9 | | namespace IceRpc.Extensions.DependencyInjection; |
| | 10 | |
|
| | 11 | | /// <summary>Provides an extension method for <see cref="IServiceCollection" /> to add a connection cache.</summary> |
| | 12 | | public static class ConnectionCacheServiceCollectionExtensions |
| | 13 | | { |
| | 14 | | /// <summary>Adds a <see cref="ConnectionCache" /> and an <see cref="IInvoker" /> to this service collection. |
| | 15 | | /// </summary> |
| | 16 | | /// <param name="services">The service collection to add services to.</param> |
| | 17 | | /// <returns>The service collection.</returns> |
| | 18 | | /// <remarks>This method uses the connection cache options provided by the <see cref="IOptions{T}" /> of |
| | 19 | | /// <see cref="ConnectionCacheOptions" />.</remarks> |
| | 20 | | /// <example> |
| | 21 | | /// The following code adds a ConnectionCache singleton to the service collection. |
| | 22 | | /// <code source="../../docfx/examples/IceRpc.Extensions.DependencyInjection.Examples/AddIceRpcConnectionCacheExampl |
| | 23 | | /// region="DefaultConnectionCache" lang="csharp" /> |
| | 24 | | /// The resulting singleton is a default connection cache. If you want to customize this connection cache, add an |
| | 25 | | /// <see cref="IOptions{T}" /> of <see cref="ConnectionCacheOptions" /> to your DI container: |
| | 26 | | /// <code source="../../docfx/examples/IceRpc.Extensions.DependencyInjection.Examples/AddIceRpcConnectionCacheExampl |
| | 27 | | /// region="ConnectionCacheWithOptions" lang="csharp" /> |
| | 28 | | /// You can also inject a client transport: |
| | 29 | | /// <list type="bullet"> |
| | 30 | | /// <item><description>an <see cref="IDuplexClientTransport" /> for the ice protocol</description></item> |
| | 31 | | /// <item><description>an <see cref="IMultiplexedClientTransport" /> for the icerpc protocol</description></item> |
| | 32 | | /// </list> |
| | 33 | | /// The following example shows a connection cache that uses QUIC for icerpc connections and keeps the default |
| | 34 | | /// duplex transport (tcp) for ice connections. |
| | 35 | | /// <code source="../../docfx/examples/IceRpc.Extensions.DependencyInjection.Examples/AddIceRpcConnectionCacheExampl |
| | 36 | | /// region="ConnectionCacheWithQuic" |
| | 37 | | /// lang="csharp" /> |
| | 38 | | /// If you want to customize the options of the default transport (tcp), you just need to inject an |
| | 39 | | /// <see cref="IOptions{T}" /> of <see cref="TcpClientTransportOptions" />. |
| | 40 | | /// </example> |
| | 41 | | public static IServiceCollection AddIceRpcConnectionCache(this IServiceCollection services) => |
| 4 | 42 | | services |
| 4 | 43 | | .TryAddIceRpcClientTransport() |
| 4 | 44 | | .AddSingleton(provider => |
| 4 | 45 | | new ConnectionCache( |
| 4 | 46 | | provider.GetRequiredService<IOptions<ConnectionCacheOptions>>().Value, |
| 4 | 47 | | provider.GetRequiredService<IDuplexClientTransport>(), |
| 4 | 48 | | provider.GetRequiredService<IMultiplexedClientTransport>(), |
| 4 | 49 | | provider.GetService<ILogger<ConnectionCache>>())) |
| 4 | 50 | | .AddSingleton<IInvoker>(provider => provider.GetRequiredService<ConnectionCache>()); |
| | 51 | | } |