private void SendHttpRequest() { // Tie the transport method to the ControlChannelTrigger object to push enable it. // Note that if the transport's TCP connection is broken at a later point of time, // the ControlChannelTrigger object can be reused to plug in a new transport by // calling UsingTransport again. Diag.DebugPrint("Calling UsingTransport() ..."); channel.UsingTransport(httpRequest); // Call the SendRequestAsync function to kick start the TCP connection establishment // process for this HTTP request. Diag.DebugPrint("Calling SendRequestAsync() ..."); sendRequestOperation = httpClient.SendRequestAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead); sendRequestOperation.Completed += OnSendRequestCompleted; // Call WaitForPushEnabled API to make sure the TCP connection has been established, // which will mean that the OS will have allocated any hardware or software slot for this TCP connection. ControlChannelTriggerStatus status = channel.WaitForPushEnabled(); Diag.DebugPrint("WaitForPushEnabled() completed with status: " + status); if (status != ControlChannelTriggerStatus.HardwareSlotAllocated && status != ControlChannelTriggerStatus.SoftwareSlotAllocated) { throw new Exception("Hardware/Software slot not allocated"); } Diag.DebugPrint("Transport is ready to read response from server."); }
private void SendHttpRequest() { if (httpRequest == null) { throw new Exception("HttpRequest object is null"); } // Tie the transport method to the controlchanneltrigger object to push enable it. // Note that if the transport's TCP connection is broken at a later point of time, // the controlchanneltrigger object can be reused to plugin a new transport by // calling UsingTransport API again. Diag.DebugPrint("Calling UsingTransport() ..."); channel.UsingTransport(httpRequest); // Call the SendAsync function to kick start the TCP connection establishment // process for this http request. Task <HttpResponseMessage> httpResponseTask = httpClient.SendAsync(httpRequest); // Call WaitForPushEnabled API to make sure the TCP connection has been established, // which will mean that the OS will have allocated any hardware slot for this TCP connection. ControlChannelTriggerStatus status = channel.WaitForPushEnabled(); Diag.DebugPrint("WaitForPushEnabled() completed with status: " + status); if (status != ControlChannelTriggerStatus.HardwareSlotAllocated && status != ControlChannelTriggerStatus.SoftwareSlotAllocated) { throw new Exception("Hardware/Software slot not allocated"); } Diag.DebugPrint("In SetupHttpRequestAndSendToHttpServer httpResponse is prepared to read response from server."); // IMPORTANT: This sample is noticably different from other transports based on ControlChannelTrigger // The HttpClient receive callback is delivered via a Task to the app since the HttpClient code is purely // managed code based. This means push notification task will fire as soon as the data or error is dispatched // to the application. Hence, in this sample we Enqueue the responseTask returned by httpClient.sendAsync // into a queue that the push notify task will pick up and process inline. AppContext.messageQueue.Enqueue(httpResponseTask); }