private static IHubConnectionBuilder WithUrlCore(this IHubConnectionBuilder hubConnectionBuilder, Uri url, HttpTransportType?transports, Action <HttpConnectionOptions>?configureHttpConnection)
    {
        if (hubConnectionBuilder == null)
        {
            throw new ArgumentNullException(nameof(hubConnectionBuilder));
        }

        hubConnectionBuilder.Services.Configure <HttpConnectionOptions>(o =>
        {
            o.Url = url;
            if (transports != null)
            {
                o.Transports = transports.Value;
            }
        });

        if (configureHttpConnection != null)
        {
            hubConnectionBuilder.Services.Configure(configureHttpConnection);
        }

        // Add HttpConnectionOptionsDerivedHttpEndPoint so HubConnection can read the Url from HttpConnectionOptions
        // without the Signal.Client.Core project taking a new dependency on Http.Connections.Client.
        hubConnectionBuilder.Services.AddSingleton <EndPoint, HttpConnectionOptionsDerivedHttpEndPoint>();

        // Configure the HttpConnection so that it uses the correct transfer format for the configured IHubProtocol.
        hubConnectionBuilder.Services.AddSingleton <IConfigureOptions <HttpConnectionOptions>, HubProtocolDerivedHttpOptionsConfigurer>();

        // If and when HttpConnectionFactory is made public, it can be moved out of this assembly and into Http.Connections.Client.
        hubConnectionBuilder.Services.AddSingleton <IConnectionFactory, HttpConnectionFactory>();
        return(hubConnectionBuilder);
    }
        public static IHubConnectionBuilder WithUrlBlazor(this IHubConnectionBuilder hubConnectionBuilder, Uri url, IJSRuntime jsRuntime,
                                                          HttpTransportType?transports = null, Action <BlazorHttpConnectionOptions> options = null)
        {
            if (hubConnectionBuilder == null)
            {
                throw new ArgumentNullException(nameof(hubConnectionBuilder));
            }

            if (jsRuntime == null)
            {
                throw new ArgumentNullException(nameof(jsRuntime));
            }

            hubConnectionBuilder.Services.Configure <BlazorHttpConnectionOptions>(o =>
            {
                o.Url = url;
                if (!transports.HasValue)
                {
                    return;
                }
                o.Transports = transports.Value;
            });
            if (options != null)
            {
                hubConnectionBuilder.Services.Configure(options);
            }

            hubConnectionBuilder.Services.AddSingleton <EndPoint, BlazorHttpConnectionOptionsDerivedHttpEndPoint>();

            hubConnectionBuilder.Services.AddSingleton <IConfigureOptions <BlazorHttpConnectionOptions>, BlazorHubProtocolDerivedHttpOptionsConfigurer>();

            hubConnectionBuilder.Services.AddSingleton(provider => BuildBlazorHttpConnectionFactory(provider, jsRuntime));
            return(hubConnectionBuilder);
        }
Пример #3
0
        public static IHubConnectionBuilder WithEndPoint(this IHubConnectionBuilder builder, EndPoint endPoint)
        {
            builder.Services.AddSingleton <IConnectionFactory, TcpConnectionFactory>();
            builder.Services.AddSingleton(endPoint);

            return(builder);
        }
        public static IHubConnectionBuilder WithUrl(this IHubConnectionBuilder hubConnectionBuilder, Uri url)
        {
            if (url == null)
            {
                throw new ArgumentNullException(nameof(url));
            }

            hubConnectionBuilder.ConfigureConnectionFactory(() =>
            {
                var headers     = hubConnectionBuilder.GetHeaders();
                var httpOptions = new HttpOptions
                {
                    HttpMessageHandler    = hubConnectionBuilder.GetMessageHandler(),
                    Headers               = headers != null ? new ReadOnlyDictionary <string, string>(headers) : null,
                    JwtBearerTokenFactory = hubConnectionBuilder.GetJwtBearerTokenFactory(),
                    WebSocketOptions      = hubConnectionBuilder.GetWebSocketOptions(),
                };

                return(new HttpConnection(url,
                                          hubConnectionBuilder.GetTransport(),
                                          hubConnectionBuilder.GetLoggerFactory(),
                                          httpOptions));
            });
            return(hubConnectionBuilder);
        }
