public async Task <HttpResponseMessage> ExecuteAuthorizationFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func <Task <HttpResponseMessage> > continuation) { string action = ProfilerActionSplitterAttribute.GetActionDescription(actionContext); using (TimeTracker.Start(action)) { using (HeavyProfiler.Log("Web.API " + actionContext.Request.Method, () => actionContext.Request.RequestUri.ToString())) { //if (ProfilerLogic.SessionTimeout != null) //{ // IDisposable sessionTimeout = Connector.CommandTimeoutScope(ProfilerLogic.SessionTimeout.Value); // if (sessionTimeout != null) // actionContext.Request.RegisterForDispose(sessionTimeout); //} actionContext.Request.Properties[SavedRequestKey] = await actionContext.Request.Content.ReadAsStringAsync(); using (Authenticate == null ? null : Authenticate(actionContext)) { if (actionContext.Response != null) { return(actionContext.Response); } return(await continuation()); } } } }
protected internal override object ExecuteScalar(SqlPreCommandSimple preCommand, CommandType commandType) { using (SqlConnection con = EnsureConnection()) using (SqlCommand cmd = NewCommand(preCommand, con, commandType)) using (HeavyProfiler.Log("SQL", () => preCommand.PlainSql())) { try { object result = cmd.ExecuteScalar(); if (result == null || result == DBNull.Value) { return(null); } return(result); } catch (Exception ex) { var nex = HandleException(ex, preCommand); if (nex == ex) { throw; } throw nex; } } }
public static int UnsafeDelete <T>(this IQueryable <T> query, string message = null) where T : Entity { if (message != null) { return(SafeConsole.WaitRows(message == "auto" ? $"Deleting {typeof(T).TypeName()}" : message, () => query.UnsafeDelete(message: null))); } using (HeavyProfiler.Log("DBUnsafeDelete", () => typeof(T).TypeName())) { if (query == null) { throw new ArgumentNullException("query"); } using (Transaction tr = new Transaction()) { Schema.Current.OnPreUnsafeDelete <T>(query); int rows = DbQueryProvider.Single.Delete(query, sql => (int)sql.ExecuteScalar()); return(tr.Commit(rows)); } } }
public static int UnsafeDeleteMList <E, V>(this IQueryable <MListElement <E, V> > mlistQuery, string message = null) where E : Entity { if (message != null) { return(SafeConsole.WaitRows(message == "auto" ? $"Deleting MList<{typeof(V).TypeName()}> in {typeof(E).TypeName()}" : message, () => mlistQuery.UnsafeDeleteMList(message: null))); } using (HeavyProfiler.Log("DBUnsafeDelete", () => typeof(MListElement <E, V>).TypeName())) { if (mlistQuery == null) { throw new ArgumentNullException("query"); } using (Transaction tr = new Transaction()) { Schema.Current.OnPreUnsafeMListDelete <E>(mlistQuery, mlistQuery.Select(mle => mle.Parent)); int rows = DbQueryProvider.Single.Delete(mlistQuery, sql => (int)sql.ExecuteScalar()); return(tr.Commit(rows)); } } }
public static List <Lite <T> > RetrieveListLite <T>(List <PrimaryKey> ids) where T : Entity { using (HeavyProfiler.Log("DBRetrieve", () => "List<Lite<{0}>>".FormatWith(typeof(T).TypeName()))) { if (ids == null) { throw new ArgumentNullException("ids"); } var cc = GetCacheController <T>(); if (cc != null && GetFilterQuery <T>() == null) { return(ids.Select(id => (Lite <T>) new LiteImp <T>(id, cc.GetToString(id))).ToList()); } var retrieved = ids.GroupsOf(Schema.Current.Settings.MaxNumberOfParameters).SelectMany(gr => Database.Query <T>().Where(a => gr.Contains(a.Id)).Select(a => a.ToLite())).ToDictionary(a => a.Id); var missing = ids.Except(retrieved.Keys); if (missing.Any()) { throw new EntityNotFoundException(typeof(T), missing.ToArray()); } return(ids.Select(id => retrieved[id]).ToList()); //Preserve order } }
public static T Save <T>(this T entity) where T : class, IEntity { if (entity == null) { throw new ArgumentNullException("entity"); } try { using (new EntityCache()) using (HeavyProfiler.Log("DBSave", () => "Save<{0}>".FormatWith(typeof(T).TypeName()))) using (Transaction tr = new Transaction()) { Saver.Save((Entity)(IEntity)entity); return(tr.Commit(entity)); } } catch (Exception e) { e.Data["entity"] = ((Entity)(IEntity)entity).BaseToString(); throw; } }
public static Lite <T> RetrieveLite <T>(PrimaryKey id) where T : Entity { using (HeavyProfiler.Log("DBRetrieve", () => "Lite<{0}>".FormatWith(typeof(T).TypeName()))) { try { var cc = GetCacheController <T>(); if (cc != null && GetFilterQuery <T>() == null) { return(new LiteImp <T>(id, cc.GetToString(id))); } var result = Database.Query <T>().Select(a => a.ToLite()).SingleOrDefaultEx(a => a.Id == id); if (result == null) { throw new EntityNotFoundException(typeof(T), id); } return(result); } catch (Exception e) { e.Data["type"] = typeof(T).TypeName(); e.Data["id"] = id; throw; } } }
public static int UnsafeInsertMList <T, E, V>(this IQueryable <T> query, Expression <Func <E, MList <V> > > mListProperty, Expression <Func <T, MListElement <E, V> > > constructor, string message = null) where E : Entity { if (message != null) { return(SafeConsole.WaitRows(message == "auto" ? $"Inserting MList<{ typeof(V).TypeName()}> in { typeof(E).TypeName()}" : message, () => query.UnsafeInsertMList(mListProperty, constructor, message: null))); } using (HeavyProfiler.Log("DBUnsafeInsert", () => typeof(E).TypeName())) { if (query == null) { throw new ArgumentNullException("query"); } if (constructor == null) { throw new ArgumentNullException("constructor"); } using (Transaction tr = new Transaction()) { constructor = (Expression <Func <T, MListElement <E, V> > >)Schema.Current.OnPreUnsafeInsert(typeof(E), query, constructor, query.Select(constructor).Select(c => c.Parent)); var table = ((FieldMList)Schema.Current.Field(mListProperty)).TableMList; int rows = DbQueryProvider.Single.Insert(query, constructor, table, sql => (int)sql.ExecuteScalar()); return(tr.Commit(rows)); } } }
public static string WikiParse(this WikiSettings settings, string content) { using (HeavyProfiler.Log("Wiki")) using (var t = HeavyProfiler.LogNoStackTrace("SaveCodeRegions")) { string result = content; result = SaveCodeRegions(result, out Dictionary <string, string> codeRegions); if (settings.EncodeHtml) { t.Switch("HtmlEncode"); //1: Replace token delimiters which are different from their encoded string so that they are not encoded result = Regex.Replace(result, "'{2,}", m => "####" + m.Length + "####"); //2: Encode all text result = HttpUtility.HtmlEncode(result); //3: Replace encrypted tokens to original tokens result = Regex.Replace(result, "####(?<count>\\d+)####", m => new string('\'', int.Parse(m.Groups["count"].Value))); } t.Switch("ProcessTokens"); result = ProcessTokens(result, settings); t.Switch("ProcessFormat"); result = ProcessFormat(result, settings); t.Switch("WriteCodeRegions"); result = WriteCodeRegions(result, codeRegions, settings); return(result.Trim()); } }
protected internal override DataSet ExecuteDataSet(SqlPreCommandSimple preCommand, CommandType commandType) { using (SqlConnection con = EnsureConnection()) using (SqlCommand cmd = NewCommand(preCommand, con, commandType)) using (HeavyProfiler.Log("SQL", () => preCommand.PlainSql())) { try { SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet result = new DataSet(); da.Fill(result); return(result); } catch (Exception ex) { var nex = HandleException(ex, preCommand); if (nex == ex) { throw; } throw nex; } } }
public virtual void SaveFile(IFilePath fp) { using (HeavyProfiler.Log("AzureBlobStorage SaveFile")) using (new EntityCache(EntityCacheType.ForceNew)) { if (WeakFileReference) { return; } BlobContainerClient client = CalculateSuffixWithRenames(fp); try { var action = this.BlobAction(fp); client.GetBlobClient(fp.Suffix).Upload(new MemoryStream(fp.BinaryFile), httpHeaders: GetBlobHttpHeaders(fp, action)); } catch (Exception ex) { ex.Data.Add("Suffix", fp.Suffix); ex.Data.Add("AccountName", client.AccountName); ex.Data.Add("ContainerName", client.Name); } } }
public async Task <HttpResponseMessage> ExecuteAuthorizationFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func <Task <HttpResponseMessage> > continuation) { string action = ProfilerActionSplitterAttribute.GetActionDescription(actionContext); try { using (TimeTracker.Start(action)) { using (HeavyProfiler.Log("Web.API " + actionContext.Request.Method, () => actionContext.Request.RequestUri.ToString())) { var user = (IUserEntity)GetProp(actionContext, SignumAuthenticationFilterAttribute.UserKey); using (user != null ? UserHolder.UserSession(user) : null) { var culture = (CultureInfo)SignumAuthenticationFilterAttribute.GetCurrentCultures?.Invoke(actionContext); using (culture != null ? CultureInfoUtils.ChangeBothCultures(culture) : null) { var result = await continuation(); return(result); } } } } } finally { Statics.CleanThreadContextAndAssert(); } }
protected internal override DataTable ExecuteDataTable(SqlPreCommandSimple preCommand, CommandType commandType) { return(EnsureConnectionRetry(con => { using (NpgsqlCommand cmd = NewCommand(preCommand, con, commandType)) using (HeavyProfiler.Log("SQL", () => preCommand.sp_executesql())) { try { NpgsqlDataAdapter da = new NpgsqlDataAdapter(cmd); DataTable result = new DataTable(); da.Fill(result); return result; } catch (Exception ex) { var nex = HandleException(ex, preCommand); if (nex == ex) { throw; } throw nex; } } })); }
protected internal override object?ExecuteScalar(SqlPreCommandSimple preCommand, CommandType commandType) { return(EnsureConnectionRetry(con => { using (NpgsqlCommand cmd = NewCommand(preCommand, con, commandType)) using (HeavyProfiler.Log("SQL", () => preCommand.sp_executesql())) { try { object?result = cmd.ExecuteScalar(); if (result == null || result == DBNull.Value) { return null; } return result; } catch (Exception ex) { var nex = HandleException(ex, preCommand); if (nex == ex) { throw; } throw nex; } } })); }
public static ResetLazy <T> WithoutInvalidations <T>(Func <T> func, LazyThreadSafetyMode mode = LazyThreadSafetyMode.ExecutionAndPublication) { ResetLazy <T> result = new ResetLazy <T>(() => { using (ExecutionMode.Global()) using (HeavyProfiler.Log("ResetLazy", () => typeof(T).TypeName())) using (Transaction? tr = Transaction.InTestTransaction ? null : Transaction.ForceNew()) using (new EntityCache(EntityCacheType.ForceNewSealed)) { var value = func(); if (tr != null) { tr.Commit(); } return(value); } }, mode, declaringType: func.Method.DeclaringType); registeredLazyList.Add(result); return(result); }
internal static bool WakeUp(string reason, SqlNotificationEventArgs?args) { using (HeavyProfiler.Log("WakeUp", () => "WakeUp! " + reason + ToString(args))) { return(autoResetEvent.Set()); } }
public static int BulkInsert <T>(this IEnumerable <T> entities, SqlBulkCopyOptions copyOptions = SqlBulkCopyOptions.Default, bool preSaving = true, bool validateFirst = true, bool disableIdentity = false, int?timeout = null, string?message = null) where T : Entity { using (HeavyProfiler.Log(nameof(BulkInsert), () => typeof(T).TypeName())) using (Transaction tr = new Transaction()) { var table = Schema.Current.Table(typeof(T)); if (!disableIdentity && table.IdentityBehaviour && table.TablesMList().Any()) { throw new InvalidOperationException($@"Table {typeof(T)} contains MList but the entities have no IDs. Consider: * Using BulkInsertQueryIds, that queries the inserted rows and uses the IDs to insert the MList elements. * Set {nameof(disableIdentity)} = true, and set manually the Ids of the entities before inseting using {nameof(UnsafeEntityExtensions.SetId)}. * If you know what you doing, call ${nameof(BulkInsertTable)} manually (MList wont be saved)." ); } var list = entities.ToList(); var rowNum = BulkInsertTable(list, copyOptions, preSaving, validateFirst, disableIdentity, timeout, message); BulkInsertMLists <T>(list, copyOptions, timeout, message); return(tr.Commit(rowNum)); } }
public static R Return <S, R>(Func <S, R> function) where S : class { retry: AssertConnected(); S server = current as S; if (server == null) { throw new InvalidOperationException("Server {0} does not implement {1}".FormatWith(current.GetType(), typeof(S))); } try { using (HeavyProfiler.Log("WCFClient", () => "Return(({0} server)=>{1})".FormatWith(typeof(S).TypeName(), typeof(R).TypeName()))) using (CreateOperationContext((IContextChannel)current)) { return(function(server)); } } catch (MessageSecurityException e) { HandleSessionException(e); current = null; goto retry; } }
public async Task FillAsync(Dictionary <LookupToken, IEnumerable> lookups, IRetriever retriever, CancellationToken token) { using (HeavyProfiler.Log("SQL", () => Command.sp_executesql())) using (var reader = await Executor.UnsafeExecuteDataReaderAsync(Command, token: token)) { ProjectionRowEnumerator <KeyValuePair <K, V> > enumerator = new ProjectionRowEnumerator <KeyValuePair <K, V> >(reader.Reader, ProjectorExpression, lookups, retriever, token); IEnumerable <KeyValuePair <K, V> > enumerabe = new ProjectionRowEnumerable <KeyValuePair <K, V> >(enumerator); try { var lookUp = enumerabe.ToLookup(a => a.Key, a => a.Value); lookups.Add(Token, lookUp); } catch (Exception ex) when(!(ex is OperationCanceledException)) { FieldReaderException fieldEx = enumerator.Reader.CreateFieldReaderException(ex); fieldEx.Command = Command; fieldEx.Row = enumerator.Row; fieldEx.Projector = ProjectorExpression; throw fieldEx; } } }
public static void Execute <S>(Action <S> action) where S : class { retry: AssertConnected(); S server = current as S; if (server == null) { throw new InvalidOperationException("Server does not implement {0}".FormatWith(typeof(S))); } try { using (HeavyProfiler.Log("WCFClient", () => "{0}".FormatWith(typeof(S).TypeName()))) using (CreateOperationContext((IContextChannel)current)) { action(server); } } catch (MessageSecurityException e) { HandleSessionException(e); current = null; goto retry; } }
protected virtual void SendSMTP(EmailMessageEntity email, SmtpEmbedded smtp) { System.Net.Mail.MailMessage message = CreateMailMessage(email); using (HeavyProfiler.Log("SMTP-Send")) smtp.GenerateSmtpClient().Send(message); }
public void Fill(Dictionary <LookupToken, IEnumerable> lookups, IRetriever retriever) { using (HeavyProfiler.Log("SQL", () => Command.Sql)) using (DbDataReader reader = Executor.UnsafeExecuteDataReader(Command)) { ProjectionRowEnumerator <KeyValuePair <K, V> > enumerator = new ProjectionRowEnumerator <KeyValuePair <K, V> >(reader, ProjectorExpression, lookups, retriever); IEnumerable <KeyValuePair <K, V> > enumerabe = new ProjectionRowEnumerable <KeyValuePair <K, V> >(enumerator); try { var lookUp = enumerabe.ToLookup(a => a.Key, a => a.Value); lookups.Add(Token, lookUp); } catch (Exception ex) { FieldReaderException fieldEx = enumerator.Reader.CreateFieldReaderException(ex); fieldEx.Command = Command; fieldEx.Row = enumerator.Row; fieldEx.Projector = ProjectorExpression; throw fieldEx; } } }
protected internal override int ExecuteNonQuery(SqlPreCommandSimple preCommand, CommandType commandType) { return(EnsureConnectionRetry(con => { using (NpgsqlCommand cmd = NewCommand(preCommand, con, commandType)) using (HeavyProfiler.Log("SQL", () => preCommand.sp_executesql())) { try { int result = cmd.ExecuteNonQuery(); return result; } catch (Exception ex) { var nex = HandleException(ex, preCommand); if (nex == ex) { throw; } throw nex; } } })); }
public static int BulkInsertMListTable <E, V>( List <E> entities, Expression <Func <E, MList <V> > > mListProperty, SqlBulkCopyOptions copyOptions = SqlBulkCopyOptions.Default, int?timeout = null, string?message = null) where E : Entity { using (HeavyProfiler.Log(nameof(BulkInsertMListTable), () => $"{mListProperty} ({typeof(E).TypeName()})")) { try { var func = mListProperty.Compile(); var mlistElements = (from e in entities from mle in func(e).Select((iw, i) => new MListElement <E, V> { Order = i, Element = iw, Parent = e, }) select mle).ToList(); return(BulkInsertMListTable(mlistElements, mListProperty, copyOptions, timeout, updateParentTicks: false, message: message)); } catch (InvalidOperationException e) when(e.Message.Contains("has no Id")) { throw new InvalidOperationException($"{nameof(BulkInsertMListTable)} requires that you set the Id of the entities manually using {nameof(UnsafeEntityExtensions.SetId)}"); throw; } } }
public static List <T> RetrieveList <T>(List <PrimaryKey> ids) where T : Entity { using (HeavyProfiler.Log("DBRetrieve", () => "List<{0}>".FormatWith(typeof(T).TypeName()))) { if (ids == null) { throw new ArgumentNullException("ids"); } List <PrimaryKey> remainingIds; Dictionary <PrimaryKey, T> result = null; if (EntityCache.Created) { result = ids.Select(id => EntityCache.Get <T>(id)).NotNull().ToDictionary(a => a.Id); if (result.Count == 0) { remainingIds = ids; } else { remainingIds = ids.Where(id => !result.ContainsKey(id)).ToList(); } } else { remainingIds = ids; } if (remainingIds.Count > 0) { var retrieved = RetrieveFromDatabaseOrCache <T>(remainingIds).ToDictionary(a => a.Id); var missing = ids.Except(retrieved.Keys); if (missing.Any()) { throw new EntityNotFoundException(typeof(T), missing.ToArray()); } if (result == null) { result = retrieved; } else { result.AddRange(retrieved); } } else { if (result == null) { result = new Dictionary <PrimaryKey, T>(); } } return(ids.Select(id => result[id]).ToList()); //Preserve order } }
public static void ForEachLine <T>(this ExecutingProcess executingProcess, IQueryable <T> remainingLines, Action <T> action, int groupsOf = 100) where T : Entity, IProcessLineDataEntity, new() { var remainingNotExceptionsLines = remainingLines.Where(li => li.Exception(executingProcess.CurrentProcess) == null); var totalCount = remainingNotExceptionsLines.Count(); int j = 0; while (true) { List <T> lines = remainingNotExceptionsLines.Take(groupsOf).ToList(); if (lines.IsEmpty()) { return; } for (int i = 0; i < lines.Count; i++) { executingProcess.CancellationToken.ThrowIfCancellationRequested(); T pl = lines[i]; using (HeavyProfiler.Log("ProcessLine", () => pl.ToString())) { try { Transaction.ForceNew().EndUsing(tr => { action(pl); tr.Commit(); }); } catch (Exception e) { if (Transaction.InTestTransaction) { throw; } var exLog = e.LogException(); Transaction.ForceNew().EndUsing(tr => { new ProcessExceptionLineEntity { Exception = exLog.ToLite(), Line = pl.ToLite(), Process = executingProcess.CurrentProcess.ToLite() }.Save(); tr.Commit(); }); } executingProcess.ProgressChanged(j++, totalCount); } } } }
public BlobProperties GetProperties(IFilePath fp) { using (HeavyProfiler.Log("AzureBlobStorage GetProperties")) { var client = GetClient(fp); return(client.GetBlobClient(fp.Suffix).GetProperties()); } }
public byte[] ReadAllBytes(IFilePath fp) { using (HeavyProfiler.Log("AzureBlobStorage ReadAllBytes")) { var client = GetClient(fp); return(client.GetBlobClient(fp.Suffix).Download().Value.Content.ReadAllBytes()); } }
public Stream OpenRead(IFilePath fp) { using (HeavyProfiler.Log("AzureBlobStorage OpenRead")) { var client = GetClient(fp); return(client.GetBlobClient(fp.Suffix).Download().Value.Content); } }
public async Task <object> ExecuteAsync(CancellationToken token) { using (new EntityCache()) using (Transaction tr = new Transaction()) { object result; using (var retriever = EntityCache.NewRetriever()) { if (EagerProjections.Any() || LazyChildProjections.Any()) { lookups = new Dictionary <LookupToken, IEnumerable>(); } foreach (var child in EagerProjections) { child.Fill(lookups, retriever); } using (HeavyProfiler.Log("SQL", () => MainCommand.PlainSql())) using (DbDataReader reader = await Executor.UnsafeExecuteDataReaderAsync(MainCommand, token: token)) { ProjectionRowEnumerator <T> enumerator = new ProjectionRowEnumerator <T>(reader, ProjectorExpression, lookups, retriever, token); IEnumerable <T> enumerable = new ProjectionRowEnumerable <T>(enumerator); try { if (Unique == null) { result = enumerable.ToList(); } else { result = UniqueMethod(enumerable, Unique.Value); } } catch (Exception ex) when(!(ex is OperationCanceledException)) { FieldReaderException fieldEx = enumerator.Reader.CreateFieldReaderException(ex); fieldEx.Command = MainCommand; fieldEx.Row = enumerator.Row; fieldEx.Projector = ProjectorExpression; throw fieldEx; } } foreach (var child in LazyChildProjections) { child.Fill(lookups, retriever); } retriever.CompleteAll(); } return(tr.Commit(result)); } }