MyHttpContext.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Http;
  6. using Microsoft.Extensions.Caching.Memory;
  7. namespace Common.Http
  8. {
  9. public static class MyHttpContext
  10. {
  11. //public static IServiceProvider ServiceProvider;
  12. private static IHttpContextAccessor m_httpContextAccessor;
  13. private static IMemoryCache _cache;
  14. static MyHttpContext()
  15. { }
  16. //public static HttpContext Current1
  17. //{
  18. // get
  19. // {
  20. // object factory = ServiceProvider.GetService(typeof(Microsoft.AspNetCore.Http.IHttpContextAccessor));
  21. // HttpContext context = ((IHttpContextAccessor)factory).HttpContext;
  22. // return context;
  23. // }
  24. //}
  25. public static void Configure(IHttpContextAccessor httpContextAccessor)
  26. {
  27. m_httpContextAccessor = httpContextAccessor;
  28. }
  29. public static void Configure(IMemoryCache cache)
  30. {
  31. _cache = cache;
  32. }
  33. public static HttpContext Current
  34. {
  35. get
  36. {
  37. return m_httpContextAccessor.HttpContext;
  38. }
  39. }
  40. public static IMemoryCache Cache
  41. {
  42. get
  43. {
  44. //object factory = ServiceProvider.GetService(typeof(Microsoft.Extensions.Caching.Memory.IMemoryCache));
  45. //MemoryCache context = ((MemoryCache)factory);
  46. //return context;
  47. return _cache;
  48. }
  49. }
  50. }
  51. }