Service Bus IAsyncResult

125 Views Asked by At

I'm curently trying to make my App able to receive Asyncronously message from my Service Bus. But, Sometimes an Exception is Throw and i don't know how to make it disapear or from what it can come from.

Here's the exception:

Exception: System.NullReferenceException: Object reference not set to an instance of an     object. at GetOffers.DbConnection.processEndReceive(IAsyncResult result);

So, I know it come from my IASync method, but I'm not able to understand where it don't work properly.

My QueueClient conf:

QClient = QueueClient.CreateFromConnectionString(connectionString, queueStr,ReceiveMode.ReceiveAndDelete);

Here my both IASync Method:

    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
        { 

        }
    }

Basicaly those method are Trigger everytime he detect a new message in the ServiceBus. The thing is, I've put a Console.WriteLine("New Message"); when a new message are detected, but, this CW are trigger 2times (or more).

So One of two things: What can cause my error ? And HOW can I make my IAsync method to just be trigger once ON THIS message.

1

There are 1 best solutions below

0
On

Using the Begin/End pairs can be difficult to program correctly. Have you tried updating your code to use the async/await pattern? Service Bus libraries for the released service as well as for Service Bus 1.1 on server also support a Task based API. See http://www.windows-azure.net/task-based-apis-for-service-bus/ for details (I'm the author of that post). You can then use ReceiveAsync. You may also want to look at the OnMessage API which will also work. (docs are here: http://msdn.microsoft.com/en-us/library/microsoft.servicebus.messaging.queueclient.onmessage.aspx)