【发布时间】:2011-07-27 22:19:33
【问题描述】:
试图测试我的 wcf 服务的可靠性。我正在从客户端循环调用 wcf 服务。 wcf 休息服务(webhttpbinding)做一些数据处理和插入 记录到数据库中。整个操作在一个事务中完成。
如果我将 InstanceContextMode 设置为 PerCall,我发现在大约 60 条消息(从循环内调用服务的 60 倍)中只有 40 条消息通过数据库。没有错误没有例外。消息刚刚被丢弃。
如果我将 InstanceContextMode 设置为 Single,那么我会看到所有消息都到达了 db。 InstanceContextMode.Percall 是有损的预期行为吗?另外,我没有设置并发。任何澄清都会非常有帮助。添加了代码。使用 MySQL 作为数据库...
编辑我的错 - 我刚刚注意到服务器端出现异常 - {"Deadlock found when trying to get lock; try restarting transaction"}
这是因为在进行事务时,对同一记录调用了另一个事务。如果它失败一次,则通过重新启动事务来修复它。
服务
[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;
}
}
客户端只是循环调用服务
【问题讨论】:
-
当你说“没有错误没有例外”时,你的意思是什么?客户端没有看到任何错误? WCF 异常处理程序没有触发?服务方法本身没有异常?
-
客户端没有看到任何错误。服务器方法也不会抛出异常。对服务的一些调用刚刚被丢弃。当我使用 instancecontext 单一模式时,一切正常..
标签: wcf wcf-binding wcf-rest webhttpbinding