试图测试我的 wcf 服务的可靠性。我正在从客户端循环调用 wcf 服务。wcf 休息服务 (webhttpbinding) 进行一些数据处理并将记录插入数据库。整个操作在一个事务中完成。
如果我将 InstanceContextMode 设置为 PerCall,我发现在大约 60 条消息中(从循环内部调用服务的 60 倍)只有 40 条消息通过数据库。没有错误没有例外。消息只是被丢弃。
如果我将 InstanceContextMode 设置为 Single,那么我会看到所有消息都到达数据库。InstanceContextMode.Percall 是有损的预期行为吗?另外,我没有设置并发。任何澄清都会非常有帮助。添加了代码。使用 MySQL 作为数据库...
编辑我的错 - 我刚刚注意到我在服务器端遇到异常 - {“尝试获取锁定时发现死锁;尝试重新启动事务”}
这是因为在事务正在进行时,在相同记录上调用了另一个事务。如果它失败一次,则通过重新启动事务来修复它。
服务
[WebInvoke(UriTemplate = "employee", Method = "POST")]
public long AddNewEmployee(EmployeeEntity newEmployee)
{
EmployeeRepository eRep = new EmployeeRepository();
return eRep.AddNewEmployee(newEventEntity);
}
存储库类构造函数初始化对象上下文
public EmployeeRepository()
{
bd = new EmployeeEntity();
}
代码 - 服务调用
//bd is the object context
//there are two tables
internal long AddNewEmployee(EmployeeEntity newEmployee)
{
bool tSuccess = false;
using (TransactionScope transaction = new TransactionScope())
{
try
{
//get existing employees
var existingEmployees = from employee
in bd.employees select employee;
List<employee> returnedEmployees = new List<employee>();
//Do some processing
returnedEmployees = DoSomeprocessing(existingEmployees);
//Insert returned employees as updates
foreach (employee e in returnedEmployees)
{
bd.employees.AddObject(e);
}
bd.SaveChanges();
returnedEmployees.Clear();
//add to second table
bd.otherdetails.AddObject(newEmployee);
bd.SaveChanges();
//Transaction Complete
transaction.Complete();
tSuccess = true;
}
catch (Exception e)
{
//return something meaningful
return -1;
}
}
if (tSuccess)
{
//End Transaction
bd.AcceptAllChanges();
return 200;
}
else
{
return -1;
}
}
客户端只是循环调用服务