Note that there are some explanatory texts on larger screens.

plurals
  1. PONInject and multi-threading issue
    primarykey
    data
    text
    <p>We are using Entity framework 4 for our web application and Ninject as our DI container. We have a log table to log everything that happens in our application and also we have backend WCF workflow service which again writes log into log table.</p> <p>For all requests coming from the web we use </p> <pre><code>StandardScopeCallbacks.Request(ctx); </code></pre> <p>and for everything in the backend we use </p> <pre><code>StandardScopeCallbacks.ThreadScope(ctx); </code></pre> <p>our logging method is very simple</p> <pre><code> public void Log(string message, LogLevel level) { var log = new ApplicationLog() { LogDate = DateTime.Now, Message = message, LogLevel = Enum.GetName(typeof(LogLevel), level) }; db.ApplicationLog.Add(log); db.SaveChanges(); } </code></pre> <p>Problem : If we have more threads running then we get an exception</p> <blockquote> <p>System.Data.SqlClient.SqlException: New transaction is not allowed because there are other threads running in the session. at </p> <p>System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) at System.Data.SqlClient.TdsParser.TdsExecuteTransactionManagerRequest(Byte[] buffer, TransactionManagerRequestType request, String transactionName, TransactionManagerIsolationLevel isoLevel, Int32 timeout, SqlInternalTransaction transaction, TdsParserStateObject stateObj, Boolean isDelegateControlRequest) at</p> <p>System.Data.SqlClient.SqlInternalConnectionTds.ExecuteTransactionYukon(TransactionRequest transactionRequest, String transactionName, IsolationLevel iso, SqlInternalTransaction internalTransaction, Boolean isDelegateControlRequest) at </p> <p>System.Data.SqlClient.SqlInternalConnection.BeginSqlTransaction(IsolationLevel iso, String transactionName) at System.Data.SqlClient.SqlInternalConnection.BeginTransaction(IsolationLevel iso) at System.Data.SqlClient.SqlConnection.BeginDbTransaction(IsolationLevel isolationLevel) at System.Data.EntityClient.EntityConnection.BeginDbTransaction(IsolationLevel isolationLevel)</p> </blockquote> <p>I know I can create new context using "using" statement and write ti Log table without any issue. But I don't want to do this as this will make everything slower then creating Context in threadscope.</p> <p>What is the best way to handle such a scenario? I need the implementation that will have least execution time.</p> <p>Please leave a comment if you need more information. Thank you in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload