Files
Yi.Admin/Yi.Framework.Net6/Yi.Framework.WebCore/ServiceLocator.cs

27 lines
719 B
C#
Raw Normal View History

2022-09-18 17:22:47 +08:00
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using System;
using Ubiety.Dns.Core.Common;
namespace Yi.Framework.WebCore
{
public static class ServiceLocator
{
2022-10-18 09:01:16 +08:00
public static IServiceProvider? Instance { get; set; }
2022-09-18 17:22:47 +08:00
2022-11-06 21:38:55 +08:00
//需要兼容不存在http请求的情况
2022-10-18 09:01:16 +08:00
public static bool GetHttp(out HttpContext? httpContext)
2022-09-18 17:22:47 +08:00
{
httpContext = null;
2022-10-18 09:01:16 +08:00
var httpContextAccessor = Instance?.GetService<IHttpContextAccessor>();
2022-09-18 17:22:47 +08:00
if (httpContextAccessor is null)
{
return false;
}
httpContext = httpContextAccessor.HttpContext;
return true;
}
}
}