diff --git a/.github/workflows/ci-main.yml b/.github/workflows/ci-main.yml index 9b70682..3fcb662 100644 --- a/.github/workflows/ci-main.yml +++ b/.github/workflows/ci-main.yml @@ -59,7 +59,7 @@ jobs: arrTag=(${GITHUB_REF//\// }) VERSION="${arrTag[2]}" echo Version: $VERSION - VERSION="${VERSION//v}" + VERSION="${VERSION:1}" echo Clean Version: $VERSION dotnet pack -v normal -c Release --include-symbols --include-source -p:PackageVersion=$VERSION -o nupkg src/Blazored.$PROJECT_NAME/Blazored.$PROJECT_NAME.csproj dotnet pack -v normal -c Release --include-symbols --include-source -p:PackageVersion=$VERSION -o nupkg src/Blazored.$PROJECT_NAME.TestExtensions/Blazored.$PROJECT_NAME.TestExtensions.csproj diff --git a/Blazored.LocalStorage.sln b/Blazored.LocalStorage.sln index 4a442b5..8234643 100644 --- a/Blazored.LocalStorage.sln +++ b/Blazored.LocalStorage.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.28531.58 +# Visual Studio Version 17 +VisualStudioVersion = 17.1.32228.430 MinimumVisualStudioVersion = 15.0.26124.0 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blazored.LocalStorage", "src\Blazored.LocalStorage\Blazored.LocalStorage.csproj", "{EFA12DC3-35DE-449C-88F2-2BD7576CAF2C}" EndProject diff --git a/README.md b/README.md index 9de5feb..72d06f0 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,16 @@ public static async Task Main(string[] args) } ``` +### Registering services as Singleton - Blazor WebAssembly **ONLY** +99% of developers will want to register Blazored LocalStorage using the method described above. However, in some very specific scenarios +developer may have a need to register services as Singleton as apposed to Scoped. This is possible by using the following method: + +```csharp +builder.Services.AddBlazoredLocalStorageAsSingleton(); +``` + +This method will not work with Blazor Server applications as Blazor's JS interop services are registered as Scoped and cannot be injected into Singletons. + ## Usage (Blazor WebAssembly) To use Blazored.LocalStorage in Blazor WebAssembly, inject the `ILocalStorageService` per the example below. diff --git a/samples/BlazorServer/App.razor b/samples/BlazorServer/App.razor index b941644..6fd3ed1 100644 --- a/samples/BlazorServer/App.razor +++ b/samples/BlazorServer/App.razor @@ -1,10 +1,12 @@ - + + + Not found -

Sorry, there's nothing at this address.

+

Sorry, there's nothing at this address.

diff --git a/samples/BlazorServer/BlazorServer.csproj b/samples/BlazorServer/BlazorServer.csproj index 681d263..4622db9 100644 --- a/samples/BlazorServer/BlazorServer.csproj +++ b/samples/BlazorServer/BlazorServer.csproj @@ -2,6 +2,8 @@ net6.0 + enable + enable diff --git a/samples/BlazorServer/NewtonSoftJsonSerializer.cs b/samples/BlazorServer/NewtonSoftJsonSerializer.cs new file mode 100644 index 0000000..c37118a --- /dev/null +++ b/samples/BlazorServer/NewtonSoftJsonSerializer.cs @@ -0,0 +1,13 @@ +using Blazored.LocalStorage.Serialization; +using Newtonsoft.Json; + +namespace BlazorServer; + +public class NewtonSoftJsonSerializer : IJsonSerializer +{ + public T Deserialize(string text) + => JsonConvert.DeserializeObject(text); + + public string Serialize(T obj) + => JsonConvert.SerializeObject(obj); +} diff --git a/samples/BlazorServer/Pages/Index.razor b/samples/BlazorServer/Pages/Index.razor index 5e1aaaf..a94c1db 100644 --- a/samples/BlazorServer/Pages/Index.razor +++ b/samples/BlazorServer/Pages/Index.razor @@ -1,6 +1,8 @@ @page "/" @inject ILocalStorageService localStorage +Blazored LocalStorage (Server) +

Blazored LocalStorage Sample


diff --git a/samples/BlazorServer/Pages/_Host.cshtml b/samples/BlazorServer/Pages/_Host.cshtml index a83d46b..7d287d6 100644 --- a/samples/BlazorServer/Pages/_Host.cshtml +++ b/samples/BlazorServer/Pages/_Host.cshtml @@ -1,32 +1,9 @@ @page "/" @namespace BlazorServer.Pages @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers +@{ + Layout = "_Layout"; +} - - - - - - Blazored LocalStorage (Server) - - - - - - - + -
- - An error has occurred. This application may no longer respond until reloaded. - - - An unhandled exception has occurred. See browser dev tools for details. - - Reload - 🗙 -
- - - - diff --git a/samples/BlazorServer/Pages/_Layout.cshtml b/samples/BlazorServer/Pages/_Layout.cshtml new file mode 100644 index 0000000..6c31c4c --- /dev/null +++ b/samples/BlazorServer/Pages/_Layout.cshtml @@ -0,0 +1,32 @@ +@using Microsoft.AspNetCore.Components.Web +@namespace BlazorServer.Pages +@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers + + + + + + + + + + + + + + @RenderBody() + +
+ + An error has occurred. This application may no longer respond until reloaded. + + + An unhandled exception has occurred. See browser dev tools for details. + + Reload + 🗙 +
+ + + + diff --git a/samples/BlazorServer/Program.cs b/samples/BlazorServer/Program.cs index 1f2b71a..fbc44cd 100644 --- a/samples/BlazorServer/Program.cs +++ b/samples/BlazorServer/Program.cs @@ -1,20 +1,33 @@ -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Hosting; +using Blazored.LocalStorage; +using Blazored.LocalStorage.Serialization; +using BlazorServer; +using Microsoft.Extensions.DependencyInjection.Extensions; -namespace BlazorServer +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. +builder.Services.AddRazorPages(); +builder.Services.AddServerSideBlazor(config => config.DetailedErrors = true); +builder.Services.AddBlazoredLocalStorage(); +builder.Services.Replace(ServiceDescriptor.Scoped()); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (!app.Environment.IsDevelopment()) { - public class Program - { - public static void Main(string[] args) - { - CreateHostBuilder(args).Build().Run(); - } - - public static IHostBuilder CreateHostBuilder(string[] args) => - Host.CreateDefaultBuilder(args) - .ConfigureWebHostDefaults(webBuilder => - { - webBuilder.UseStartup(); - }); - } + app.UseExceptionHandler("/Error"); + // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. + app.UseHsts(); } + +app.UseHttpsRedirection(); + +app.UseStaticFiles(); + +app.UseRouting(); + +app.MapBlazorHub(); +app.MapFallbackToPage("/_Host"); + +app.Run(); diff --git a/samples/BlazorServer/Startup.cs b/samples/BlazorServer/Startup.cs deleted file mode 100644 index 31b04c0..0000000 --- a/samples/BlazorServer/Startup.cs +++ /dev/null @@ -1,68 +0,0 @@ -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using Blazored.LocalStorage; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection.Extensions; -using Blazored.LocalStorage.Serialization; -using Newtonsoft.Json; -using System; - -namespace BlazorServer -{ - public class Startup - { - public Startup(IConfiguration configuration) - { - Configuration = configuration; - } - - public IConfiguration Configuration { get; } - - // This method gets called by the runtime. Use this method to add services to the container. - // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 - public void ConfigureServices(IServiceCollection services) - { - services.AddRazorPages(); - services.AddServerSideBlazor(config => config.DetailedErrors = true); - services.AddBlazoredLocalStorage(); - services.Replace(ServiceDescriptor.Scoped()); - } - - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IWebHostEnvironment env) - { - if (env.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - } - else - { - app.UseExceptionHandler("/Error"); - // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. - app.UseHsts(); - } - - app.UseHttpsRedirection(); - app.UseStaticFiles(); - - app.UseRouting(); - - app.UseEndpoints(endpoints => - { - endpoints.MapBlazorHub(); - endpoints.MapFallbackToPage("/_Host"); - }); - } - } - - public class NewtonSoftJsonSerializer : IJsonSerializer - { - public T Deserialize(string text) - =>JsonConvert.DeserializeObject(text); - - public string Serialize(T obj) - => JsonConvert.SerializeObject(obj); - } -} diff --git a/samples/BlazorWebAssembly/BlazorWebAssembly.csproj b/samples/BlazorWebAssembly/BlazorWebAssembly.csproj index 85b7b01..bcf4a6d 100644 --- a/samples/BlazorWebAssembly/BlazorWebAssembly.csproj +++ b/samples/BlazorWebAssembly/BlazorWebAssembly.csproj @@ -5,8 +5,8 @@ - - + + diff --git a/samples/BlazorWebAssembly/Program.cs b/samples/BlazorWebAssembly/Program.cs index 2e74822..96895b7 100644 --- a/samples/BlazorWebAssembly/Program.cs +++ b/samples/BlazorWebAssembly/Program.cs @@ -11,7 +11,7 @@ public static async Task Main(string[] args) var builder = WebAssemblyHostBuilder.CreateDefault(args); builder.RootComponents.Add("#app"); - builder.Services.AddBlazoredLocalStorage(); + builder.Services.AddBlazoredLocalStorageAsSingleton(); await builder.Build().RunAsync(); } diff --git a/src/Blazored.LocalStorage/ServiceCollectionExtensions.cs b/src/Blazored.LocalStorage/ServiceCollectionExtensions.cs index 0699c0e..c561126 100644 --- a/src/Blazored.LocalStorage/ServiceCollectionExtensions.cs +++ b/src/Blazored.LocalStorage/ServiceCollectionExtensions.cs @@ -26,5 +26,34 @@ public static IServiceCollection AddBlazoredLocalStorage(this IServiceCollection configureOptions.JsonSerializerOptions.Converters.Add(new TimespanJsonConverter()); }); } + + /// + /// Registers the Blazored LocalStorage services as singletons. This should only be used in Blazor WebAssembly applications. + /// Using this in Blazor Server applications will cause unexpected and potentially dangerous behaviour. + /// + /// + public static IServiceCollection AddBlazoredLocalStorageAsSingleton(this IServiceCollection services) + => AddBlazoredLocalStorageAsSingleton(services, null); + + /// + /// Registers the Blazored LocalStorage services as singletons. This should only be used in Blazor WebAssembly applications. + /// Using this in Blazor Server applications will cause unexpected and potentially dangerous behaviour. + /// + /// + /// + /// + public static IServiceCollection AddBlazoredLocalStorageAsSingleton(this IServiceCollection services, Action configure) + { + return services + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .Configure(configureOptions => + { + configure?.Invoke(configureOptions); + configureOptions.JsonSerializerOptions.Converters.Add(new TimespanJsonConverter()); + }); + } } }