//Utilities protected string GetIncomingValue(WebCallContext context) { if (!Direction.IsSet(WebTokenDirection.Input)) //it is not input token { return(null); } switch (this.TokenType) { case WebTokenType.Cookie: var cookies = context.GetIncomingCookies(this.TokenName); if (cookies == null) { return(null); } if (cookies.Count == 1) { return(cookies[0].Value); } return(string.Join(";", cookies.Select(ck => ck.Value))); case WebTokenType.Header: var values = context.GetIncomingHeader(this.TokenName); if (values == null || values.Count == 0) { return(null); } if (values.Count == 1) { return(values[0]); } return(string.Join(";", values)); }//switch return(null); }
public static void SetHeaders(this HttpResponseMessage response, WebCallContext webContext) { foreach (var kv in webContext.OutgoingHeaders) { if (kv.Key.StartsWith("Content-", StringComparison.OrdinalIgnoreCase)) { if (response.Content == null) { continue; } var headers = response.Content.Headers; if (headers.Contains(kv.Key)) { headers.Remove(kv.Key); } headers.Add(kv.Key, kv.Value); } else { var headers = response.Headers; if (headers.Contains(kv.Key)) { headers.Remove(kv.Key); } headers.Add(kv.Key, kv.Value); } } }
protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { if (IgnoreUri(request.RequestUri)) { return(await base.SendAsync(request, cancellationToken)); } WebCallInfo callInfo = null; WebCallContext webContext = null; try { callInfo = new WebCallInfo(this.App, this.Settings, request, cancellationToken); webContext = callInfo.WebContext; PreprocessRequest(callInfo); OnWebCallStarting(callInfo); // Fire WebCallStarting event - UserSession service will handle it and attach user session and setup UserInfo in OperationContext if (callInfo.Response == null) { callInfo.Response = await base.SendAsync(request, cancellationToken); } OnWebCallEnding(callInfo); PostProcessResponse(callInfo); await LogWebCallInfo(callInfo); return(callInfo.Response); } catch (Exception ex) { LogError(ex, webContext); System.Diagnostics.Debug.WriteLine("Exception: " + ex.ToLogString()); throw; } }//method
public WebCallInfo(EntityApp app, WebCallContextHandlerSettings settings, HttpRequestMessage request, CancellationToken cancellationToken) { Request = request; WebContext = new WebCallContext(request, app.TimeService.UtcNow, app.TimeService.ElapsedMilliseconds, GetIncomingCookies, GetIncomingHeaderValues); WebContext.OperationContext = new OperationContext(app, UserInfo.Anonymous, WebContext, settings.ConnectionReuseMode); WebContext.OperationContext.SetExternalCancellationToken(cancellationToken); Request.Properties[WebCallContext.WebCallContextKey] = WebContext; WebContext.RequestUrl = request.RequestUri.ToString(); WebContext.HttpMethod = request.Method.ToString().ToUpperInvariant(); WebContext.RequestSize = request.Content.GetLength(); //Check if it is one of the sensitive URLs var path = request.RequestUri.LocalPath; // The only way to get IPaddress it seems is thru use of HttpContext (from ASP.NET host). // NOTE: it is available only under ASP.NET/IIS host, not in self-hosting scenario var ctxWrapper = WebHelper.GetHttpContextWrapper(request); if (ctxWrapper != null) { //IIS hosting WebContext.Referrer = ctxWrapper.Request.UrlReferrer + string.Empty; WebContext.IPAddress = ctxWrapper.Request.UserHostAddress; } else { //Self hosting // webCallContext.IPAddress = "(unknown)"; } //Set log level for this call WebContext.OperationContext.LogLevel = settings.LogLevel; }
public override void HandleRequest(WebCallContext context, HttpRequestMessage request) { var versions = GetIncomingValue(context); if (string.IsNullOrWhiteSpace(versions)) { return; } //Parse var arrVersions = versions.Split(','); if (arrVersions.Length < 1) { return; } int value; if (int.TryParse(arrVersions[0], out value)) { context.MinUserSessionVersion = value; } if (arrVersions.Length > 1 && int.TryParse(arrVersions[1], out value)) { context.MinCacheVersion = value; } }
public OperationContext(EntityApp app, UserInfo user = null, WebCallContext webContext = null, ILog log = null, DbConnectionReuseMode connectionMode = DbConnectionReuseMode.NoReuse, ProcessType?processType = null, Guid?processId = null) { App = app; User = user ?? UserInfo.Anonymous; WebContext = webContext; Log = log ?? new DefaultOperationLog(this); DbConnectionMode = connectionMode; ProcessId = processId; if (WebContext != null) { ProcessType = ProcessType.WebRequest; } else { if (processType == null) { ProcessType = (processId == null) ? ProcessType.User : ProcessType.BackgroundProcess; } else { ProcessType = processType.Value; } } }
//log and exceptions public WebCallLogEntry(WebCallContext webCtx) : base(webCtx.OperationContext.LogContext) { WebCallId = Guid.NewGuid(); Request = webCtx.Request; Response = webCtx.Response; Flags = webCtx.Flags; Exception = webCtx.Request.Exception; }
public EntityUnitOfWork(IDbContext context) { _context = context; lock (LockObject) { WebCallContext.SetData(WebCallContextKey, this); } }
private Guid?LogError(Exception exception, WebCallContext webCallInfo) { if (_errorLog == null) { return(null); } var id = _errorLog.LogError(exception, webCallInfo.OperationContext); return(id); }
public OperationContext(EntityApp app, UserInfo user = null, WebCallContext webContext = null, DbConnectionReuseMode connectionMode = DbConnectionReuseMode.NoReuse) { App = app; User = user ?? UserInfo.Anonymous; WebContext = webContext; DbConnectionMode = connectionMode; LocalLog = new MemoryLog(this); Disposables = new ConcurrentDisposableSet(); }
//Used for creating System-level context within user operation public OperationContext(OperationContext parentContext, UserInfo user) { App = parentContext.App; User = user; WebContext = parentContext.WebContext; LocalLog = parentContext.LocalLog; UserSession = parentContext.UserSession; DataSourceName = parentContext.DataSourceName; DbConnectionMode = parentContext.DbConnectionMode; Disposables = parentContext.Disposables; }
public static IUnitOfWork GetOpenEntityUnitOfWork() { var unitOfWork = (EntityUnitOfWork)WebCallContext.GetData(WebCallContextKey); SqlConnectionContext.GetOpenSqlConnection(); if (unitOfWork.Context.TransactionSet == false)//Workaround to check if transaction is opened. { unitOfWork.Context.Database.UseTransaction(SqlConnectionContext.GetTransaction()); unitOfWork.Context.TransactionSet = true; } return(unitOfWork); }
/// <summary>Sets controller info in WebCallInfo object. Call it from ApiController.Init method after calling base Init method. </summary> public static void SetControllerInfo(this WebCallContext webCallContext, ApiController controller) { if (webCallContext == null) // should never happen if you use WebMessageHandler { return; } webCallContext.ControllerName = controller.GetType().Name; object action; // Note: this works for regular routing, not for attribute routing controller.ControllerContext.RouteData.Values.TryGetValue("action", out action); webCallContext.MethodName = string.Empty + action; //safe ToString() method }
private void RetrieveRoutingData(HttpContext httpContext, WebCallContext webCtx) { var routeData = httpContext.GetRouteData(); if (routeData == null) { return; } routeData.Values.TryGetValue("action", out var action); webCtx.Request.HandlerMethodName = action?.ToString(); routeData.Values.TryGetValue("controller", out var contr); webCtx.Request.HandlerControllerName = contr?.ToString(); }
public WebCallLogEntry(WebCallContext webCtx) : base(webCtx.OperationContext, LogEntryType.WebCall) { var ctx = webCtx.OperationContext; this.Id = webCtx.Id; this.CreatedOn = webCtx.CreatedOn; this.Duration = (int)(webCtx.TickCountEnd - webCtx.TickCountStart); this.Url = webCtx.RequestUrl; this.UrlTemplate = webCtx.RequestUrlTemplate; this.UrlReferrer = webCtx.Referrer; this.IPAddress = webCtx.IPAddress; this.ControllerName = webCtx.ControllerName; this.MethodName = webCtx.MethodName; if (webCtx.Exception != null) { this.Error = webCtx.Exception.Message; this.ErrorDetails = webCtx.Exception.ToLogString(); } this.ErrorLogId = webCtx.ErrorLogId; this.HttpMethod = webCtx.HttpMethod; this.HttpStatus = webCtx.HttpStatus; this.RequestSize = webCtx.RequestSize; this.RequestHeaders = webCtx.RequestHeaders; this.Flags = webCtx.Flags; if (webCtx.CustomTags.Count > 0) { this.CustomTags = string.Join(",", webCtx.CustomTags); } if (webCtx.Flags.IsSet(WebCallFlags.HideRequestBody)) { this.RequestBody = "(Omitted)"; } else { this.RequestBody = webCtx.RequestBody; } this.ResponseSize = webCtx.ResponseSize; this.ResponseHeaders = webCtx.ResponseHeaders; if (webCtx.Flags.IsSet(WebCallFlags.HideResponseBody)) { this.ResponseBody = "(Omitted)"; } else { this.ResponseBody = webCtx.ResponseBody; } this.RequestObjectCount = webCtx.RequestObjectCount; this.ResponseObjectCount = webCtx.ResponseObjectCount; }
protected void SetOutgoingValue(WebCallContext context, string value) { if (!Direction.IsSet(WebTokenDirection.Output)) //it is not output token { return; } switch (this.TokenType) { case WebTokenType.Cookie: context.OutgoingCookies.Add(new System.Net.Cookie(this.TokenName, value)); return; case WebTokenType.Header: context.OutgoingHeaders.Add(this.TokenName, value); return; } //switch }
protected override void Initialize(System.Web.Http.Controllers.HttpControllerContext controllerContext) { //Note: when exception is thrown here, it is not routed to exc filter, everything just crashes, // so be careful not to throw anything in controller.Initialize base.Initialize(controllerContext); try { WebContext = WebHelper.GetWebCallContext(this); if (WebContext == null) { return; } OpContext = WebContext.OperationContext; ErrorLog = OpContext.App.GetService <IErrorLogService>(); } catch (Exception ex) { System.Diagnostics.Trace.WriteLine("Exception in controller.Initialize: " + ex.ToLogString()); if (ErrorLog != null) { ErrorLog.LogError(ex, OpContext); } } }
public virtual void WebInitilialize(WebCallContext webContext) { if (_webInitialized) { return; } lock (this) { if (_webInitialized) { return; } try { foreach (var m in Modules) { m.WebInitialize(webContext); } foreach (var linkedApp in LinkedApps) { linkedApp.WebInitilialize(webContext); } } finally { _webInitialized = true; } }//lock }
public override void HandleResponse(WebCallContext context, HttpResponseMessage response) { var versions = context.MinUserSessionVersion + "," + context.MinCacheVersion; context.OutgoingHeaders.Add(this.TokenName, versions); }
public void Log(WebCallContext webContext) { _backgroundSaveService.AddObject(webContext); }
public void Dispose() { _context.Dispose(); WebCallContext.FreeNamedDataSlot(WebCallContextKey, dispose: false); }
private void OnWebCallCompleting(WebCallContext webCtx) { WebCallCompleting?.Invoke(this, new WebCallEventArgs(webCtx)); }
public virtual void HandleResponse(WebCallContext context, HttpResponseMessage response) { }
public virtual void HandleRequest(WebCallContext context, HttpRequestMessage request) { }
protected virtual void Init() { Util.Check(this.ControllerContext != null, "Controller not initialized, ControllerContext is null"); _webContext = base.HttpContext.GetWebCallContext(); }
public WebCallEventArgs(WebCallContext webContext) { WebContext = webContext; }