1
我在ASP.NET MVC中,並且(主要)使用實體框架。我想調用一個存儲過程而不用等待它完成。我目前的做法是使用後臺工作人員。麻煩的是,它在沒有使用後臺工作的情況下工作正常,但無法執行。
在DoWork事件處理程序中,當我調用 command.ExecuteNonQuery(); 它只是「消失」(在調試模式下永遠不會進入下一行)。 任何人都有異步調用sproc的提示?順便說一句,如果這很重要的話,它將成爲SQL Azure的產品;現在爲SQL Server 2008.無法從BackgroundWorker調用存儲過程
public void ExecAsyncUpdateMemberScoreRecalc(MemberScoreRecalcInstruction instruction)
{
var bw = new BackgroundWorker();
bw.DoWork += new DoWorkEventHandler(AsyncUpdateMemberScoreRecalc_DoWork);
bw.WorkerReportsProgress = false;
bw.WorkerSupportsCancellation = false;
bw.RunWorkerAsync(instruction);
}
private void AsyncUpdateMemberScoreRecalc_DoWork(object sender, DoWorkEventArgs e)
{
var instruction = (MemberScoreRecalcInstruction)e.Argument;
string connectionString = string.Empty;
using (var sprocEntities = new DSAsyncSprocEntities()) // getting the connection string
{
connectionString = sprocEntities.Connection.ConnectionString;
}
using (var connection = new EntityConnection(connectionString))
{
connection.Open();
EntityCommand command = connection.CreateCommand();
command.CommandText = DSConstants.Sproc_MemberScoreRecalc;
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue(DSConstants.Sproc_MemberScoreRecalc_Param_SageUserId, instruction.SageUserId);
command.Parameters.AddWithValue(DSConstants.Sproc_MemberScoreRecalc_Param_EventType, instruction.EventType);
command.Parameters.AddWithValue(DSConstants.Sproc_MemberScoreRecalc_Param_EventCode, instruction.EventCode);
command.Parameters.AddWithValue(DSConstants.Sproc_MemberScoreRecalc_Param_EventParamId, instruction.EventParamId);
int result = 0;
// NEVER RETURNS FROM RUNNING NEXT LINE (and never executes)... yet it works if I do the same thing directly in the main thread.
result = command.ExecuteNonQuery();
}
}
這麼簡單,但如此真實......我在外面嘗試了一下,但是由於某些原因,它從來沒有冒出來。事實證明這是一些基本的東西,所以上面的代碼確實可以做到這一點。對於那些希望使用EF來做到這一點的人,我需要做的另一件事是使用單獨的edmx文件,並且只能從異步線程調用它。 – Vince 2010-11-04 11:51:48