From 1050244e4475c265d439f08be4c2f1f768a153b2 Mon Sep 17 00:00:00 2001 From: jsakamoto Date: Thu, 14 Dec 2023 08:20:15 +0900 Subject: [PATCH] Make the Blazor Server sample project be able to test the DisableClientScriptAutoInjection option --- .../Components/SampleSite.Components.csproj | 4 ++-- SampleSites/Server/Pages/_Host.cshtml | 10 +++++++- SampleSites/Server/Program.cs | 8 +++++-- SampleSites/Server/appsettings.json | 5 +++- Toolbelt.Blazor.Gamepad/GamepadExtensions.cs | 23 +++++++++++++++---- 5 files changed, 39 insertions(+), 11 deletions(-) diff --git a/SampleSites/Components/SampleSite.Components.csproj b/SampleSites/Components/SampleSite.Components.csproj index 18371e0..08353ea 100644 --- a/SampleSites/Components/SampleSite.Components.csproj +++ b/SampleSites/Components/SampleSite.Components.csproj @@ -17,9 +17,9 @@ - + - + diff --git a/SampleSites/Server/Pages/_Host.cshtml b/SampleSites/Server/Pages/_Host.cshtml index 1c3a2d4..ef403a0 100644 --- a/SampleSites/Server/Pages/_Host.cshtml +++ b/SampleSites/Server/Pages/_Host.cshtml @@ -2,6 +2,10 @@ @namespace SampleSite.Server @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers @using SampleSite.Components +@using Microsoft.Extensions.Options +@using Toolbelt.Blazor.Gamepad + +@inject IOptions GamepadOptions @@ -13,6 +17,7 @@ + @@ -32,7 +37,10 @@ - @**@ + @if (GamepadOptions.Value.DisableClientScriptAutoInjection) + { + + } diff --git a/SampleSites/Server/Program.cs b/SampleSites/Server/Program.cs index d03dd95..3f0cdfe 100644 --- a/SampleSites/Server/Program.cs +++ b/SampleSites/Server/Program.cs @@ -1,14 +1,18 @@ +using Microsoft.Extensions.Options; using Toolbelt.Blazor.Extensions.DependencyInjection; +using Toolbelt.Blazor.Gamepad; var builder = WebApplication.CreateBuilder(args); +builder.Services.Configure(builder.Configuration.GetSection("Gamepad")); // Add services to the container. builder.Services.AddRazorPages(); builder.Services.AddServerSideBlazor(); builder.Services.AddHeadElementHelper(); -builder.Services.AddGamepadList(options => +builder.Services.AddGamepadList((services, options) => { - //options.DisableClientScriptAutoInjection = true; + var config = services.GetRequiredService>(); + options.DisableClientScriptAutoInjection = config.Value.DisableClientScriptAutoInjection; }); var app = builder.Build(); diff --git a/SampleSites/Server/appsettings.json b/SampleSites/Server/appsettings.json index d9d9a9b..28bc3db 100644 --- a/SampleSites/Server/appsettings.json +++ b/SampleSites/Server/appsettings.json @@ -6,5 +6,8 @@ "Microsoft.Hosting.Lifetime": "Information" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "Gamepad": { + "DisableClientScriptAutoInjection": false + } } diff --git a/Toolbelt.Blazor.Gamepad/GamepadExtensions.cs b/Toolbelt.Blazor.Gamepad/GamepadExtensions.cs index dacec57..abb7082 100644 --- a/Toolbelt.Blazor.Gamepad/GamepadExtensions.cs +++ b/Toolbelt.Blazor.Gamepad/GamepadExtensions.cs @@ -15,19 +15,32 @@ public static class GamepadExtensions /// The Microsoft.Extensions.DependencyInjection.IServiceCollection to add the service to. public static IServiceCollection AddGamepadList(this IServiceCollection services) { - return services.AddGamepadList(configure: null); + return services.AddGamepadList(configure: (_) => { }); } /// /// Adds a gamepad list service to the specified Microsoft.Extensions.DependencyInjection.IServiceCollection. /// /// The Microsoft.Extensions.DependencyInjection.IServiceCollection to add the service to. - /// + /// A delegate that is used to configure the Microsoft.Extensions.DependencyInjection.Options.GamepadOptions. public static IServiceCollection AddGamepadList(this IServiceCollection services, Action? configure) { - var options = new GamepadOptions(); - configure?.Invoke(options); - services.AddScoped(serviceProvider => new GamepadList(serviceProvider.GetRequiredService(), options)); + return services.AddGamepadList(configure: (_, options) => { configure?.Invoke(options); }); + } + + /// + /// Adds a gamepad list service to the specified Microsoft.Extensions.DependencyInjection.IServiceCollection. + /// + /// The Microsoft.Extensions.DependencyInjection.IServiceCollection to add the service to. + /// A delegate that is used to configure the Microsoft.Extensions.DependencyInjection.Options.GamepadOptions. + public static IServiceCollection AddGamepadList(this IServiceCollection services, Action configure) + { + services.AddScoped(serviceProvider => + { + var options = new GamepadOptions(); + configure?.Invoke(serviceProvider, options); + return new GamepadList(serviceProvider.GetRequiredService(), options); + }); return services; } } \ No newline at end of file