我正在为我的问题寻找一个有效的解决方案。我正在使用 VS 2010。我想从 wcf 服务方法执行一系列操作,并将每个操作状态发送回调用客户端。我已经设置了带有回调合同的 wcf 并使用了双工通道,我能够连接到 wcf。当我开始长时间运行的操作时,有时会触发回调,有时不会。我不知道为什么。以下是我遵循的方法。
在 wcf 服务方法中,
public void Start()
{
List<Employee> empLists = GetEmpData(); // geting lots of employee objects
foreach(Employee emp in empLists) // maybe 1000 records
{
StartlongRunning(emp);
}
}
private void StartlongRunning(Employee emp)
{
// here i am creating a new background worker...
// Here i am registering for RunWorkerCompleted, DoWork, ReportProgress events...
bgw.RunWorkerAsync(emp)
}
void bgw_DoWork(object sender, DoWorkEventArgs e)
{
Employee emp = (Employee)e.Argument;
using (ClassA p = new ClassA(emp.ID)) // this class is from another dll.
{
e.Result = p.StartProcess(emp.Code);
}
}
void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// This is not calling properly.
// Sometimes this method is working...most of the time it is not working..
// here i am unregistering the registerd DoWork, RunWorkerCompleted events...
// calling bgw.Dispose(); // i tried without this also;...but no use...
// from here I am firing the callback to client...
}
下面是 StartProcess 方法的实现。
public string StartProcess(string empcode)
{
// call to another method1() // here saving to DB frequently. works fine
// call to another method2() // here also saving to DB frequently. works fine
// call to someother method3() // here also some DB insert frequently. fine
// call to Method4() // here also some DB insert.
// this method is not calling frequently..
// sometimes it is calling but most of times not..why ???
return value;
}
private void SaveToDB(args1, args2...)
{
DatabaseHelper.Save(args1, args2.....); // this static class is from another dll
// only DB operation in this static class..
}
这个静态类的实现如下所示。
using (SqlConnection conn = new SqlConnection(DBConnection))
{
conn.open;
using (SqlCommand cmd = conn.CreateCommand())
{
...adding parameters
cmd.ExecuteNonQuery();
}
conn.close();
}
如果StartProcess
返回,则后台工作人员将执行该RunWorker
方法。但它没有发生。这里有什么问题?
我正在ClassA
为每个后台工作人员创建每个对象。一旦一个对象ClassA
完成,那么它就会被释放。
但我不知道为什么StartProcess
没有正确回电。
ClassA
在我的实现中,后台工作人员之间是否存在对象重叠?