public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); services.Configure <BackendSettings>(Configuration.GetSection("Backend")); services.Configure <AzureSettings>(Configuration.GetSection("Azure")); var configureAction = new Action <IServiceProvider, GrpcClientFactoryOptions>((provider, options) => { var settings = provider.GetRequiredService <IOptionsMonitor <BackendSettings> >(); options.Address = new Uri(settings.CurrentValue.Address); Console.WriteLine($"Backend address from configuration: {options.Address}"); }); services.AddSingleton(provider => { var configSettings = provider.GetRequiredService <IOptionsMonitor <AzureSettings> >(); var serviceManager = new ServiceManagerBuilder() .WithOptions(option => { option.ConnectionString = configSettings.CurrentValue.SignalR.ConnectionString; }) .Build(); return((IServiceManager)serviceManager); }); services.AddSingleton(new HttpClient { BaseAddress = new Uri("https://api.covid19api.com") }); services.AddSingleton <Covid19ApiClient>(); services.AddGrpcClient <CovidClient>(configureAction); services.AddGrpcClient <StocksClient>(configureAction); }
public void GetClientConnectionInfo() { var hubName = "TestHub"; var hubUrl = "http://localhost"; var accessKey = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; var connectionString = $"Endpoint={hubUrl};AccessKey={accessKey};Version=1.0;"; var userId = "User"; var idToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"; var expectedName = "John Doe"; var expectedIat = "1516239022"; var claimTypeList = new string[] { "name", "iat" }; var serviceManager = new ServiceManagerBuilder() .WithOptions(o => { o.ConnectionString = connectionString; }) .Build(); var serviceHubContextStore = new ServiceHubContextStore(serviceManager, null); var azureSignalRClient = new AzureSignalRClient(serviceHubContextStore, serviceManager); var connectionInfo = azureSignalRClient.GetClientConnectionInfo(hubName, userId, idToken, claimTypeList); Assert.Equal(connectionInfo.Url, $"{hubUrl}/client/?hub={hubName.ToLower()}"); var claims = new JwtSecurityTokenHandler().ReadJwtToken(connectionInfo.AccessToken).Claims; Assert.Equal(expectedName, GetClaimValue(claims, "name")); Assert.Equal(expectedIat, GetClaimValue(claims, $"{AzureSignalRClient.AzureSignalRUserPrefix}iat")); }
public async Task Call_NegotiateAsync_After_WithEndpoints(ServiceTransportType serviceTransportType) { var serviceManager = new ServiceManagerBuilder() .WithOptions(o => { o.ServiceTransportType = serviceTransportType; o.ServiceEndpoints = ServiceEndpoints; }) .BuildServiceManager(); var hubContext = await serviceManager.CreateHubContextAsync(Hub, default); for (var i = 0; i < 5; i++) { var randomEndpoint = ServiceEndpoints[StaticRandom.Next(0, Count)]; var negotiationResponse = await(hubContext as IInternalServiceHubContext) .WithEndpoints(new ServiceEndpoint[] { randomEndpoint }) .NegotiateAsync(); Assert.Equal(ClientEndpointUtils.GetExpectedClientEndpoint(Hub, null, randomEndpoint.Endpoint), negotiationResponse.Url); var tokenString = negotiationResponse.AccessToken; var token = JwtTokenHelper.JwtHandler.ReadJwtToken(tokenString); var expectedToken = JwtTokenHelper.GenerateJwtBearer( ClientEndpointUtils.GetExpectedClientEndpoint(Hub, null, randomEndpoint.Endpoint), ClaimsUtility.BuildJwtClaims(null, null, null), token.ValidTo, token.ValidFrom, token.ValidFrom, randomEndpoint.AccessKey); Assert.Equal(expectedToken, tokenString); } }
private async Task DirectConnectionJoinGroup(string connectionString) { var serviceManager = new ServiceManagerBuilder().WithOptions(option => { option.ConnectionString = connectionString; option.ServiceTransportType = ServiceTransportType.Transient; }).Build(); var hubContext = await serviceManager.CreateHubContextAsync(SignalRConstants.DefaultRestHubName); await SignalRUtils.JoinGroupForConnection( _totalConnection, _groupCount, _connectionIndex, async (i, g) => { var userId = SignalRUtils.GenClientUserIdFromConnectionIndex(_connectionIndex[i]); try { await hubContext.UserGroups.AddToGroupAsync( userId, SignalRUtils.GroupName(_type, g)); _statisticsCollector.IncreaseJoinGroupSuccess(); } catch (Exception e) { _statisticsCollector.IncreaseJoinGroupFail(); Log.Error($"Fail to join group: {e.Message}"); } }); }
public static void AddAzureSignalRIntegration(this IServiceCollection services, IConfiguration configuration) { string connectionString = configuration.GetConnectionString("SignalR"); var serviceManager = new ServiceManagerBuilder() .WithOptions(o => o.ConnectionString = connectionString) .Build(); services.AddSingleton(serviceManager); }
public async Task CheckServiceHealthTest() { var serviceManager = new ServiceManagerBuilder() .WithOptions(o => { o.ConnectionString = TestConfiguration.Instance.ConnectionString; }) .Build(); var isHealthy = await serviceManager.IsServiceHealthy(default);
internal void GenerateClientEndpointTestWithClientEndpoint() { var manager = new ServiceManagerBuilder().WithOptions(o => { o.ConnectionString = $"Endpoint=http://localhost;AccessKey=ABC;Version=1.0;ClientEndpoint=https://remote"; }).Build(); var clientEndpoint = manager.GetClientEndpoint(HubName); Assert.Equal("https://remote/client/?hub=signalrbench", clientEndpoint); }
public async Task NegotiateAsync() { var serviceManager = new ServiceManagerBuilder().WithOptions(o => o.ConnectionString = FakeEndpointUtils.GetFakeConnectionString(1).Single()).BuildServiceManager(); var hubContext = await serviceManager.CreateHubContextAsync <IChatClient>("hubName", default); var myHub = new TestStronglyTypedHub(hubContext); var connectionInfo = await myHub.Negotiate("user"); Assert.NotNull(connectionInfo); }
public async Task InitAsync() { var serviceManager = new ServiceManagerBuilder().WithOptions(option => { option.ConnectionString = _connectionString; option.ServiceTransportType = _serviceTransportType; }).Build(); _hubContext = await serviceManager.CreateHubContextAsync(HubName, new LoggerFactory()); }
public async Task StartAsync(CancellationToken cancellationToken) { var serviceManager = new ServiceManagerBuilder().WithOptions(option => { option.ConnectionString = _config[ENV_SIGNALR]; }).Build(); _notificationHub = await serviceManager.CreateHubContextAsync(HubName); ConnectToQueue(); }
public static async Task <IActionResult> SubscribeForSessionUpdatesOnSignalR( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "sessions/{sessionId}/signalr/{userId}/subscribe")] HttpRequestMessage reqMsg, string sessionId, string userId, [SignalR(HubName = "%Region%")] IAsyncCollector <SignalRGroupAction> signalRGroupActions, ILogger log) { log.LogInformation("SubscribeForSessionUpdatesOnSignalR was called."); // TODO: check if session with given id exists or not // determine browser session id from cookie: // TODO: this fails, probably because of cross domain restrictions on cookies: var browserSessionIdCookie = reqMsg.Headers.GetCookies(SessionToken).FirstOrDefault(); var browserSessionId = System.Guid.NewGuid().ToString(); if (browserSessionIdCookie != null && browserSessionIdCookie.Cookies[0] != null) { browserSessionId = browserSessionIdCookie.Cookies[0].Value; log.LogInformation($"got cookie with session id: {browserSessionId}"); } // remove users from all signalr groups: try{ log.LogInformation($"About to remove user with browser session {browserSessionId} from all SignalR groups."); var signalRMgmtBuilder = new ServiceManagerBuilder(); var signalRService = signalRMgmtBuilder.WithOptions(config => { config.ConnectionString = System.Environment.GetEnvironmentVariable("AzureSignalRConnectionString", EnvironmentVariableTarget.Process); }).Build(); var region = System.Environment.GetEnvironmentVariable("Region", EnvironmentVariableTarget.Process); var signalRHub = await signalRService.CreateHubContextAsync(region); await signalRHub.UserGroups.RemoveFromAllGroupsAsync(browserSessionId); log.LogInformation($"Removed user with browser session {browserSessionId} from all SignalR groups."); } catch (Exception e) { log.LogError($"Failed to remove user/browsersession ({browserSessionId}) from all signalr groups: {e.Message}"); } // add user to group for specified session: var groupname = $"listeners-for-session-{sessionId}"; log.LogInformation($"About to add user to group {groupname} for session {sessionId}."); await signalRGroupActions.AddAsync(new SignalRGroupAction { UserId = userId, GroupName = groupname, Action = GroupAction.Add }); log.LogInformation($"Added user with id {userId} and browser session {browserSessionId} to SignalR group for qna session {sessionId} with group name: {groupname}"); var signalRNegotiateBaseUrl = System.Environment.GetEnvironmentVariable("SignalRNegotiateBaseUrl", EnvironmentVariableTarget.Process).Replace("{sessionId}", sessionId); return(new OkWithSessionCookieObjectResult(new { SignalRNegotiateBaseUrl = signalRNegotiateBaseUrl }, browserSessionId)); }
/// <summary> /// Create signalR event bus /// </summary> /// <param name="config"></param> public SignalRServiceEndpoint(ISignalRServiceConfig config) { Resource = NameAttribute.GetName(typeof(THub)); if (!string.IsNullOrEmpty(config?.SignalRConnString) && config.SignalRServerLess) { _serviceManager = new ServiceManagerBuilder().WithOptions(option => { option.ConnectionString = config.SignalRConnString; option.ServiceTransportType = ServiceTransportType.Persistent; }).Build(); } }
public static void AddAzureSignalRIntegration(this IServiceCollection services) { string connectionString = Environment.GetEnvironmentVariable("SignalRConnectionString"); var serviceManager = new ServiceManagerBuilder() .WithOptions(o => { o.ConnectionString = connectionString; }) .Build(); services.AddSingleton(serviceManager); }
public async Task InitAsync() { var serviceManager = new ServiceManagerBuilder().WithOptions(option => { option.ConnectionString = _connectionString; option.ServiceTransportType = _serviceTransportType; }) //Uncomment the following line to get more logs //.WithLoggerFactory(LoggerFactory.Create(builder => builder.AddConsole())) .BuildServiceManager(); _hubContext = await serviceManager.CreateHubContextAsync(HubName, default); }
internal void GenerateClientEndpointTest(string appName, string expectedClientEndpoint) { var builder = new ServiceManagerBuilder() .WithOptions(o => { o.ApplicationName = appName; o.ConnectionString = _testConnectionString; }); var manager = builder.Build(); var clientEndpoint = manager.GetClientEndpoint(HubName); Assert.Equal(expectedClientEndpoint, clientEndpoint); }
/// <summary> /// Create signalR event bus /// </summary> /// <param name="config"></param> /// <param name="logger"></param> public SignalRServiceHost(ISignalRServiceConfig config, ILogger logger) { _logger = logger ?? throw new ArgumentNullException(nameof(logger)); if (string.IsNullOrEmpty(config?.SignalRConnString)) { throw new ArgumentNullException(nameof(config.SignalRConnString)); } _serviceManager = new ServiceManagerBuilder().WithOptions(option => { option.ConnectionString = config.SignalRConnString; option.ServiceTransportType = ServiceTransportType.Persistent; }).Build(); Resource = !string.IsNullOrEmpty(config.SignalRHubName) ? config.SignalRHubName : "default"; }
private async Task DirectConnectionLeaveGroup(string connectionString) { var serviceManager = new ServiceManagerBuilder().WithOptions(option => { option.ConnectionString = connectionString; option.ServiceTransportType = ServiceTransportType.Transient; }).Build(); var hubContext = await serviceManager.CreateHubContextAsync(SignalRConstants.DefaultRestHubName); if (_connections.Count >= _groupCount) { for (var i = 0; i < _connections.Count; i++) { var userId = SignalRUtils.GenClientUserIdFromConnectionIndex(_connectionIndex[i]); try { await hubContext.UserGroups.RemoveFromGroupAsync(userId, SignalRUtils.GroupName(_type, _connectionIndex[i] % _groupCount)); _statisticsCollector.IncreaseLeaveGroupSuccess(); } catch (Exception e) { _statisticsCollector.IncreaseLeaveGroupFail(); Log.Error($"Fail to leave group: {e.Message}"); } } } else { for (var i = 0; i < _groupCount; i++) { var userId = SignalRUtils.GenClientUserIdFromConnectionIndex(i); try { await hubContext.UserGroups.RemoveFromGroupAsync( userId, SignalRUtils.GroupName(_type, i)); _statisticsCollector.IncreaseLeaveGroupSuccess(); } catch (Exception e) { _statisticsCollector.IncreaseLeaveGroupFail(); Log.Error($"Fail to leave group: {e.Message}"); } } } }
private static IServiceManager GenerateServiceManager(string connectionString, ServiceTransportType serviceTransportType = ServiceTransportType.Transient, string appName = null) { var serviceManager = new ServiceManagerBuilder() .WithOptions(opt => { opt.ConnectionString = connectionString; opt.ServiceTransportType = serviceTransportType; opt.ApplicationName = appName; }) .WithCallingAssembly() .Build(); return(serviceManager); }
//[InlineData(ServiceTransportType.Transient)] Not implemented yet public async Task StrongTypedHubContextCheckEndpointHealthWithMultiEndpoints(ServiceTransportType serviceTransportType) { var serviceManager = new ServiceManagerBuilder() .WithOptions(o => { o.ServiceTransportType = serviceTransportType; o.ServiceEndpoints = FakeEndpointUtils.GetFakeEndpoint(2).ToArray(); }) .WithLoggerFactory(_loggerFactory) .BuildServiceManager(); var hubContext = await serviceManager.CreateHubContextAsync <IChat>("hubName", default); await Assert.ThrowsAsync <AzureSignalRNotConnectedException>(() => hubContext.NegotiateAsync().AsTask()); }
private static Task <IServiceHubContext> CreateHubContext() { string signalRConnString = Environment.GetEnvironmentVariable(Constants.AzureSignalRConnectionStringEnvironmentVariableName); var serviceManager = new ServiceManagerBuilder() .WithOptions(option => { option.ConnectionString = signalRConnString; // ServiceTransportType.Persistent would be more efficient, but it is not reliable (connection might be dropped and never reestablished) option.ServiceTransportType = ServiceTransportType.Transient; }) .Build(); return(serviceManager.CreateHubContextAsync(nameof(LetsGoOutHub))); }
static Task <IServiceHubContext> CreateHubContextAsync() { var config = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json").Build(); string signalRConnString = config.GetConnectionString("AzureSignalRConnectionString"); var serviceManager = new ServiceManagerBuilder() .WithOptions(option => { option.ConnectionString = signalRConnString; option.ServiceTransportType = ServiceTransportType.Persistent; }) .Build(); return(serviceManager.CreateHubContextAsync("testhub")); }
internal void GenerateClientAccessTokenTest(string userId, Claim[] claims, string appName) { var builder = new ServiceManagerBuilder() .WithOptions(o => { o.ApplicationName = appName; o.ConnectionString = _testConnectionString; }); var manager = builder.Build(); var tokenString = manager.GenerateClientAccessToken(HubName, userId, claims, _tokenLifeTime); var token = JwtTokenHelper.JwtHandler.ReadJwtToken(tokenString); string expectedToken = JwtTokenHelper.GenerateExpectedAccessToken(token, ClientEndpointUtils.GetExpectedClientEndpoint(HubName, appName), AccessKey, claims); Assert.Equal(expectedToken, tokenString); }
public async Task StrongTypedHubContextNotCheckEndpointHealthWithSingleEndpoint(ServiceTransportType serviceTransportType) { var serviceManager = new ServiceManagerBuilder() .WithOptions(o => { o.ServiceTransportType = serviceTransportType; o.ConnectionString = FakeEndpointUtils.GetFakeConnectionString(1).First(); }) .WithLoggerFactory(_loggerFactory) .BuildServiceManager(); var hubContext = await serviceManager.CreateHubContextAsync <IChat>("hubName", default); var negotiateResponse = await hubContext.NegotiateAsync(); Assert.NotNull(negotiateResponse); }
internal async Task CreateServiceHubContextTest(ServiceTransportType serviceTransportType, bool useLoggerFacory, string appName, int connectionCount) { var builder = new ServiceManagerBuilder() .WithOptions(o => { o.ServiceTransportType = serviceTransportType; o.ApplicationName = appName; o.ConnectionCount = connectionCount; o.ConnectionString = _testConnectionString; }); var serviceManager = builder.Build(); using (var loggerFactory = useLoggerFacory ? (ILoggerFactory) new LoggerFactory() : NullLoggerFactory.Instance) { var hubContext = await serviceManager.CreateHubContextAsync(HubName, default); } }
/// <summary> /// Create signalR event bus /// </summary> /// <param name="config"></param> /// <param name="logger"></param> public SignalRServiceHost(ISignalRServiceConfig config, ILogger logger) { _logger = logger ?? throw new ArgumentNullException(nameof(logger)); if (string.IsNullOrEmpty(config?.SignalRConnString)) { throw new ArgumentNullException(nameof(config.SignalRConnString)); } _serviceManager = new ServiceManagerBuilder().WithOptions(option => { option.ConnectionString = config.SignalRConnString; option.ServiceTransportType = ServiceTransportType.Persistent; }).Build(); Resource = !string.IsNullOrEmpty(config.SignalRHubName) ? config.SignalRHubName : "default"; // TODO : force hub renew mechanism introduced to workaround a // signalR SDK bug. To be removed after the fix is done in the SDK _renewHubTimer = new Timer(RenewHubTimer_ElapesedAsync); _renewHubInterval = TimeSpan.FromMinutes(3); }
static async Task Main(string[] args) { var serviceManager = new ServiceManagerBuilder() .WithOptions(option => { option.ConnectionString = ""; }) .Build(); Console.WriteLine("Press any key to connect"); Console.ReadKey(); var hubContext = await serviceManager.CreateHubContextAsync("TestHub").ConfigureAwait(false); await hubContext.Clients.Group("testGroup").SendAsync("Send", "Hello").ConfigureAwait(false); Console.ReadKey(); await hubContext.DisposeAsync(); }
public static NewhlSecurityPrincipal ParseCookie(HttpCookieCollection cookies) { // Get the authentication cookie string cookieName = FormsAuthentication.FormsCookieName; HttpCookie authCookie = cookies[cookieName]; NewhlSecurityPrincipal retVal = null; IServiceManager serviceManager = ServiceManagerBuilder.CreateServiceManager(); if (authCookie != null) { if (authCookie.Value != string.Empty) { try { // Get the authentication ticket // and rebuild the principal & identity FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value); AMFUserLogin currentUser = serviceManager.UserService.GetUserById(int.Parse(authTicket.Name)); retVal = new NewhlSecurityPrincipal(currentUser); } catch (Exception e) { retVal = new NewhlSecurityPrincipal(null); } } } else { retVal = new NewhlSecurityPrincipal(null); } System.Threading.Thread.CurrentPrincipal = retVal; HttpContext.Current.User = retVal; return(retVal); }
private async Task MockConnectionTestAsync(ServiceTransportType serviceTransportType, Func <ServiceHubContext, Task> testAction, Action <Dictionary <HubServiceEndpoint, List <TestServiceConnection> > > assertAction) { using (StartLog(out var loggerFactory, LogLevel.Debug)) { var connectionFactory = new TestServiceConnectionFactory(); var serviceManager = new ServiceManagerBuilder() .WithOptions(o => { o.ServiceTransportType = serviceTransportType; o.ServiceEndpoints = ServiceEndpoints; }) .WithLoggerFactory(loggerFactory) .ConfigureServices(services => services.AddSingleton <IServiceConnectionFactory>(connectionFactory)) .BuildServiceManager(); var hubContext = await serviceManager.CreateHubContextAsync(Hub, default); await testAction.Invoke(hubContext); var createdConnections = connectionFactory.CreatedConnections.ToDictionary(p => p.Key, p => p.Value.Select(conn => conn as TestServiceConnection).ToList()); assertAction.Invoke(createdConnections); } }
internal async Task StopServiceHubContextTest() { using (StartVerifiableLog(out var loggerFactory, LogLevel.Debug, expectedErrors: context => context.EventId == new EventId(2, "EndpointOffline"))) { var serviceManager = new ServiceManagerBuilder() .WithOptions(o => { o.ConnectionString = TestConfiguration.Instance.ConnectionString; o.ConnectionCount = 1; o.ServiceTransportType = ServiceTransportType.Persistent; }) .Build(); var serviceHubContext = await serviceManager.CreateHubContextAsync("hub", loggerFactory); var connectionContainer = ((ServiceHubContext)serviceHubContext).ServiceProvider.GetRequiredService <IServiceConnectionContainer>(); await serviceHubContext.DisposeAsync(); await Task.Delay(500); Assert.Equal(ServiceConnectionStatus.Disconnected, connectionContainer.Status); } }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { // Replacing SignalR's userId provider with our custom services.AddTransient <IUserIdProvider, NickNameUserIdProvider>(); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); string signalRConnString = this.Configuration.GetValue <string>(Constants.AzureSignalRConnectionStringEnvironmentVariableName); signalRConnString = GetFromKeyVaultIfNeeded(signalRConnString); // Initializing SignalR with Azure SignalR Service connection string services .AddSignalR() .AddAzureSignalR(signalRConnString); // Also initializing and adding SignalR client var serviceManager = new ServiceManagerBuilder() .WithOptions(option => { option.ConnectionString = signalRConnString; // ServiceTransportType.Persistent would be more efficient, but it is not reliable (connection might be dropped and never reestablished) option.ServiceTransportType = ServiceTransportType.Transient; }) .Build(); // Adding the initialization Task to the container, not the IServiceHubContext itself. // That's OK, because the Task will only ran once (as guaranteed by the framework). services.AddSingleton(serviceManager.CreateHubContextAsync(nameof(LetsGoOutHub))); // Finally connecting to Redis string redisConnString = this.Configuration.GetValue <string>(Constants.RedisConnectionStringEnvironmentVariableName); redisConnString = GetFromKeyVaultIfNeeded(redisConnString); services.AddSingleton(ConnectionMultiplexer.Connect(redisConnString)); }