0
Я использую глобальный атрибут ActionFilterAttribute для подключения моей логики регистрации, и я хочу сохранить объект журнала в базу данных.ActionFilterAttribute OnActionExecutedAsync сохраняет объект регистратора в базе данных
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public sealed class RequireHttpHeaderAttribute : ActionFilterAttribute
{
public ApplicationDbContext db = new ApplicationDbContext();
public override async Task OnActionExecutedAsync(HttpActionExecutedContext httpActionExecutedContext, CancellationToken cancellationToken)
{
HttpActionExecutedContext executedContext;
var WebServiceRequestURI = httpActionExecutedContext.Request.RequestUri;
var WebServiceRequestMethod = httpActionExecutedContext.Request.Method;
Task<string> content = httpActionExecutedContext.Request.Content.ReadAsStringAsync();
string WebServiceRequestBody = content.Result;
if (WebServiceRequestBody == "")
WebServiceRequestBody = "No-Content";
var UserID = httpActionExecutedContext.Request.GetOwinContext().Authentication.User.Claims.First().Value;
// Get the sender address
var myRequest = ((HttpContextWrapper)httpActionExecutedContext.Request.Properties["MS_HttpContext"]).Request;
var IPAddress = myRequest.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(IPAddress))
{
string[] ipRange = IPAddress.Split(',');
int le = ipRange.Length - 1;
string trueIP = ipRange[le];
}
else
{
IPAddress = myRequest.ServerVariables["REMOTE_ADDR"];
}
var webserviceRequestDate = DateTime.Now;
FMBP_ActionLog fMBP_ActionLog = new FMBP_ActionLog();
fMBP_ActionLog.WebServiceRequestString = WebServiceRequestURI.ToString();
fMBP_ActionLog.WebServiceRequestMethod = WebServiceRequestMethod.ToString();
fMBP_ActionLog.WebServiceRequestBody = WebServiceRequestBody;
fMBP_ActionLog.WebServiceRequestDate = DateTime.Now;
fMBP_ActionLog.IPAddress = IPAddress;
fMBP_ActionLog.UserID = UserID;
fMBP_ActionLog.StatusCode = (int)httpActionExecutedContext.Response.StatusCode;
db.FMBP_ActionLog.Add(fMBP_ActionLog);
db.SaveChanges();
}
}
Каким образом можно сохранить FMBP_ActionLog в базу данных? Я получаю ряд случайных ошибок, подобных этому
"ExceptionMessage":"The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state.
Я действительно не вижу, откуда происходит 'db'? Можете ли вы включить код, в котором вы инициализируете свой DbContext, пожалуйста? – timothyclifford
@timothyclifford - добавлен весь класс – C4CodeE4Exe