Exemplo n.º 1
0
        private async void PostTelemetryAsync(BandEvent deviceTelemetry)
        {
            
            try {

                var sas = "SharedAccessSignature sr=YourSAS";
                
                // Namespace info.
                var serviceNamespace = "ServiceBusNamespace";
                var hubName = "EventHubName";
                var publisher = "MSBand";
                var url = string.Format("{0}/publishers/{1}/messages", hubName, deviceTelemetry.SourceBand.Name);

                // Create client.
                var httpClient = new HttpClient();
                httpClient.DefaultRequestHeaders.TryAppendWithoutValidation("Authorization", sas);
                
                var content = new HttpStringContent(JsonConvert.SerializeObject(deviceTelemetry));
                httpClient.DefaultRequestHeaders.Add("ContentType", "application/json");
              

                var result = await httpClient.PostAsync(new Uri(string.Format("https://{0}.servicebus.windows.net/{1}/publishers/{2}/messages", serviceNamespace, hubName, publisher)), content);

                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                    () =>
                    {
                        resultLog.Text = string.Format("Data sent at {0}", DateTime.Now);

                    });
            }
            catch(Exception e)
            {
                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
               () =>
               {
                   resultLog.Text = e.ToString();

               });
            }
        }
Exemplo n.º 2
0
        private async void PostTelemetryAsync(BandEvent deviceTelemetry)
        {
            try {
                var sas = "SharedAccessSignature sr=YourSAS";

                // Namespace info.
                var serviceNamespace = "ServiceBusNamespace";
                var hubName          = "EventHubName";
                var publisher        = "MSBand";
                var url = string.Format("{0}/publishers/{1}/messages", hubName, deviceTelemetry.SourceBand.Name);

                // Create client.
                var httpClient = new HttpClient();
                httpClient.DefaultRequestHeaders.TryAppendWithoutValidation("Authorization", sas);

                var content = new HttpStringContent(JsonConvert.SerializeObject(deviceTelemetry));
                httpClient.DefaultRequestHeaders.Add("ContentType", "application/json");


                var result = await httpClient.PostAsync(new Uri(string.Format("https://{0}.servicebus.windows.net/{1}/publishers/{2}/messages", serviceNamespace, hubName, publisher)), content);

                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                                                                              () =>
                {
                    resultLog.Text = string.Format("Data sent at {0}", DateTime.Now);
                });
            }
            catch (Exception e)
            {
                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                                                                              () =>
                {
                    resultLog.Text = e.ToString();
                });
            }
        }