Error while using IJSRuntime or HttpClient in a seperate .cs file in Blazor WebAssembly!
I am testing a project with Blazor WebAssembly, in a Razor component, I can normally use the IJSRuntime or HttpClient , however, I hope to move those code to a separate file for better management, using dependency injection way. but as a result, program can run to the function but is failed to call a IJSRuntime or HttpClient function which works properly in Razor component. Could you let me know where I did is wrong? Thank you very much!
and this is my JavaScript code for browser data storage:
window.stateManager = {
set: function (key, str) {
window.localStorage[key] = str;
},
get: function (key) {
return window.localStorage[key];
},
getAll: function () {
return window.localStorage;
}
};
window.sessionManager = {
set: function (key, str) {
window.sessionStorage[key] = str;
},
get: function (key) {
return window.sessionStorage[key];
},
getAll: function () {
return window.sessionStorage;
}
};
Every time when run to the code, it gives error as below:
****…………GetLocalBufferValue!!!!!^^^^^System.ArgumentNullException: Value cannot be null.
Parameter name: jsRuntime
at Microsoft.JSInterop.JSRuntimeExtensions.InvokeAsync[TValue] (Microsoft.JSInterop.IJSRuntime jsRuntime, System.String identifier, System.Object[] args) <0x34a72c0 + 0x00014> in <filename unknown>:0
at EnergyManagePro.Client.BaseFiles.JSHelper.GetLocalBufferValue (System.String key) [0x00023] in C:\Users\Tony\source\repos\EnergyManagePro-200723\Client\BaseFiles\CBase.cs:100****
13528-%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE-20200724173558.png