Пример #5
0
        public static IHubConnectionBuilder WithLogger(this IHubConnectionBuilder hubConnectionBuilder, Action <ILoggerFactory> configureLogging)
        {
            var loggerFactory = hubConnectionBuilder.GetLoggerFactory() ?? new LoggerFactory();

            configureLogging(loggerFactory);
            return(hubConnectionBuilder.WithLoggerFactory(loggerFactory));
        }
Пример #6
0
 private void CreateConnectionBuilder()
 {
     hubConnectionBuilder = new HubConnectionBuilder()
                            .WithUrl(serverUrl + "/signalr/CommandHub", options => { options.Headers = new Dictionary <string, string> {
                                                                                         { "Authorization", String.Format("Bearer {0}", controllerSettings.JwtToken) }
                                                                                     }; });
 }
 public static IHubConnectionBuilder WithConsoleLogger(this IHubConnectionBuilder hubConnectionBuilder, LogLevel logLevel)
 {
     return(hubConnectionBuilder.WithLogger(loggerFactory =>
     {
         loggerFactory.AddConsole(logLevel);
     }));
 }
        public static IHubConnectionBuilder WithUrl(this IHubConnectionBuilder hubConnectionBuilder, Uri url)
        {
            if (url == null)
            {
                throw new ArgumentNullException(nameof(url));
            }

            hubConnectionBuilder.ConfigureConnectionFactory(() =>
            {
                var headers     = hubConnectionBuilder.GetHeaders();
                var httpOptions = new HttpOptions
                {
                    HttpMessageHandler = hubConnectionBuilder.GetMessageHandler(),
                    Headers            = headers != null ? new ReadOnlyDictionary <string, string>(headers) : null,
                    AccessTokenFactory = hubConnectionBuilder.GetAccessTokenFactory(),
                    WebSocketOptions   = hubConnectionBuilder.GetWebSocketOptions(),
                    Cookies            = hubConnectionBuilder.GetCookies(),
                    Proxy = hubConnectionBuilder.GetProxy(),
                    UseDefaultCredentials = hubConnectionBuilder.GetUseDefaultCredentials(),
                    ClientCertificates    = hubConnectionBuilder.GetClientCertificates(),
                    Credentials           = hubConnectionBuilder.GetCredentials(),
                };

                return(new HttpConnection(url,
                                          hubConnectionBuilder.GetTransport(),
                                          hubConnectionBuilder.GetLoggerFactory(),
                                          httpOptions));
            });
            return(hubConnectionBuilder);
        }
Пример #9
0
        public static IObservable <FSharpOption <HubConnection> > Observe(
            this IHubConnectionBuilder hubConnectionBuilder
            )
        {
            return(Observable.Create <FSharpOption <HubConnection> >(
                       async(observer, cancellationToken) =>
            {
                HubConnection hubConnection = default;

                try
                {
                    observer.OnNext(FSharpOption <HubConnection> .None);

                    hubConnection = hubConnectionBuilder.Build();

                    hubConnection.Closed += exception =>
                    {
                        if (exception is Exception e)
                        {
                            observer.OnError(e);
                        }
                        else
                        {
                            observer.OnCompleted();
                        }

                        return Task.CompletedTask;
                    };

                    await hubConnection.StartAsync(cancellationToken);

                    observer.OnNext(FSharpOption <HubConnection> .Some(hubConnection));
                }
                catch (Exception e)
                {
                    try
                    {
                        await hubConnection?.DisposeAsync();
                    }
                    catch
                    {
                    }

                    observer.OnError(e);
                }

                return () =>
                {
                    try
                    {
                        hubConnection?.DisposeAsync();
                    }
                    catch
                    {
                    }
                };
            }
                       ));
        }
