private async void button3_Click(object sender, EventArgs e) { try { string pdffilename = System.IO.Path.GetFileName(openFileDialog1.FileName); string metadatafilename = System.IO.Path.GetFileName(openFileDialog2.FileName); if (pdffilename == null || metadatafilename == null) { MessageBox.Show("Please select both pdf & Metadata files."); } else { string path = Application.StartupPath.Substring(0, (Application.StartupPath.Length - 10)); System.IO.File.Copy(openFileDialog1.FileName, path + "\\Documents\\" + pdffilename); string currentDirectory = Directory.GetCurrentDirectory(); string filePath = System.IO.Path.Combine(path, "Documents", pdffilename); string BlobStorageConnection = ConfigurationManager.AppSettings["BlobStorageConnection"]; // await BlobStorage.SavePdf(BlobStorageConnection, filePath); string EventHubConnectionString = ConfigurationManager.AppSettings["EventHubConnectionString"]; System.IO.File.Copy(openFileDialog2.FileName, path + "\\Documents\\" + metadatafilename); string filePathMeta = System.IO.Path.Combine(path, "Documents", metadatafilename); AttachmentMetaData eventHubMessage = JsonHelpers.CreateFromJsonFile <AttachmentMetaData>(filePathMeta); await EventHubsHelper.PushMessageToEventHubsAsync(eventHubMessage, EventHubConnectionString); MessageBox.Show("Document uploaded."); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
// Creates an event hub client and sends 100 messages to the event hub. private static async Task SendMessagesToEventHub(AttachmentMetaData msg) { try { var message = JsonConvert.SerializeObject(msg); Console.WriteLine($"Sending message: {message}"); await eventHubClient.SendAsync(new Microsoft.Azure.EventHubs.EventData(Encoding.UTF8.GetBytes(message))); } catch (Exception exception) { Console.WriteLine($"{DateTime.Now} > Exception: {exception.Message}"); } }
public static async Task PushMessageToEventHubsAsync(AttachmentMetaData msg, string eventHubConnectionString) { // Creates an EventHubsConnectionStringBuilder object from the connection string, and sets the EntityPath. // Typically, the connection string should have the entity path in it, but for the sake of this simple scenario // we are using the connection string from the namespace. var connectionStringBuilder = new EventHubsConnectionStringBuilder(eventHubConnectionString) { EntityPath = Constants.EVENT_HUB_NAME }; eventHubClient = Microsoft.Azure.EventHubs.EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString()); await SendMessagesToEventHub(msg); await eventHubClient.CloseAsync(); }