-
I am having an issue where my HttpClient is getting disposed the third time I attempt to fetch an item from the remote DataPortal. I have a grid populated with a list of info objects. When a grid row is double clicked I retrieve the edit object for editing. The first 2 times I double click a grid row everything works as expected. For some reason the 3rd time and on I get a System.ObjectDisposedException on the HttpClient when the DataPortal attempts to fetch the object I am adding the HttpClient in Program.cs as:
Injection of portal into Blazor component:
Error message on 3rd try and on:
Does anyone have any ideas? Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 22 replies
-
I don't know. It doesn't happen in the BlazorExample app after numerous data portal operations. |
Beta Was this translation helpful? Give feedback.
-
If I change the registration of the HttpClient to AddSingleton instead of AddScoped, I do not have this issue. I do not understand why this would make a difference as the documentation states "Blazor WebAssembly apps don't currently have a concept of DI scopes. Scoped-registered services behave like Singleton services." I have no idea what the ramification will be in making this change. |
Beta Was this translation helpful? Give feedback.
-
@rockfordlhotka I think I found what's causing the HttpClient to get disposed. The _httpClient in HttpProxy is declared as static. The LocalProxy calls DisposeScope at the end of the Create method. This is disposing the static _httpClient in HttpProxy. |
Beta Was this translation helpful? Give feedback.
-
This issue occurs when any code that runs between when the LocalProxy gets created and disposed, instantiates any DataPortal object. When the DataPortal gets instantiates it will create an HttpProxy which has an HttpClient injected into its constructor. The constructor has a Ultimately, as Rocky assumed, this has nothing to do with the ClaimsPrincipalFactory. It just so happens that when using a ClaimsPrincipalFactory, its CreateUserAsync is going to get called whenever a LocalProxy is created (presumably to setup the user in the LocalProxy's context.) Creating an IDataPortal within that method causes it to get added to the LocalProxy's scope and ultimately dispose the HttpClient. I can now re-create the issue without the ClaimsPrincipalFactory, by just injecting an IDataPortal to any businsess object's create method that is set RunLocal. I have updated my sample repo that demonstrates this issue accordingly. I removed the AddMsalAuthentication with the AppClaimsPrincipalFactory from program.cs, and just injected an IDataPortal to the TestEdit object's Create method. Creating a TestEdit object prior to calling any other server side dataportal operation, will cause the operation to fail, because the static _httpClient in HttpProxy is now disposed. This is demonstrated in PersonList.razor where the personListPortal.FetchAsync() fails because it comes after testEditPortal.CreateAsync(). Can this be fixed simply by making the _httpClient in HttpProxy non static, or is it intentionally static and changing it will cause other issues? |
Beta Was this translation helpful? Give feedback.
This issue occurs when any code that runs between when the LocalProxy gets created and disposed, instantiates any DataPortal object. When the DataPortal gets instantiates it will create an HttpProxy which has an HttpClient injected into its constructor. The constructor has a
_httpClient = httpClient;
statement that sets the static _httpClient variable to the new httpClient passed in. When the LocalProxy gets disposed at the end of the Create method, it will dispose of that very same HttpClient that the static _httpClient variable in HttpProxy is now pointing to.Ultimately, as Rocky assumed, this has nothing to do with the ClaimsPrincipalFactory. It just so happens that when using a Claim…