Пример #10
0
 /// <summary>
 /// Creates a new instance of <see cref="ChatClient"/>.
 /// </summary>
 /// <param name="builder">This <see cref="IHubConnectionBuilder"/> will be used to create the <see cref="HubConnection"/> to the server.</param>
 /// <param name="logger">This <see cref="ILogger"/> will become the <see cref="ChatClient"/>'s <see cref="logger"/>.</param>
 /// <param name="mapper">This <see cref="IMapper"/> will become the <see cref="ChatClient"/>'s <see cref="mapper"/>.</param>
 /// <param name="options">This <see cref="AppSettings"/> will become the <see cref="ChatClient"/>'s <see cref="options"/>.</param>
 public ChatClient(IHubConnectionBuilder builder, ILogger <ChatClient> logger, IMapper mapper, IOptionsMonitor <AppSettings> options)
 {
     this.builder = builder ?? throw new ArgumentNullException(nameof(builder));
     this.logger  = logger ?? throw new ArgumentNullException(nameof(logger));
     this.mapper  = mapper ?? throw new ArgumentNullException(nameof(mapper));
     this.options = options ?? throw new ArgumentNullException(nameof(options));
     messages     = new ConcurrentDictionary <long, ChatMessage>();
 }
        public static ICredentials GetCredentials(this IHubConnectionBuilder hubConnectionBuilder)
        {
            if (hubConnectionBuilder.TryGetSetting <ICredentials>(CredentialsKey, out var credentials))
            {
                return(credentials);
            }

            return(null);
        }
        public static CookieContainer GetCookies(this IHubConnectionBuilder hubConnectionBuilder)
        {
            if (hubConnectionBuilder.TryGetSetting <CookieContainer>(CookiesKey, out var cookies))
            {
                return(cookies);
            }

            return(null);
        }
        public static bool?GetUseDefaultCredentials(this IHubConnectionBuilder hubConnectionBuilder)
        {
            if (hubConnectionBuilder.TryGetSetting <bool?>(UseDefaultCredentialsKey, out var useDefaultCredentials))
            {
                return(useDefaultCredentials);
            }

            return(null);
        }
        public static IWebProxy GetProxy(this IHubConnectionBuilder hubConnectionBuilder)
        {
            if (hubConnectionBuilder.TryGetSetting <IWebProxy>(ProxyKey, out var proxy))
            {
                return(proxy);
            }

            return(null);
        }
            static void ConfigureHubConnection(
                IHubConnectionBuilder hubConnectionBuilder,
                IServiceProvider serviceProvider)
            {
                var jsRuntime         = serviceProvider.GetRequiredService <IJSRuntime>();
                var navigationManager = serviceProvider.GetRequiredService <NavigationManager>();

                hubConnectionBuilder.WithUrlBlazor(new Uri(DefaultHubUrl, UriKind.Relative), jsRuntime, navigationManager);
            }
Пример #16
0
 public MainPageViewModel(IHubConnectionBuilder builder, IDispatcher dispatcher, IAlertMessageService alertService)
 {
     SendMessage  = new DelegateCommand <string>(DoSendMessage);
     Connect      = new DelegateCommand(DoConnect);
     Disconnect   = new DelegateCommand(DoDisconnect);
     Connection   = builder.Build();
     Dispatcher   = dispatcher;
     AlertService = alertService;
 }
        public static X509CertificateCollection GetClientCertificates(this IHubConnectionBuilder hubConnectionBuilder)
        {
            if (hubConnectionBuilder.TryGetSetting <X509CertificateCollection>(ClientCertificatesKey, out var clientCertificates))
            {
                return(clientCertificates);
            }

            return(null);
        }
 public static IHubConnectionBuilder WithConnectionFactory(this IHubConnectionBuilder hubConnectionBuilder, Func <TransferFormat, Task <ConnectionContext> > connectionFactory)
 {
     if (connectionFactory == null)
     {
         throw new ArgumentNullException(nameof(connectionFactory));
     }
     hubConnectionBuilder.Services.AddSingleton <IConnectionFactory>(new DelegateConnectionFactory(connectionFactory));
     return(hubConnectionBuilder);
 }
