/// <summary> /// Maps incoming requests with the specified path to the provided connection pipeline. /// </summary> /// <typeparam name="TConnectionHandler">The <see cref="ConnectionHandler"/> type.</typeparam> /// <param name="endpoints">The <see cref="IEndpointBuilder"/> to add the route to.</param> /// <param name="pattern">The route pattern.</param> /// <param name="configureOptions">A callback to configure dispatcher options.</param> /// <returns>An <see cref="ConnectionEndpointRouteBuilder"/> for endpoints associated with the connections.</returns> public static ConnectionEndpointRouteBuilder MapConnectionHandler <TConnectionHandler>(this IEndpointBuilder endpoints, string pattern, Action <HttpConnectionDispatcherOptions>?configureOptions) where TConnectionHandler : ConnectionHandler { var options = new HttpConnectionDispatcherOptions(); configureOptions?.Invoke(options); var conventionBuilder = endpoints.MapConnections(pattern, options, b => { b.UseConnectionHandler <TConnectionHandler>(); }); var attributes = typeof(TConnectionHandler).GetCustomAttributes(inherit: true); conventionBuilder.Add(e => { // Add all attributes on the ConnectionHandler has metadata (this will allow for things like) // auth attributes and cors attributes to work seamlessly foreach (var item in attributes) { e.Metadata.Add(item); } }); return(conventionBuilder); }
/// <summary> /// Maps incoming requests with the specified path to the specified <see cref="Hub"/> type. /// </summary> /// <typeparam name="THub">The <see cref="Hub"/> type to map requests to.</typeparam> /// <param name="endpoints">The <see cref="IEndpointBuilder"/> to add the route to.</param> /// <param name="pattern">The route pattern.</param> /// <param name="configureOptions">A callback to configure dispatcher options.</param> /// <returns>An <see cref="HubEndpointConventionBuilder"/> for endpoints associated with the connections.</returns> public static HubEndpointConventionBuilder MapHub <THub>(this IEndpointBuilder endpoints, string pattern, Action <HttpConnectionDispatcherOptions>?configureOptions) where THub : Hub { var marker = endpoints.ServiceProvider.GetService(_signalRMarkerServiceType); if (marker == null) { throw new InvalidOperationException( "Unable to find the required services. Please add all the required services by calling " + "'IServiceCollection.AddSignalR' inside the call to 'ConfigureServices(...)' in the application startup code."); } var options = new HttpConnectionDispatcherOptions(); configureOptions?.Invoke(options); var conventionBuilder = endpoints.MapConnections(pattern, options, b => { b.UseHub <THub>(); }); var attributes = typeof(THub).GetCustomAttributes(inherit: true); conventionBuilder.Add(e => { // Add all attributes on the Hub as metadata (this will allow for things like) // auth attributes and cors attributes to work seamlessly foreach (var item in attributes) { e.Metadata.Add(item); } // Add metadata that captures the hub type this endpoint is associated with e.Metadata.Add(new HubMetadata(typeof(THub))); }); return((HubEndpointConventionBuilder) typeof(HubEndpointConventionBuilder) .GetTypeInfo().DeclaredConstructors .Single() .Invoke(new object[] { conventionBuilder })); }
/// <summary> /// Maps incoming requests with the specified path to the provided connection pipeline. /// </summary> /// <param name="endpoints">The <see cref="IEndpointBuilder"/> to add the route to.</param> /// <param name="pattern">The route pattern.</param> /// <param name="configure">A callback to configure the connection.</param> /// <returns>An <see cref="ConnectionEndpointRouteBuilder"/> for endpoints associated with the connections.</returns> public static ConnectionEndpointRouteBuilder MapConnections(this IEndpointBuilder endpoints, string pattern, Action <IConnectionBuilder> configure) => endpoints.MapConnections(pattern, new HttpConnectionDispatcherOptions(), configure);