// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using Microsoft.AspNetCore.Http.Connections; using Microsoft.AspNetCore.Http.Connections.Client; using Microsoft.Extensions.DependencyInjection; namespace Microsoft.AspNetCore.SignalR.Client { /// /// Extension methods for . /// public static class HubConnectionBuilderHttpExtensions { /// /// Configures the to use HTTP-based transports to connect to the specified URL. /// /// The to configure. /// The URL the will use. /// The same instance of the for chaining. public static IHubConnectionBuilder WithUrl(this IHubConnectionBuilder hubConnectionBuilder, string url) { hubConnectionBuilder.WithUrlCore(new Uri(url), null, null); return hubConnectionBuilder; } /// /// Configures the to use HTTP-based transports to connect to the specified URL. /// /// The to configure. /// The URL the will use. /// The delegate that configures the . /// The same instance of the for chaining. public static IHubConnectionBuilder WithUrl(this IHubConnectionBuilder hubConnectionBuilder, string url, Action configureHttpConnection) { hubConnectionBuilder.WithUrlCore(new Uri(url), null, configureHttpConnection); return hubConnectionBuilder; } /// /// Configures the to use HTTP-based transports to connect to the specified URL and transports. /// /// The to configure. /// The URL the will use. /// A bitmask comprised of one or more that specify what transports the client should use. /// The same instance of the for chaining. public static IHubConnectionBuilder WithUrl(this IHubConnectionBuilder hubConnectionBuilder, string url, HttpTransportType transports) { hubConnectionBuilder.WithUrlCore(new Uri(url), transports, null); return hubConnectionBuilder; } /// /// Configures the to use HTTP-based transports to connect to the specified URL and transports. /// /// The to configure. /// The URL the will use. /// A bitmask comprised of one or more that specify what transports the client should use. /// The delegate that configures the . /// The same instance of the for chaining. public static IHubConnectionBuilder WithUrl(this IHubConnectionBuilder hubConnectionBuilder, string url, HttpTransportType transports, Action configureHttpConnection) { hubConnectionBuilder.WithUrlCore(new Uri(url), transports, configureHttpConnection); return hubConnectionBuilder; } /// /// Configures the to use HTTP-based transports to connect to the specified URL. /// /// The to configure. /// The URL the will use. /// The same instance of the for chaining. public static IHubConnectionBuilder WithUrl(this IHubConnectionBuilder hubConnectionBuilder, Uri url) { hubConnectionBuilder.WithUrlCore(url, null, null); return hubConnectionBuilder; } /// /// Configures the to use HTTP-based transports to connect to the specified URL. /// /// The to configure. /// The URL the will use. /// The delegate that configures the . /// The same instance of the for chaining. public static IHubConnectionBuilder WithUrl(this IHubConnectionBuilder hubConnectionBuilder, Uri url, Action configureHttpConnection) { hubConnectionBuilder.WithUrlCore(url, null, configureHttpConnection); return hubConnectionBuilder; } /// /// Configures the to use HTTP-based transports to connect to the specified URL and transports. /// /// The to configure. /// The URL the will use. /// A bitmask comprised of one or more that specify what transports the client should use. /// The same instance of the for chaining. public static IHubConnectionBuilder WithUrl(this IHubConnectionBuilder hubConnectionBuilder, Uri url, HttpTransportType transports) { hubConnectionBuilder.WithUrlCore(url, transports, null); return hubConnectionBuilder; } /// /// Configures the to use HTTP-based transports to connect to the specified URL and transports. /// /// The to configure. /// The URL the will use. /// A bitmask comprised of one or more that specify what transports the client should use. /// The delegate that configures the . /// The same instance of the for chaining. public static IHubConnectionBuilder WithUrl(this IHubConnectionBuilder hubConnectionBuilder, Uri url, HttpTransportType transports, Action configureHttpConnection) { hubConnectionBuilder.WithUrlCore(url, transports, configureHttpConnection); return hubConnectionBuilder; } private static IHubConnectionBuilder WithUrlCore(this IHubConnectionBuilder hubConnectionBuilder, Uri url, HttpTransportType? transports, Action configureHttpConnection) { if (hubConnectionBuilder == null) { throw new ArgumentNullException(nameof(hubConnectionBuilder)); } hubConnectionBuilder.Services.Configure(o => { o.Url = url; if (transports != null) { o.Transports = transports.Value; } }); if (configureHttpConnection != null) { hubConnectionBuilder.Services.Configure(configureHttpConnection); } hubConnectionBuilder.Services.AddSingleton(); return hubConnectionBuilder; } } }