异步WCF响应丢失

时间:2012-02-28 07:47:43

标签: c# wcf long-polling

我正在使用WCF服务在托管WCF服务的服务器和多个客户端之间应用长轮询连接。现在,我只测试一个客户端。我从客户端调用的方法是异步的,它包含一个ManualResetEvent,它将从另一个方法发出信号。

以下是异步方法的代码:

public IAsyncResult BeginWaitNotification(Guid printServerId, List<Guid> printers ,AsyncCallback callback, object state)
{
    var notRes = new NotificationResult(callback, state, printServerId);
    lock (SyncObject)
    {
        if ((from p in ConnectedPrintServers where p.PrintServerId == printServerId select p).FirstOrDefault() == null)
            ConnectedPrintServers.Add(new PrinterHandlePair { PrintServerId = printServerId, Handle = notRes, Printers = printers });
    }
    //wait for a notification from InvokePrint
    notRes.WaitForResult();
    return notRes;
}

public string EndWaitNotification(IAsyncResult result)
{
    var myResult = result as NotificationResult;
    if (myResult == null)
        throw new ArgumentException("Result was of the wrong type!");
    lock (SyncObject)
    {
        var printerPair = (from p in ConnectedPrintServers where p.PrintServerId == myResult.PrinterId select p).FirstOrDefault();
        if (printerPair == null)
            return null;
        ConnectedPrintServers.Remove(printerPair);
    }
    return "test";
}

这是另一种方法的方法,它将发送信号:

public PrintData InvokePrint(PrintData printData)
{
    var returnData = printData;
    if (printData == null)
        return null;
    lock(SyncObject)
    {
        //finds a printerhandlepair
        var printerPair = (from p in ConnectedPrintServers where p.Printers.Contains(printData.Printer) select p).FirstOrDefault();
        if (printerPair == null)
            return returnData;
        //notify long polling method so it returns
            ((ManualResetEvent)printerPair.Handle.AsyncWaitHandle).Set();
        return returnData;
    }
}

这些方法完全符合预期。我看到它们都返回了。问题在于客户倾听。

我使用以下代码调用WaitNotification:

_asyncPsc = new AsyncPrintServiceClient(new BasicHttpBinding { SendTimeout = TimeSpan.FromSeconds(1900), }, new EndpointAddress(url));
var result = _asyncPsc.WaitNotification(PrintServerId, Printers.ToArray());

有时,不是每次都看到_asyncPsc.WaitNotification(PrintServerId, Printers.ToArray());被调用,但永远不会回来。甚至当我看到EndWaitNotification返回时也是如此。可能是造成这种行为的原因??

如果我在运行之前使用任务管理器关闭w3wp.exe,我没有任何问题。

0 个答案:

没有答案