Search code examples
azureazure-storageazure-webjobs

How to push data in document db using webjobs?


I have written following code to read the data from text file and then separate the sentences and send them to azure event hub.

I am able to send the data to event hub but not able to push data in document db.

How to push data in document db using webjobs? I am running webjobs from console application, Do I need add more configuration to run it locally? Program.cs

   static void Main(string[] args)
        {
            JobHostConfiguration config = new JobHostConfiguration();
            config.Tracing.ConsoleLevel = System.Diagnostics.TraceLevel.Error;

            var eventHubConfig = new EventHubConfiguration();
            eventHubConfig.AddReceiver(eventHubName, connectionString);
            config.UseEventHub(eventHubConfig);

            JobHost host = new JobHost(config);
            config.DashboardConnectionString = StorageConnectionString;
            if (config.IsDevelopment)
            {
                config.UseDevelopmentSettings();
            }

            //Send test messages
            Task.Run(() =>
            {
                SendMessagesToEventHub();
            });

            host.RunAndBlock();
        }

function.cs

class Functions
    {
        public static void Run([EventHubTrigger("azurepochub")] EventData message, [Microsoft.Azure.WebJobs.DocumentDB("testcosmosdb01122018", "Items", ConnectionStringSetting = "dbConnctionString")]out dynamic document)
        {
            string data = Encoding.UTF8.GetString(message.GetBytes());

            document = data;
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine($"Message received. Data: '{data}'");
            Console.ResetColor();
        }
    }  

Solution

  • You could get answer from github Azure WebJobs SDK Extensions. If we want to insert document to the Azure documentdb, we need to insert the object into it. In your case, your output is string.

    Do I need add more configuration to run it locally?

    I also do demo for that. The following is the detail steps

    1.Create an .net framework Webjob project.

    2.Add the AzureWebJobsDocumentDBConnectionString in the App.config file.

    <appSettings>
        <!-- Service Bus specific app setings for messaging connections -->
        <add key="Microsoft.ServiceBus.ConnectionString" value="Endpoint=sb://[your namespace].servicebus.windows.net;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=[your secret]" />
        <add key ="AzureWebJobsDocumentDBConnectionString" value="xxxxx"/>
     </appSettings>
    

    enter image description here

    3.Add the following code in the Program.cs file.

    using System;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using Microsoft.Azure.WebJobs;
    using Microsoft.Azure.WebJobs.Extensions.DocumentDB;
    using Microsoft.Azure.WebJobs.ServiceBus;
    using Microsoft.ServiceBus.Messaging;
    
    namespace WebJobTest
    {
        // To learn more about Microsoft Azure WebJobs SDK, please see https://go.microsoft.com/fwlink/?LinkID=320976
        class Program
        {
            // Please set the following connection strings in app.config for this WebJob to run:
            // AzureWebJobsDashboard and AzureWebJobsStorage
            private static string eventHubName = "eventhubNam";
            private static string connectionString = "eventhub connectionstring";
            static void Main()
            {
                JobHostConfiguration config = new JobHostConfiguration();
                config.Tracing.ConsoleLevel = System.Diagnostics.TraceLevel.Error;
    
                var eventHubConfig = new EventHubConfiguration();
                eventHubConfig.AddReceiver(eventHubName, connectionString);
                config.UseDocumentDB(new DocumentDBConfiguration
                {
                    ConnectionString = "DocumentDB ConnectionString"
                });
                config.UseEventHub(eventHubConfig);
                config.DashboardConnectionString = "storage connection string";
                JobHost host = new JobHost(config);
    
                if (config.IsDevelopment)
                {
                    config.UseDevelopmentSettings();
                }
    
                //Send test messages
                Task.Run(() =>
                {
                    SendMessagesToEventHub();
                });
    
                host.RunAndBlock();
            }
            static void SendMessagesToEventHub()
            {
                var eventHubClient = EventHubClient.CreateFromConnectionString(connectionString, eventHubName);
                  try
                    {
                        var message = Guid.NewGuid().ToString();
                        Console.WriteLine("{0} > Sending message: {1}", DateTime.Now, message);
                        eventHubClient.Send(new EventData(Encoding.UTF8.GetBytes(message)));
                    }
                    catch (Exception exception)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("{0} > Exception: {1}", DateTime.Now, exception.Message);
                        Console.ResetColor();
                    }
    
                    Thread.Sleep(200);
    
            }
        }
    }
    

    4.In the function.cs

     public static void Run([EventHubTrigger("eventhub name")] EventData message, [DocumentDB("document Database name", "collection", ConnectionStringSetting = "AzureWebJobsDocumentDBConnectionString")]out Item document)
        {
            string data = Encoding.UTF8.GetString(message.GetBytes());
    
            document = new Item{Text = data};
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine($"Message received. Data: '{data}'");
            Console.ResetColor();
        }
    
        public class Item
        {
            public string Text;
    
        }
    

    5.Check the result from console.

    enter image description here

    1. Finish debug from the console then check from the Azure documentdb

    enter image description here