question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Latest commit (PR 22): Under Netstandard 3.0: Cannot consume scoped service 'Microsoft.JSInterop.IJSRuntime' from singleton 'Blazor.Extensions.Storage.SessionStorage'

See original GitHub issue

Using .net core 3 preview 4 and your latest commit of PR22, injecting LocalStorage into my class using DI results in error:

Cannot consume scoped service ‘Microsoft.JSInterop.IJSRuntime’ from singleton ‘Blazor.Extensions.Storage.LocalStorage’

I believe this is because your StorageExtensions.cs calls: AddSingleton<LocalStorage>()

But when your LocalStorage.cs constructor injects IJSRuntime public LocalStorage(IJSRuntime Runtime) I am guessing that IJSRuntime is added to the Service Collection as Scoped, therefore consuming it from your Singleton StorageExtensions.cs cases this error.

FYI I have tried amending your StorageExtensions.cs to read: AddScoped<LocalStorage>(). This does compile and run but then any call to await _localStorage.SetItem<>() never returns…

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
cpsaezcommented, Jun 19, 2019

After a couple of hours I can execute it in server mode. How:

install nuget package configure dependencies by hand using Transient scope: services.AddTransient<LocalStorage>();

Clone this repo Compile with typescript the ts files in Storage\src\Blazor.Extensions.Storage.JS\src create a javascript file join both created files. I removed the ‘required’ to put all code togheter:

class BrowserStorage {
    Length(storage) {
        return window[storage].length;
    }
    ;
    Key(storage, index) {
        return window[storage].key(index);
    }
    ;
    GetItem(storage, key) {
        const item = window[storage].getItem(key);
        if (item) {
            return JSON.parse(item);
        }
        return null;
    }
    ;
    SetItem(storage, key, keyValue) {
        window[storage].setItem(key, JSON.stringify(keyValue));
    }
    ;
    RemoveItem(storage, key) {
        window[storage].removeItem(key);
    }
    ;
    Clear(storage) {
        window[storage].clear();
    }
    ;
}


var Storage;
(function (Storage) {
    const blazorExtensions = 'BlazorExtensions';
    const extensionObject = {
        Storage: new BrowserStorage()
    };
    function initialize() {
        if (typeof window !== 'undefined' && !window[blazorExtensions]) {
            window[blazorExtensions] = Object.assign({}, extensionObject);
        }
        else {
            window[blazorExtensions] = Object.assign({}, window[blazorExtensions], extensionObject);
        }
    }
    Storage.initialize = initialize;
})(Storage || (Storage = {}));
Storage.initialize();

I named this file InitializeStorage.js (very original I know)

Reference this file in _Host.cshtml at the end of the body part:

`<body id="page-top"> <app>@(await Html.RenderComponentAsync<App>())</app> <script src="_framework/blazor.server.js"></script> <script src="~/assets/js/InitializeStorage.js"></script>

</body>`

And it started to work. I figured it out the problem to “embed” javascript code from external dll. But this trick works for me. No idea how to contribute and fix this issue once for all.

Thanks @BlackFenix2 for the tips in his fork

0reactions
galvesribeirocommented, Jul 31, 2019

The package was published https://www.nuget.org/packages/Blazor.Extensions.Storage/0.2.1

Nuget is indexing and it should be available soon.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot consume scoped service 'Microsoft.JSInterop. ...
I'm creating Blazor project that at first everything works fine until I need to inject IJSRuntime into cs file. Microsoft.JSInterop; ... ...
Read more >
Cannot Consume Scoped Service From Singleton
Scoped means that a new instance will be created essentially per page load (Atleast that's the “scope” within ASP.NET Core). services.AddScoped<MyFatherService> ...
Read more >
Cannot consume scoped service 'Microsoft.JSInterop ...
Coding example for the question InvalidOperationException: Cannot consume scoped service 'Microsoft.JSInterop.IJSRuntime' from singleton '.
Read more >
Posts
This new way of running Blazor had the application hosted on the server with clients connecting over a SignalR connection. All interactions on...
Read more >
Building Web Applications in .NET 6 and Beyond
The use in this publication of trade names, trademarks, service marks, ... at https://github.com/PeterHimschoot/microsoft- blazor- book- 3, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found