I have a WebAPI project which is using Entity Framework, and the following is the SaveChanges that I overrides to monitor timestamp & etc. I wonder why when I'm moved AddAuditCustomField into the child Method, HttpContext became null. I'm using Audit.NET AuditDbContext.
        public override async Task<int> SaveChangesAsync()
        {
            AddAuditCustomField("url_endpoint",HttpContextHelper.GetUriEndpoint());
            return await SaveChangesAsync(true);
        }
        public async Task<int> SaveChangesAsync(bool invokeEvent)
        {
            try
            {
            //Placing here will be NULL 
           //AddAuditCustomField("url_endpoint",HttpContextHelper.GetUriEndpoint());
            return await SaveChangesAsync(true);
                if (invokeEvent)
                    OnItemSaveChanges?.Invoke();
                AddTimestamps();
                return await base.SaveChangesAsync();
            }
            catch (DbEntityValidationException e)
            {
                throw;
            }
        }
Below is the HttpContextHelper
namespace Test.Core.Helpers
{
    public class HttpContextHelper
    {
        public static  string GetUriEndpoint()
        {
            if (HttpContext.Current != null) return HttpContext.Current.Request.Url.AbsoluteUri;
            if (WebOperationContext.Current != null)
                return WebOperationContext.Current.IncomingRequest.UriTemplateMatch.RequestUri.OriginalString;
            if( HttpContextProvider.Current!=null) return HttpContextProvider.Current.Request.Url.AbsoluteUri;
            return null;
        }
    }
}
The Controller I'm calling it
  var entity = new Entity
                {
                    Name = "test"
                };
                  Db.Entities.Add(entity);
                try
                {
                    await Db.SaveChangesAsync();
                }
                catch (Exception e)
                {
                    if (e.IsDuplicateIndexError())
                    {
                        LogUtil.Error(message);
                    }
                    throw;
                }
Have been reading some other thread and they stated it's working Using HttpContext.Current in WebApi is dangerous because of async
And I'm targetting 4.7.1
 
    