我在c#中编写(我的第一个)Web服务。它是一个中间件桥接我们的旗舰产品和供应商的系统,一个Web服务。使用典型的Web服务请求/响应完成管理工作。一个这样的管理任务是注册某些事件的回调。我的问题涉及正确处理这些入站回调事件 - 它们到达随机托管线程:
5:27:16 PM: <- newEventResponse returned on managed thread id 9
5:27:16 PM: -> notifyUpdated received on managed thread id 9
5:27:16 PM: <- notifyUpdatedResponse returned on managed thread id 9
5:27:16 PM: -> notifyUpdated received on managed thread id 9
5:27:16 PM: <- notifyUpdatedResponse returned on managed thread id 9
5:27:16 PM: -> notifyUpdated received on managed thread id 9
5:27:16 PM: <- notifyUpdatedResponse returned on managed thread id 9
5:27:16 PM: -> newEvent received on managed thread id 9
5:27:19 PM: <- newEventResponse returned on managed thread id 9
5:27:19 PM: -> newEvent received on managed thread id 10
5:27:19 PM: <- newEventResponse returned on managed thread id 10
5:27:19 PM: -> notifyUpdated received on managed thread id 9
5:27:19 PM: <- notifyUpdatedResponse returned on managed thread id 9
5:27:19 PM: -> notifyUpdated received on managed thread id 10
5:27:19 PM: <- notifyUpdatedResponse returned on managed thread id 10
5:27:20 PM: -> newEvent received on managed thread id 9
5:27:20 PM: -> newEvent received on managed thread id 5 **(Doh!)**
我正在使用lock()强制入站事件通过我的处理程序(我认为是)有序地运行,但测试人员抱怨突然缺少事件进入用户界面,导致我的记录(上图)。
public static Object Mutex = new Object();
class DsrCallbackInterfaceImpl : DsrCallbackInterface
{
public notifyUpdatedResponse notifyUpdated(notifyUpdated dsrUpdate)
{
lock (Lifecycle.Mutex)
{
try
{
Lifecycle.AppendLog(string.Format("-> notifyUpdated received on managed thread id {0}", Thread.CurrentThread.ManagedThreadId));
if (!Lifecycle.ShuttingDown)
{
if (Lifecycle.UpdateVisibleDoorList != null)
Lifecycle.UpdateVisibleDoorList(dsrUpdate);
DsrInteractions.DigestDoorUpdate(dsrUpdate);
}
}
catch (Exception ex)
{
Lifecycle.ShowException(ex, "handling notifyUpdated: " + dsrUpdate.ToString());
}
Lifecycle.AppendLog(string.Format("<- notifyUpdatedResponse returned on managed thread id {0}", Thread.CurrentThread.ManagedThreadId));
}
return new notifyUpdatedResponse();
}
public newEventResponse newEvent(newEvent dsrEvent)
{
lock (Lifecycle.Mutex)
{
try
{
Lifecycle.AppendLog(string.Format("-> newEvent received on managed thread id {0}", Thread.CurrentThread.ManagedThreadId));
if (!Lifecycle.ShuttingDown)
{
if (Lifecycle.AddEventToVisibleLog != null)
Lifecycle.AddEventToVisibleLog(dsrEvent);
DsrInteractions.DigestEvent(dsrEvent);
}
}
catch (Exception ex)
{
Lifecycle.ShowException(ex, "handling newEvent: " + dsrEvent.ToString());
}
Lifecycle.AppendLog(string.Format("<- newEventResponse returned on managed thread id {0}", Thread.CurrentThread.ManagedThreadId));
}
return new newEventResponse(); ;
}
}
我想了解我做错了什么,特别是我可以做的是一次将线程限制为一个,因为我认为lock()会这样做。