// 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 System.ComponentModel; using Microsoft.Extensions.DependencyInjection; namespace Microsoft.AspNetCore.SignalR.Client { /// /// A builder for configuring instances. /// public class HubConnectionBuilder : IHubConnectionBuilder { private bool _hubConnectionBuilt; /// public IServiceCollection Services { get; } /// /// Initializes a new instance of the class. /// public HubConnectionBuilder() { Services = new ServiceCollection(); Services.AddSingleton(); Services.AddLogging(); this.AddJsonProtocol(); } /// public HubConnection Build() { // Build can only be used once if (_hubConnectionBuilt) { throw new InvalidOperationException("HubConnectionBuilder allows creation only of a single instance of HubConnection."); } _hubConnectionBuilt = true; // The service provider is disposed by the HubConnection var serviceProvider = Services.BuildServiceProvider(); var connectionFactory = serviceProvider.GetService(); if (connectionFactory == null) { throw new InvalidOperationException($"Cannot create {nameof(HubConnection)} instance. An {nameof(IConnectionFactory)} was not configured."); } return serviceProvider.GetService(); } // Prevents from being displayed in intellisense /// [EditorBrowsable(EditorBrowsableState.Never)] public override int GetHashCode() { return base.GetHashCode(); } // Prevents from being displayed in intellisense /// [EditorBrowsable(EditorBrowsableState.Never)] public override bool Equals(object obj) { return base.Equals(obj); } // Prevents from being displayed in intellisense /// [EditorBrowsable(EditorBrowsableState.Never)] public override string ToString() { return base.ToString(); } // Prevents from being displayed in intellisense [EditorBrowsable(EditorBrowsableState.Never)] public new Type GetType() { return base.GetType(); } } }