none
Queue output sample RRS feed

  • Question

  • I would like the following code to run in a method in the function:

    // C# example of HTTP trigger binding to a custom POCO, with a queue output binding
    public class CustomQueueMessage
    {
        public string PersonName { get; set; }
        public string Title { get; set; }
    }
    
    public static CustomQueueMessage Run(CustomQueueMessage input, TraceWriter log)
    {
        return input;
    }
    How do I define the method call and method definition? I have defined the queue in the output definition in the Integration session as the default outputQueueItem ... 

    Monday, September 11, 2017 6:23 PM

Answers

  • If you use the ICollector (see previous responses), you can dynamically choose whether you want to create a queue item or not.

    David

    • Marked as answer by DDL19 Monday, September 18, 2017 4:07 PM
    Monday, September 18, 2017 2:34 PM

All replies

  • What does your function.json look like? Also, are you getting a specific error when you compile or run your function? Sharing more details will help investigate.

    thanks,
    David

    Monday, September 11, 2017 11:06 PM
  • The function is a timer function that writes out to an Azure Table. This works fine. What I would also like to do is put data on a Queue to be processed by a different Queue Function.

    My code that works now is:

    #r "Microsoft.WindowsAzure.Storage" #r "Newtonsoft.Json" using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Net; using System.Web; using Microsoft.WindowsAzure.Storage; using Microsoft.WindowsAzure.Storage.Table; using Microsoft.WindowsAzure.Storage.Queue; public static void Run(TimerInfo myTimer, CloudTable Stormtable, TraceWriter log) { string StormType = "T"; WriteStormData(StormType, Stormtable, log); StormType = "S"; WriteStormData(StormType, Stormtable, log); } public static void WriteStormData(string stormType, CloudTable Stormtable, TraceWriter log) { log.Info($"Got to writeStormData Method: {DateTime.Now}");

    ***** would like to make a call to method to write to queue from here. What would the method description

    look like?

     

    I have Azure Queue Storage output defined in the integrate area as: 

    outputQueueItem with a Queue name of outqueue...

    Tuesday, September 12, 2017 3:08 PM
  • Can you show what your function.json looks like?
    Tuesday, September 12, 2017 4:52 PM
  • {
      "bindings": [
        {
          "name": "myTimer",
          "type": "timerTrigger",
          "direction": "in",
          "schedule": "0 */5 * * * *"
        },
        {
          "type": "table",
          "name": "Stormtable",
          "tableName": "StormTable",
          "connection": "AzureWebJobsDashboard",
          "direction": "out"
        },
        {
          "type": "queue",
          "name": "outputQueueItem",
          "queueName": "outqueue",
          "connection": "AzureWebJobsDashboard",
          "direction": "out"
        }
      ],
      "disabled": false
    }
    Tuesday, September 12, 2017 11:52 PM
  • Thanks for the info. Try making your method look like this:

    public static void Run(TimerInfo myTimer, CloudTable Stormtable, out string outputQueueItem, TraceWriter log)

    And then you can create a queue item by just setting the variable, e.g. outputQueueItem="test".

    Or if you want to add multiple items, you can take in an ICollector<string>, and call Add multiple times on it, e.g.

    public static void Run(TimerInfo myTimer, CloudTable Stormtable, ICollector<string> outputQueueItem, TraceWriter log)
    {
        outputQueueItem.Add("First");  
        outputQueueItem.Add("Second");
    

    • Proposed as answer by David Ebbo Thursday, September 14, 2017 1:51 AM
    Thursday, September 14, 2017 1:50 AM
  •  Sorry if I am not being clear. I have my main routine with the run. Then I have 2 methods. In the second method is where I write to the table and would also like to write to the Queue the CustomQueueMessage.

    public class StormEntity : TableEntity 
    {
        public StormEntity(string PKey, string RKey)
        {
            PartitionKey = PKey;
            RowKey = RKey;
        }
    
    
        public StormEntity() { }    
    
        public string Date {get; set; }
        public string StormType {get; set; }
    
    }
    
    public class CustomQueueMessage
    {
        public string Date {get; set; }
        public string StormType {get; set; }
        public string IssueDateTime {get; set; }
        public string ExpiresDateTime {get; set; }
    
    public static void Run(TimerInfo myTimer, CloudTable Stormtable, TraceWriter log, out string outputQueueItem)
    {
        string StormType = "T";
        WriteStormData(StormType, Stormtable, log, out outputQueueItem);
        StormType = "S";
        WriteStormData(StormType, Stormtable, log, out outputQueueItem);
    
      
      }
    
      public static void WriteStormData(string stormType, CloudTable Stormtable, TraceWriter log, out string outputQueueItem)
      {
       string PKey = (string)jsonParse1.SelectToken("resourceSets[0].resources[0].address.adminDistrict");      
           string RKey = (string)jsonParse.SelectToken("features[0].properties.id");
           StormEntity stormrecord = new StormEntity(PKey, RKey);
           stormrecord.Date = eventdate[0];
           stormrecord.StormType = stormType;
    
          TableOperation insertOperation = TableOperation.InsertOrReplace(stormrecord); 
    
           //Stormtable from table parameter name in Integrate section
           Stormtable.Execute(insertOperation);   
    
           // Write to Queue
              outputQueueItem = stormrecord;
    Hope this explains my method calls a bit better... 


    • Edited by DDL19 Thursday, September 14, 2017 8:05 PM typo
    Thursday, September 14, 2017 8:04 PM
  • Once you have the relevant parameters in your Run function, you can just flow them to whatever other method needs to use them. So I don't think having an extra method makes any difference to the essence of the question. Unless I'm misunderstanding?

    Thursday, September 14, 2017 8:36 PM
  • making progress. How do I write the CustomQueue Entity instead of:

    outputQueueItem="test".

    Thanks for your help!

     
    Thursday, September 14, 2017 9:13 PM
  • Rather than having a parameter:

    out string message

    you can use either this (for a single message):

    out CustomQueueMessage message

    or this (if you need to enqueue multiple messages):

    ICollector<CustomQueueMessage> messages

    In the ICollector case, call messages.Add(message) to add messages.


    Mathew Charles [MSFT]




    Thursday, September 14, 2017 10:05 PM
  • I am getting the following errors:
    
    2017-09-14T23:50:00.106 Function compilation error
    2017-09-14T23:50:00.106 run.csx(137,42): error CS0246: The type or namespace name 'CustomQueueMesssage' could not be found (are you missing a using directive or an assembly reference?)
    2017-09-14T23:50:00.106 run.csx(24,22): error CS0177: The out parameter 'outputQueueItem' must be assigned to before control leaves the current method
    
    Line 24
    public static void WriteStormData(string stormType, CloudTable Stormtable, out CustomQueueMessage outputQueueItem, TraceWriter log)
    
    Line 137
    CustomQueueMessage KMLQueue = new CustomQueueMesssage();
           KMLQueue.Date = eventdate[0];
           KMLQueue.StormType = stormType;
           outputQueueItem = KMLQueue;
    
    public class CustomQueueMessage 
    { 
    
        public string Date {get; set; }
        public string StormType {get; set; }
        public string IssueDateTime {get; set; }.......
    ...
    ...
    }

    I am missing the boat here somewhere. I was able to get this to work fine with just a string......
    Thursday, September 14, 2017 11:56 PM
  • Maybe to clear up my question. How do you write a TableEntity to an Output Queue? What is the syntax for this? 
    • Marked as answer by DDL19 Monday, September 18, 2017 2:17 PM
    • Unmarked as answer by David Ebbo Monday, September 18, 2017 2:32 PM
    Friday, September 15, 2017 12:36 PM
  • I found what my issue was. I had two branches in my code. One that wrote to the queue and one that didn't. When I didn't write to the queue is when I got the error message: 

    error CS0177: The out parameter 'outputQueueItem' must be assigned a value......

    by insuring a value is in there this code works. 

    My Question is I don't want to write to the queue unless additional processing is needed. Seems I have to define the queue in the Run method.... Is there a way around this?

    Monday, September 18, 2017 2:02 PM
  • If you use the ICollector (see previous responses), you can dynamically choose whether you want to create a queue item or not.

    David

    • Marked as answer by DDL19 Monday, September 18, 2017 4:07 PM
    Monday, September 18, 2017 2:34 PM