Ticket T960767
Visible to All Users

Blazor - How to store HttpContext vars in value manager

created 4 years ago

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.

Answers approved by DevExpress Support

created 4 years ago

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.

    Show previous comments (1)
    Anatol (DevExpress) 4 years ago

      You are welcome!

      Dennis Garavsky (DevExpress) 2 years ago

          thanks for the info, Dennis

          Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

          Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.