Why my class no work in middleware?

melon NG 291 Reputation points
2022-08-30T08:59:48.62+00:00

I created a class with AddScoped, just like this:
Public class Global
{
public string UA { get=>{ return _UA} }
string _UA;
public Global(IHttpContextAccessor accessor)
{
_UA = accessor.HttpContext?.Request.Headers["User-Agent"].ToString();
}
}

It is to use for store some property of the browser.

However, when I reference this class in middleware, it is just like this:
public class CustomMiddleware
{
readonly RequestDelegate _next;
readonly Global G;
Random R = new Random(DateTime.Now.Millisecond);
public CustomMiddleware(
RequestDelegate next, Global G)
{
_next = next;
this.G = G;
}
public async Task Invoke(HttpContext context)
{
string test=G.UA;
}
}

However, in middleware it always get a blank UA. What's the matter? How can I solve this?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,140 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 55,041 Reputation points
    2022-08-30T15:54:03.24+00:00

    in Global, you are accessing the context in the constructor. this is too early. it should be access during the request, change the code to:

    public class Global      
    {      
         private IHttpContextAccessor _accessor;      
          
         public string UA       
        {       
           get => return _accessor.HttpContext?.Request.Headers["User-Agent"].ToString();   
        }      
          
        public Global(IHttpContextAccessor accessor)      
        {      
            _accessor = accessor;       
        }      
    }      
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Ruikai Feng - MSFT 2,526 Reputation points Microsoft Vendor
    2022-08-31T07:24:39.997+00:00

    Hi,@melon NG
    I tried with your codes and it got an error ,have you missed any codes?
    I Noticed your inject Gloable into your middleware,but you didn't implement the intereface, You could check this document first,
    I tried with two solutions and they both worked,
    Solution1:

        public static class GlobalStatic  
            {  
                public static IServiceProvider ServiceProvider { get; set; }          
                public static string GetUA()  
                {  
                    var accessor = (IHttpContextAccessor)ServiceProvider.GetService(typeof(IHttpContextAccessor));  
                    var UA=accessor.HttpContext?.Request.Headers["User-Agent"].ToString();  
                    return UA;  
                }  
    }  
    

    in startup:

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)  
            {  
                 ......  
                GlobalStatic.ServiceProvider = app.ApplicationServices;  
                .......  
            }  
    

    Solution2:

    public interface IGlobal  
        {  
            string GetUA();  
        }  
        public class Global: IGlobal  
        {  
            private readonly IHttpContextAccessor _accessor;          
            public Global(IHttpContextAccessor accessor)  
            {  
                _accessor = accessor;  
                  
            }  
            public string GetUA()  
            {  
                return _accessor.HttpContext?.Request.Headers["User-Agent"].ToString();  
            }  
        }  
    

    in Startup:

    public void ConfigureServices(IServiceCollection services)  
            {  
                 .....  
                services.AddHttpContextAccessor();  
                services.AddSingleton<IGlobal,Global>();  
                services.AddControllersWithViews();  
               ......  
            }  
    

    MiddleWare:

    public class CustomMiddleware  
    {  
        readonly RequestDelegate _next;  
        readonly IGlobal _G;  
        Random R = new Random(DateTime.Now.Millisecond);  
        public CustomMiddleware(  
        RequestDelegate next,IGlobal G)  
        {  
            _next = next;  
            _G = G;  
             
        }  
        public async Task Invoke(HttpContext context)  
        {  
            string test1 = GlobalStatic.GetUA();  
            string test2 = _G.GetUA();  
            await _next(context);  
        }  
    }  
    

    Result:
    236435-qa831.png

    ----------

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
    Best regards
    RuikaiFeng