Пример #19
0
        public static IHubConnectionBuilder WithMessagePackProtocol(this IHubConnectionBuilder hubConnectionBuilder, SerializationContext serializationContext)
        {
            if (serializationContext == null)
            {
                throw new ArgumentNullException(nameof(serializationContext));
            }

            return(hubConnectionBuilder.WithHubProtocol(new MessagePackHubProtocol(serializationContext)));
        }
        public static IHubConnectionBuilder WithUrl(this IHubConnectionBuilder hubConnectionBuilder, string url)
        {
            if (url == null)
            {
                throw new ArgumentNullException(nameof(url));
            }

            return(hubConnectionBuilder.WithUrl(new Uri(url)));
        }
        public static Func <string> GetJwtBearerTokenFactory(this IHubConnectionBuilder hubConnectionBuilder)
        {
            if (hubConnectionBuilder.TryGetSetting <Func <string> >(JwtBearerTokenFactoryKey, out var factory))
            {
                return(factory);
            }

            return(null);
        }
        public static IDictionary <string, string> GetHeaders(this IHubConnectionBuilder hubConnectionBuilder)
        {
            if (hubConnectionBuilder.TryGetSetting <IDictionary <string, string> >(HeadersKey, out var headers))
            {
                return(headers);
            }

            return(null);
        }
        public static TransportType GetTransport(this IHubConnectionBuilder hubConnectionBuilder)
        {
            if (hubConnectionBuilder.TryGetSetting <TransportType>(TransportTypeKey, out var transportType))
            {
                return(transportType);
            }

            return(TransportType.All);
        }
Пример #24
0
 public static IHubConnectionBuilder WithUrlBlazor(
     this IHubConnectionBuilder hubConnectionBuilder,
     string url,
     IJSRuntime jsRuntime,
     NavigationManager navigationManager,
     HttpTransportType?transports = null,
     Action <BlazorHttpConnectionOptions>?options = null)
 {
     return(WithUrlBlazor(hubConnectionBuilder, new Uri(url), jsRuntime, navigationManager, transports, options));
 }
    // Tests want to override the built in LoggerFactory, it internally calls AddLogging
    // https://github.com/aspnet/Logging/blob/671af986ec3b46dc81e28e4a6c37a9d0ee283c65/src/Microsoft.Extensions.Logging.Testing/AssemblyTestLog.cs#L130
    public static IHubConnectionBuilder WithLoggerFactory(this IHubConnectionBuilder hubConnectionBuilder, ILoggerFactory loggerFactory)
    {
        if (loggerFactory == null)
        {
            throw new ArgumentNullException(nameof(loggerFactory));
        }

        hubConnectionBuilder.Services.AddSingleton(loggerFactory);
        return(hubConnectionBuilder);
    }
Пример #26
0
        public static async Task <IHubConnectionBuilder> WithUrl(this IHubConnectionBuilder builder, Uri url,
                                                                 string email, string password, Action <HttpConnectionOptions> configureHttpConnection = null)
        {
            var cookies = await Login(email, password);

            return(builder.WithUrl(url, options => {
                options.Cookies.Add(cookies);
                configureHttpConnection?.Invoke(options);
            }));
        }
        public static IHubConnectionBuilder WithJwtBearer(this IHubConnectionBuilder hubConnectionBuilder, Func <string> jwtBearerTokenFactory)
        {
            if (jwtBearerTokenFactory == null)
            {
                throw new ArgumentNullException(nameof(jwtBearerTokenFactory));
            }

            hubConnectionBuilder.AddSetting(JwtBearerTokenFactoryKey, jwtBearerTokenFactory);

            return(hubConnectionBuilder);
        }
        public static IHubConnectionBuilder WithAccessToken(this IHubConnectionBuilder hubConnectionBuilder, Func <string> accessTokenFactory)
        {
            if (accessTokenFactory == null)
            {
                throw new ArgumentNullException(nameof(accessTokenFactory));
            }

            hubConnectionBuilder.AddSetting(AccessTokenFactoryKey, accessTokenFactory);

            return(hubConnectionBuilder);
        }
        public static IHubConnectionBuilder WithWebSocketOptions(this IHubConnectionBuilder hubConnectionBuilder, Action <ClientWebSocketOptions> configureWebSocketOptions)
        {
            if (configureWebSocketOptions == null)
            {
                throw new ArgumentNullException(nameof(configureWebSocketOptions));
            }

            hubConnectionBuilder.AddSetting(WebSocketOptionsKey, configureWebSocketOptions);

            return(hubConnectionBuilder);
        }
        public static IHubConnectionBuilder WithClientBuilder(this IHubConnectionBuilder hubConnectionBuilder, EndPoint endPoint, Action <ClientBuilder> configure)
        {
            hubConnectionBuilder.Services.AddSingleton <IConnectionFactory>(sp =>
            {
                var builder = new ClientBuilder(sp);
                configure(builder);
                return(builder.Build());
            });

            hubConnectionBuilder.Services.AddSingleton(endPoint);
            return(hubConnectionBuilder);
        }