public CircuitHost( IServiceScope scope, CircuitClientProxy client, RendererRegistry rendererRegistry, RemoteRenderer renderer, IList <ComponentDescriptor> descriptors, IDispatcher dispatcher, RemoteJSRuntime jsRuntime, CircuitHandler[] circuitHandlers, ILogger logger) { _scope = scope ?? throw new ArgumentNullException(nameof(scope)); Dispatcher = dispatcher; Client = client; RendererRegistry = rendererRegistry ?? throw new ArgumentNullException(nameof(rendererRegistry)); Descriptors = descriptors ?? throw new ArgumentNullException(nameof(descriptors)); Renderer = renderer ?? throw new ArgumentNullException(nameof(renderer)); JSRuntime = jsRuntime ?? throw new ArgumentNullException(nameof(jsRuntime)); _logger = logger; Services = scope.ServiceProvider; Circuit = new Circuit(this); _circuitHandlers = circuitHandlers; Renderer.UnhandledException += Renderer_UnhandledException; Renderer.UnhandledSynchronizationException += SynchronizationContext_UnhandledException; }
public static CircuitHost Create( string circuitId = null, IServiceScope serviceScope = null, RemoteRenderer remoteRenderer = null, CircuitHandler[] handlers = null, CircuitClientProxy clientProxy = null) { serviceScope = serviceScope ?? Mock.Of <IServiceScope>(); clientProxy = clientProxy ?? new CircuitClientProxy(Mock.Of <IClientProxy>(), Guid.NewGuid().ToString()); var jsRuntime = new RemoteJSRuntime(Options.Create(new CircuitOptions()), Mock.Of <ILogger <RemoteJSRuntime> >()); if (remoteRenderer == null) { remoteRenderer = new RemoteRenderer( serviceScope.ServiceProvider ?? Mock.Of <IServiceProvider>(), NullLoggerFactory.Instance, new CircuitOptions(), jsRuntime, clientProxy, HtmlEncoder.Default, NullLogger.Instance); } handlers = handlers ?? Array.Empty <CircuitHandler>(); return(new TestCircuitHost( circuitId ?? Guid.NewGuid().ToString(), serviceScope, clientProxy, remoteRenderer, new List <ComponentDescriptor>(), jsRuntime, handlers, NullLogger <CircuitHost> .Instance)); }
public static CircuitHost Create( CircuitId?circuitId = null, AsyncServiceScope?serviceScope = null, RemoteRenderer remoteRenderer = null, CircuitHandler[] handlers = null, CircuitClientProxy clientProxy = null) { serviceScope = serviceScope ?? new AsyncServiceScope(Mock.Of <IServiceScope>()); clientProxy = clientProxy ?? new CircuitClientProxy(Mock.Of <IClientProxy>(), Guid.NewGuid().ToString()); var jsRuntime = new RemoteJSRuntime(Options.Create(new CircuitOptions()), Options.Create(new HubOptions()), Mock.Of <ILogger <RemoteJSRuntime> >()); if (remoteRenderer == null) { remoteRenderer = new RemoteRenderer( serviceScope.Value.ServiceProvider ?? Mock.Of <IServiceProvider>(), NullLoggerFactory.Instance, new CircuitOptions(), clientProxy, NullLogger.Instance, null); } handlers ??= Array.Empty <CircuitHandler>(); return(new TestCircuitHost( circuitId is null ? new CircuitId(Guid.NewGuid().ToString(), Guid.NewGuid().ToString()) : circuitId.Value, serviceScope.Value, new CircuitOptions(), clientProxy, remoteRenderer, new List <ComponentDescriptor>(), jsRuntime, handlers, NullLogger <CircuitHost> .Instance)); }
public override CircuitHost CreateCircuitHost( HttpContext httpContext, CircuitClientProxy client, string uriAbsolute, string baseUriAbsolute) { var components = ResolveComponentMetadata(httpContext, client); var scope = _scopeFactory.CreateScope(); var encoder = scope.ServiceProvider.GetRequiredService <HtmlEncoder>(); var jsRuntime = (RemoteJSRuntime)scope.ServiceProvider.GetRequiredService <IJSRuntime>(); var componentContext = (RemoteComponentContext)scope.ServiceProvider.GetRequiredService <IComponentContext>(); jsRuntime.Initialize(client); componentContext.Initialize(client); var uriHelper = (RemoteUriHelper)scope.ServiceProvider.GetRequiredService <IUriHelper>(); if (client != CircuitClientProxy.OfflineClient) { uriHelper.Initialize(uriAbsolute, baseUriAbsolute, jsRuntime); } else { uriHelper.Initialize(uriAbsolute, baseUriAbsolute); } var rendererRegistry = new RendererRegistry(); var dispatcher = Renderer.CreateDefaultDispatcher(); var renderer = new RemoteRenderer( scope.ServiceProvider, rendererRegistry, jsRuntime, client, dispatcher, encoder, _loggerFactory.CreateLogger <RemoteRenderer>()); var circuitHandlers = scope.ServiceProvider.GetServices <CircuitHandler>() .OrderBy(h => h.Order) .ToArray(); var circuitHost = new CircuitHost( scope, client, rendererRegistry, renderer, components, dispatcher, jsRuntime, circuitHandlers, _loggerFactory.CreateLogger <CircuitHost>()); // Initialize per - circuit data that services need (circuitHost.Services.GetRequiredService <ICircuitAccessor>() as DefaultCircuitAccessor).Circuit = circuitHost.Circuit; return(circuitHost); }
public CircuitHost CreateCircuitHost( IReadOnlyList <ComponentDescriptor> components, CircuitClientProxy client, string baseUri, string uri, ClaimsPrincipal user) { var scope = _scopeFactory.CreateScope(); var jsRuntime = (RemoteJSRuntime)scope.ServiceProvider.GetRequiredService <IJSRuntime>(); jsRuntime.Initialize(client); var navigationManager = (RemoteNavigationManager)scope.ServiceProvider.GetRequiredService <NavigationManager>(); var navigationInterception = (RemoteNavigationInterception)scope.ServiceProvider.GetRequiredService <INavigationInterception>(); if (client.Connected) { navigationManager.AttachJsRuntime(jsRuntime); navigationManager.Initialize(baseUri, uri); navigationInterception.AttachJSRuntime(jsRuntime); } else { navigationManager.Initialize(baseUri, uri); } var renderer = new RemoteRenderer( scope.ServiceProvider, _loggerFactory, _options, client, _loggerFactory.CreateLogger <RemoteRenderer>(), jsRuntime.ElementReferenceContext); var circuitHandlers = scope.ServiceProvider.GetServices <CircuitHandler>() .OrderBy(h => h.Order) .ToArray(); var circuitHost = new CircuitHost( _circuitIdFactory.CreateCircuitId(), scope, _options, client, renderer, components, jsRuntime, circuitHandlers, _loggerFactory.CreateLogger <CircuitHost>()); Log.CreatedCircuit(_logger, circuitHost); // Initialize per - circuit data that services need (circuitHost.Services.GetRequiredService <ICircuitAccessor>() as DefaultCircuitAccessor).Circuit = circuitHost.Circuit; circuitHost.SetCircuitUser(user); return(circuitHost); }
/// <summary> /// Creates a new <see cref="RemoteRenderer"/>. /// </summary> public RemoteRenderer( IServiceProvider serviceProvider, ILoggerFactory loggerFactory, CircuitOptions options, CircuitClientProxy client, ILogger logger) : base(serviceProvider, loggerFactory) { _client = client; _options = options; _logger = logger; }
/// <summary> /// Creates a new <see cref="RemoteRenderer"/>. /// </summary> public RemoteRenderer( IServiceProvider serviceProvider, ILoggerFactory loggerFactory, CircuitOptions options, CircuitClientProxy client, ILogger logger, ElementReferenceContext?elementReferenceContext) : base(serviceProvider, loggerFactory) { _client = client; _options = options; _logger = logger; ElementReferenceContext = elementReferenceContext; }
public void Transfer_SetsConnected() { // Arrange var clientProxy = Mock.Of <IClientProxy>( c => c.SendCoreAsync(It.IsAny <string>(), It.IsAny <object[]>(), It.IsAny <CancellationToken>()) == Task.CompletedTask); var circuitClient = new CircuitClientProxy(clientProxy, "connection0"); circuitClient.SetDisconnected(); // Act circuitClient.Transfer(Mock.Of <IClientProxy>(), "connection1"); // Assert Assert.True(circuitClient.Connected); }
/// <summary> /// Creates a new <see cref="RemoteRenderer"/>. /// </summary> public RemoteRenderer( IServiceProvider serviceProvider, ILoggerFactory loggerFactory, CircuitOptions options, CircuitClientProxy client, ILogger logger, RemoteJSRuntime jsRuntime, CircuitJSComponentInterop jsComponentInterop) : base(serviceProvider, loggerFactory, jsRuntime.ReadJsonSerializerOptions(), jsComponentInterop) { _client = client; _options = options; _logger = logger; ElementReferenceContext = jsRuntime.ElementReferenceContext; }
public CircuitHost( CircuitId circuitId, AsyncServiceScope scope, CircuitOptions options, CircuitClientProxy client, RemoteRenderer renderer, IReadOnlyList <ComponentDescriptor> descriptors, RemoteJSRuntime jsRuntime, CircuitHandler[] circuitHandlers, ILogger logger) { CircuitId = circuitId; if (CircuitId.Secret is null) { // Prevent the use of a 'default' secret. throw new ArgumentException($"Property '{nameof(CircuitId.Secret)}' cannot be null.", nameof(circuitId)); } _scope = scope; _options = options ?? throw new ArgumentNullException(nameof(options)); Client = client ?? throw new ArgumentNullException(nameof(client)); Renderer = renderer ?? throw new ArgumentNullException(nameof(renderer)); Descriptors = descriptors ?? throw new ArgumentNullException(nameof(descriptors)); JSRuntime = jsRuntime ?? throw new ArgumentNullException(nameof(jsRuntime)); _circuitHandlers = circuitHandlers ?? throw new ArgumentNullException(nameof(circuitHandlers)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); Services = scope.ServiceProvider; Circuit = new Circuit(this); Handle = new CircuitHandle() { CircuitHost = this, }; // An unhandled exception from the renderer is always fatal because it came from user code. Renderer.UnhandledException += ReportAndInvoke_UnhandledException; Renderer.UnhandledSynchronizationException += SynchronizationContext_UnhandledException; JSRuntime.UnhandledException += ReportAndInvoke_UnhandledException; }
public async Task SendCoreAsync_WithoutTransfer() { // Arrange bool?isCancelled = null; var clientProxy = new Mock <IClientProxy>(); clientProxy.Setup(c => c.SendCoreAsync(It.IsAny <string>(), It.IsAny <object[]>(), It.IsAny <CancellationToken>())) .Callback((string _, object[] __, CancellationToken token) => { isCancelled = token.IsCancellationRequested; }) .Returns(Task.CompletedTask); var circuitClient = new CircuitClientProxy(clientProxy.Object, "connection0"); // Act var sendTask = circuitClient.SendCoreAsync("test", Array.Empty <object>()); await sendTask; // Assert Assert.False(isCancelled); }
public CircuitHost( CircuitId circuitId, IServiceScope scope, CircuitOptions options, CircuitClientProxy client, RemoteRenderer renderer, IReadOnlyList <ComponentDescriptor> descriptors, RemoteJSRuntime jsRuntime, CircuitHandler[] circuitHandlers, ILogger logger) { CircuitId = circuitId; if (CircuitId.Secret is null) { // Prevent the use of a 'default' secret. throw new ArgumentException(nameof(circuitId)); } _scope = scope ?? throw new ArgumentNullException(nameof(scope)); _options = options ?? throw new ArgumentNullException(nameof(options)); Client = client ?? throw new ArgumentNullException(nameof(client)); Renderer = renderer ?? throw new ArgumentNullException(nameof(renderer)); Descriptors = descriptors ?? throw new ArgumentNullException(nameof(descriptors)); JSRuntime = jsRuntime ?? throw new ArgumentNullException(nameof(jsRuntime)); _circuitHandlers = circuitHandlers ?? throw new ArgumentNullException(nameof(circuitHandlers)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); Services = scope.ServiceProvider; Circuit = new Circuit(this); Handle = new CircuitHandle() { CircuitHost = this, }; Renderer.UnhandledException += Renderer_UnhandledException; Renderer.UnhandledSynchronizationException += SynchronizationContext_UnhandledException; }
public static CircuitHost Create( IServiceScope serviceScope = null, RemoteRenderer remoteRenderer = null, CircuitHandler[] handlers = null, CircuitClientProxy clientProxy = null) { serviceScope = serviceScope ?? Mock.Of <IServiceScope>(); clientProxy = clientProxy ?? new CircuitClientProxy(Mock.Of <IClientProxy>(), Guid.NewGuid().ToString()); var renderRegistry = new RendererRegistry(); var jsRuntime = new RemoteJSRuntime(); var dispatcher = Rendering.Renderer.CreateDefaultDispatcher(); if (remoteRenderer == null) { remoteRenderer = new RemoteRenderer( Mock.Of <IServiceProvider>(), new RendererRegistry(), jsRuntime, clientProxy, dispatcher, HtmlEncoder.Default, NullLogger.Instance); } handlers = handlers ?? Array.Empty <CircuitHandler>(); return(new TestCircuitHost( serviceScope, clientProxy, renderRegistry, remoteRenderer, new List <ComponentDescriptor>(), dispatcher, jsRuntime, handlers, NullLogger <CircuitHost> .Instance)); }
private TestCircuitHost(string circuitId, IServiceScope scope, CircuitClientProxy client, RendererRegistry rendererRegistry, RemoteRenderer renderer, IReadOnlyList <ComponentDescriptor> descriptors, RemoteJSRuntime jsRuntime, CircuitHandler[] circuitHandlers, ILogger logger) : base(circuitId, scope, client, rendererRegistry, renderer, descriptors, jsRuntime, circuitHandlers, logger) { }
private TestCircuitHost(CircuitId circuitId, AsyncServiceScope scope, CircuitOptions options, CircuitClientProxy client, RemoteRenderer renderer, IReadOnlyList <ComponentDescriptor> descriptors, RemoteJSRuntime jsRuntime, CircuitHandler[] circuitHandlers, ILogger logger) : base(circuitId, scope, options, client, renderer, descriptors, jsRuntime, circuitHandlers, logger) { }
private static IList <ComponentDescriptor> ResolveComponentMetadata(HttpContext httpContext, CircuitClientProxy client) { if (client == CircuitClientProxy.OfflineClient) { // This is the prerendering case. return(Array.Empty <ComponentDescriptor>()); } else { var endpointFeature = httpContext.Features.Get <IEndpointFeature>(); var endpoint = endpointFeature?.Endpoint; if (endpoint == null) { throw new InvalidOperationException( $"{nameof(ComponentHub)} doesn't have an associated endpoint. " + "Use 'app.UseRouting(routes => routes.MapComponentHub<App>(\"app\"))' to register your hub."); } var componentsMetadata = endpoint.Metadata.OfType <ComponentDescriptor>().ToList(); if (componentsMetadata.Count == 0) { throw new InvalidOperationException("No component was registered with the component hub."); } return(componentsMetadata); } }
public override CircuitHost CreateCircuitHost( HttpContext httpContext, CircuitClientProxy client, string uriAbsolute, string baseUriAbsolute, ClaimsPrincipal user) { var components = ResolveComponentMetadata(httpContext, client); var scope = _scopeFactory.CreateScope(); var encoder = scope.ServiceProvider.GetRequiredService <HtmlEncoder>(); var jsRuntime = (RemoteJSRuntime)scope.ServiceProvider.GetRequiredService <IJSRuntime>(); var componentContext = (RemoteComponentContext)scope.ServiceProvider.GetRequiredService <IComponentContext>(); jsRuntime.Initialize(client); componentContext.Initialize(client); var uriHelper = (RemoteUriHelper)scope.ServiceProvider.GetRequiredService <IUriHelper>(); var navigationInterception = (RemoteNavigationInterception)scope.ServiceProvider.GetRequiredService <INavigationInterception>(); if (client.Connected) { uriHelper.AttachJsRuntime(jsRuntime); uriHelper.InitializeState( uriAbsolute, baseUriAbsolute); navigationInterception.AttachJSRuntime(jsRuntime); } else { uriHelper.InitializeState(uriAbsolute, baseUriAbsolute); } var rendererRegistry = new RendererRegistry(); var renderer = new RemoteRenderer( scope.ServiceProvider, _loggerFactory, rendererRegistry, jsRuntime, client, encoder, _loggerFactory.CreateLogger <RemoteRenderer>()); var circuitHandlers = scope.ServiceProvider.GetServices <CircuitHandler>() .OrderBy(h => h.Order) .ToArray(); var circuitHost = new CircuitHost( _circuitIdFactory.CreateCircuitId(), scope, client, rendererRegistry, renderer, components, jsRuntime, circuitHandlers, _loggerFactory.CreateLogger <CircuitHost>()); Log.CreatedCircuit(_logger, circuitHost); // Initialize per - circuit data that services need (circuitHost.Services.GetRequiredService <ICircuitAccessor>() as DefaultCircuitAccessor).Circuit = circuitHost.Circuit; circuitHost.SetCircuitUser(user); return(circuitHost); }
public override CircuitHost CreateCircuitHost( HttpContext httpContext, CircuitClientProxy client, string uriAbsolute, string baseUriAbsolute) { var components = ResolveComponentMetadata(httpContext, client); var scope = _scopeFactory.CreateScope(); var encoder = scope.ServiceProvider.GetRequiredService <HtmlEncoder>(); var jsRuntime = (RemoteJSRuntime)scope.ServiceProvider.GetRequiredService <IJSRuntime>(); var componentContext = (RemoteComponentContext)scope.ServiceProvider.GetRequiredService <IComponentContext>(); jsRuntime.Initialize(client); componentContext.Initialize(client); // You can replace the AuthenticationStateProvider with a custom one, but in that case initialization is up to you var authenticationStateProvider = scope.ServiceProvider.GetService <AuthenticationStateProvider>(); (authenticationStateProvider as FixedAuthenticationStateProvider)?.Initialize(httpContext.User); var uriHelper = (RemoteUriHelper)scope.ServiceProvider.GetRequiredService <IUriHelper>(); if (client.Connected) { uriHelper.AttachJsRuntime(jsRuntime); uriHelper.InitializeState( uriAbsolute, baseUriAbsolute); } else { uriHelper.InitializeState(uriAbsolute, baseUriAbsolute); } var rendererRegistry = new RendererRegistry(); var dispatcher = Renderer.CreateDefaultDispatcher(); var renderer = new RemoteRenderer( scope.ServiceProvider, rendererRegistry, jsRuntime, client, dispatcher, encoder, _loggerFactory.CreateLogger <RemoteRenderer>()); var circuitHandlers = scope.ServiceProvider.GetServices <CircuitHandler>() .OrderBy(h => h.Order) .ToArray(); var circuitHost = new CircuitHost( scope, client, rendererRegistry, renderer, components, dispatcher, jsRuntime, circuitHandlers, _loggerFactory.CreateLogger <CircuitHost>()); // Initialize per - circuit data that services need (circuitHost.Services.GetRequiredService <ICircuitAccessor>() as DefaultCircuitAccessor).Circuit = circuitHost.Circuit; return(circuitHost); }
internal static IList <ComponentDescriptor> ResolveComponentMetadata(HttpContext httpContext, CircuitClientProxy client) { if (!client.Connected) { // This is the prerendering case. Descriptors will be registered by the prerenderer. return(new List <ComponentDescriptor>()); } else { var endpointFeature = httpContext.Features.Get <IEndpointFeature>(); var endpoint = endpointFeature?.Endpoint; if (endpoint == null) { throw new InvalidOperationException( $"{nameof(ComponentHub)} doesn't have an associated endpoint. " + "Use 'app.UseEndpoints(endpoints => endpoints.MapBlazorHub<App>(\"app\"))' to register your hub."); } var componentsMetadata = endpoint.Metadata.OfType <ComponentDescriptor>().ToList(); return(componentsMetadata); } }
internal void Initialize(CircuitClientProxy clientProxy) { _clientProxy = clientProxy ?? throw new ArgumentNullException(nameof(clientProxy)); }
public override CircuitHost CreateCircuitHost( HttpContext httpContext, CircuitClientProxy client, string baseUri, string uri, ClaimsPrincipal user) { // We do as much intialization as possible eagerly in this method, which makes the error handling // story much simpler. If we throw from here, it's handled inside the initial hub method. var components = ResolveComponentMetadata(httpContext, client); var scope = _scopeFactory.CreateScope(); var encoder = scope.ServiceProvider.GetRequiredService <HtmlEncoder>(); var jsRuntime = (RemoteJSRuntime)scope.ServiceProvider.GetRequiredService <IJSRuntime>(); var componentContext = (RemoteComponentContext)scope.ServiceProvider.GetRequiredService <IComponentContext>(); jsRuntime.Initialize(client); componentContext.Initialize(client); var navigationManager = (RemoteNavigationManager)scope.ServiceProvider.GetRequiredService <NavigationManager>(); var navigationInterception = (RemoteNavigationInterception)scope.ServiceProvider.GetRequiredService <INavigationInterception>(); if (client.Connected) { navigationManager.AttachJsRuntime(jsRuntime); navigationManager.Initialize(baseUri, uri); navigationInterception.AttachJSRuntime(jsRuntime); } else { navigationManager.Initialize(baseUri, uri); } var renderer = new RemoteRenderer( scope.ServiceProvider, _loggerFactory, _options, jsRuntime, client, encoder, _loggerFactory.CreateLogger <RemoteRenderer>()); var circuitHandlers = scope.ServiceProvider.GetServices <CircuitHandler>() .OrderBy(h => h.Order) .ToArray(); var circuitHost = new CircuitHost( _circuitIdFactory.CreateCircuitId(), scope, client, renderer, components, jsRuntime, circuitHandlers, _loggerFactory.CreateLogger <CircuitHost>()); Log.CreatedCircuit(_logger, circuitHost); // Initialize per - circuit data that services need (circuitHost.Services.GetRequiredService <ICircuitAccessor>() as DefaultCircuitAccessor).Circuit = circuitHost.Circuit; circuitHost.SetCircuitUser(user); return(circuitHost); }
private TestCircuitHost(IServiceScope scope, CircuitClientProxy client, RendererRegistry rendererRegistry, RemoteRenderer renderer, IList <ComponentDescriptor> descriptors, IDispatcher dispatcher, RemoteJSRuntime jsRuntime, CircuitHandler[] circuitHandlers, ILogger logger) : base(scope, client, rendererRegistry, renderer, descriptors, dispatcher, jsRuntime, circuitHandlers, logger) { }
public abstract CircuitHost CreateCircuitHost( HttpContext httpContext, CircuitClientProxy client, string baseUri, string uri, ClaimsPrincipal user);
public abstract CircuitHost CreateCircuitHost( HttpContext httpContext, CircuitClientProxy client, string uriAbsolute, string baseUriAbsolute);