C# 服务总线IAsyncResult

C# 服务总线IAsyncResult,c#,azure,iasyncresult,C#,Azure,Iasyncresult,我现在正试图使我的应用程序能够从我的服务总线异步接收消息。 但是,有时一个异常是抛出,我不知道如何使它消失,或者它来自什么 例外情况如下: Exception: System.NullReferenceException: Object reference not set to an instance of an object. at GetOffers.DbConnection.processEndReceive(IAsyncResult result); 所以,我知道它来自我的IA

我现在正试图使我的应用程序能够从我的服务总线异步接收消息。 但是,有时一个异常是抛出,我不知道如何使它消失,或者它来自什么

例外情况如下:

Exception: System.NullReferenceException: Object reference not set to an instance of an     object. at GetOffers.DbConnection.processEndReceive(IAsyncResult result);
所以,我知道它来自我的IASync方法,但我无法理解它在哪里不能正常工作

我的客户机配置:

QClient = QueueClient.CreateFromConnectionString(connectionString, queueStr,ReceiveMode.ReceiveAndDelete);
这里是我的IASync方法:

    void processEndReceive(IAsyncResult result)
    {
        try
        {
            QueueClient qc = result.AsyncState as QueueClient;
            BrokeredMessage message = qc.EndReceive(result);
            ForexData data = new ForexData();

            var newtrade = new Trade(data.OfferId, message.Properties["login"].ToString(), message.Properties["amount"].ToString(), message.Properties["instr"].ToString(), message.Properties["type"].ToString(), data.CreationDate, message.Properties["mode"].ToString(), message.Properties["accountID"].ToString(), message.Properties["idTrade"].ToString());
            //Static Variable for the WHERE (sql) in ResponseListener Line 215. 
            idtradetemp = message.Properties["idTrade"].ToString();
            Console.WriteLine("Received message " + message.Label);
            if (newtrade != null)
            {
                try
                {
                    newtrades.Add(newtrade);
                }
                catch
                {
                    Console.WriteLine("Une erreur est survenue lors l'ajout du message dans la liste 'newtrades'");
                }
                Console.WriteLine("Received Delete: " + DateTime.Now);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(string.Format("Exception: {0}", e.ToString()));
            TextWriter tw = new StreamWriter("logServiceBus.txt", true);
            tw.WriteLine("Program terminated on " + DateTime.Now);
            tw.WriteLine("------------------------------------");
            tw.WriteLine(string.Format("Exception: {0}", e.ToString()));
            tw.WriteLine("------------------------------------");
            tw.Close();
        }
    }

    void processEndComplete(IAsyncResult result)
    {
        try
        {
            BrokeredMessage m = result.AsyncState as BrokeredMessage;
            m.EndComplete(result);
            Console.WriteLine("Completed message " + m.Label);
        }
        catch
        { 

        }
    }
基本上,每次他在ServiceBus中检测到新消息时,都会触发这些方法。问题是,我放了一个控制台;当检测到新消息时,但该CW被触发2次(或更多)

所以有两件事:
什么会导致我的错误?如何使IAsync方法在此消息上触发一次。

使用开始/结束对很难正确编程。您是否尝试过更新代码以使用异步/等待模式?服务器上发布的服务以及服务总线1.1的服务总线库也支持基于任务的API。详情请参阅(我是这篇文章的作者)。然后可以使用ReceiveAsync。您可能还想看看onmessageapi,它也可以工作。(文件在这里:)

我会调查的。谢谢