Hello
I need to resolve using http context in my module (to get phisycal path and base url path). I get my module helper class and make it singleton storing with valueManager and add Init method to set delegates from web and blazor methods to get data from HttpContext.
For web app it works, but for blazor (I'm using this solution to get context https://www.strathweb.com/2016/12/accessing-httpcontext-outside-of-framework-components-in-asp-net-core/)) I get exception on try to call ValueManager on Startup.Configure call app.UseStaticHttpContext(); after app.UseXaf();
So I need to somehow use ValueManager in Startup.Configure method or get httpContext when the valueManager ready.
I think I also can to get rid using httpContext in module at all (it maybe even more preferable solution), but not sure how to set this vars to using it globally per session in module.
Blazor - How to store HttpContext vars in value manager
Answers approved by DevExpress Support
Hello Andrey,
XAF initializes ValueManager's storage in a middleware class. Thus, you need to wait until ASP.NET Core invokes this middleware to use ValueManager. To access HttpContext after this, create your own middleware and register it by calling the UseMiddleware method after the UseXaf method:
C#using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;
namespace MainDemo.Blazor.ServerSide {
public class AppSetupMiddleware {
private readonly RequestDelegate _next;
public AppSetupMiddleware(RequestDelegate next) {
_next = next;
}
public async Task InvokeAsync(HttpContext context) {
// do something
await _next(context);
}
}
}
...
public class Startup {
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
...
app.UseXaf();
app.UseMiddleware<AppSetupMiddleware>();
Please refer to the following help topic for additional information: Write custom ASP.NET Core middleware.
Let me know if you have any questions.
JFYI: We published the following documentation on the subject: Core - InvalidOperationException (ValueManagerContext.Storage is null) may occur in XAF Blazor, Web API Service, and other non-XAF UI apps.