It seems pretty well documented that the HttpContext in Blazor will be null on a background thread (such as a timer) and that is what I am experiencing. I have a requirement to poll a web api service instance (the url is stored in the database) on an interval to get status updates. The challenge is that my httpclient named client is registered with a custom message handler that obtains tokens from the httpcontext and this is expectedly null when it runs in a timer. I have some plumbing in the middleware to handle expired tokens so I want to use that code and not have to get the token and pass it to my service which then would need to be refreshed manually. The more I read, it seems that the recommendation is to not do something like this in the UI. It seems kind of crazy that I can’t design a lightweight method that I can poll on a web api regularly to get status updates from a web ui. Can someone point me in the right direction? My code is highly complicated but if someone needs me to put together a small sample, I could do that for more context.
I attempted to use a timer to call my web api using an injected IHttpContextFactory so I could create my named client with my url from the database. When I call the method from the UI thread it works fine but when I execute the method in a callback while instantiating my timer, the httpcontext is always null when the middleware tries to obtain the token for the web api call. I figure I could get the token on the ui thread and then pass it to my method but this page could very likely be up for more than the expiration of the token so I would prefer this to use my token refresh code.
4