0

I am making a Timer trigger Azure Function. I am using a variable here which indicates TimeToCopy. This variable has to be updated after every iteration of the function.

I have:

[FunctionName("Function1")]
    public static void Run([TimerTrigger("0 */2 * * * *")]TimerInfo myTimer, TraceWriter log, myTimerItem elapsedTime)

I want to:

[FunctionName("Function1")]
        public static void Run([TimerTrigger("0 */2 * * * *")]TimerInfo myTimer, TraceWriter log, myTimerItem elapsedTime, double TimeToCopy)
das
  • 201
  • 1
  • 3
  • 10

1 Answers1

2

Functions can't reliably keep the state between the calls in memory.

If you can accept the possibility of data loss, you could maintain the value in a static variable.

To save/restore the state reliably you need to add additional binding to your function, e.g. to utilize Table Storage. Something like:

[FunctionName("Function1")]
public static void Run([TimerTrigger("0 */2 * * * *")] TimerInfo myTimer, 
                       myTimerItem elapsedTime,
                       [Table("MyFuncState", "default", "Function1")>] StateEntity entity)
{
    // ...
    entity.TimeToCopy = entity.TimeToCopy * 2.0;
}

public class StateEntity: TableEntity
{
    public double TimeToCopy { get; set; }
}
Mikhail Shilkov
  • 34,128
  • 3
  • 68
  • 107
  • I want the function to **remember it's TimeToCopy value the next time it is triggered**. Will the value `entity.TimeToCopy = entity.TimeToCopy * 2.0;` be retained the next time Function1 is triggered? – das Jun 28 '18 at 07:41
  • Yes, it will be stored back to Table Storage – Mikhail Shilkov Jun 28 '18 at 07:42
  • Okay, thank you! Also I was just curious as to **where** is this Table Storage stored? – das Jun 28 '18 at 07:47
  • See [Table Storage](https://azure.microsoft.com/en-us/services/storage/tables/) and [Table Storage binding](https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-table). My code expects table `MyFuncState` to be available at default storage connection string. – Mikhail Shilkov Jun 28 '18 at 07:50
  • Sorry I am new to Azure Functions. I am writing my azure function locally in Visual Studio 2017 which I will publish to Azure later. I created this table in my Azure Storage and added this code. But it says `System.NullReferenceException: Object reference not set to an instance of an object.` I think it's because I haven't been able to configure the function to storage connection string. How can I do that? – das Jun 28 '18 at 14:52
  • For eg, for a Blob triggered Azure function, I can specify ConnectionString in defination as `[FunctionName("Function2")] public static void Run([BlobTrigger("output/{name}", Connection = "StorageConnection")]Stream myBlob, string name, TraceWriter log)`. – das Jun 28 '18 at 14:52
  • @das You can do the same for `Table` binding, otherwise the default connection will be used (one which `Timer` trigger is using). I guess your exception is because you have no value in that table with that key, so `entity` is `null`. You probably need to put an initial value there (whatever initial `TimeToCopy` should be). – Mikhail Shilkov Jun 28 '18 at 15:13