Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass Parameters to Azure functions

I have Azure timer function to copy data from my on premises database to Azure managed database. Currently I have hard coded the table names in the function.

Could the parameters be passed as a input to the function instead if hardcoding it ?

public static void Run([TimerTrigger("0 */1 * * * *")]TimerInfo myTimer, TraceWriter log) {
            string srcConnection = @"on premises connecting string";
            string destConnection = @"Azure managed instance connection string";

            string srcTable = "SourceTableName"; //am trying to make this as parameter
            string destTable = "DestinationTableName"; //am trying to make this as parameter
            string tmpTable = "select top 0 * into #DestTable from " + destTable;

            using(SqlConnection
                        srcConn = new SqlConnection(srcConnection),
                        destConn = new SqlConnection(destConnection)
                    ) {
                using(SqlCommand
                        srcGetCmd = new SqlCommand(srcTable, srcConn)
                    ) {
                    srcConn.Open();

                    destConn.Open();

                    SqlCommand cmd = new SqlCommand(tmpTable, destConn);
                    cmd.CommandTimeout = 180;
                    cmd.ExecuteNonQuery();
                    log.Info($"Temp table generated at: {DateTime.Now}");

                    SqlDataReader reader = srcGetCmd.ExecuteReader();
                    log.Info($"Source data loaded at: {DateTime.Now}");

                    using(SqlBulkCopy bulk = new SqlBulkCopy(destConn)) {
                        bulk.DestinationTableName = "#DestTable";
                        bulk.WriteToServer(reader);
                    }

                    string mergeSql = @"<sql logic to insert/Update/delete the data>";

                    cmd.CommandText = mergeSql;
                    cmd.CommandTimeout = 180;
                    cmd.ExecuteNonQuery();
                    log.Info($"Data update from temp table to destination at: {DateTime.Now}");

                    //Execute the command to drop temp table
                    cmd = new SqlCommand("drop table #DestTable", destConn);
                    cmd.CommandTimeout = 180;
                    cmd.ExecuteNonQuery();
                    log.Info($"Drop temp table at: {DateTime.Now}");

                    srcConn.Close();
                    destConn.Close();
                }
            }
            log.Info($"C# Timer trigger function executed at: {DateTime.Now}");

        }

As you can see I have hardcoded the table names, could this be made as paramater ?

like image 773
Pரதீப் Avatar asked Aug 02 '18 12:08

Pரதீப்


People also ask

How do you pass parameters by post to Azure function?

To pass value in the function route in Azure function, we would have to modify the route parameter as “Hello/{valueName}”. Then add a parameter with the same name as the valueName in the Run method to use this value in your azure function. But adding {valueName} makes it a mandatory value to be passed.

What is HTTP trigger in Azure function?

About Http Triggered Function Apps These authorizations will works only after publishing the code in Azure. It can receive request data via query string parameters, request body data or URL route templates.

What are the types of bindings supported by Azure functions?

Binding to a function is a way of declaratively connecting another resource to the function; bindings may be connected as input bindings, output bindings, or both. Data from bindings is provided to the function as parameters. You can mix and match different bindings to suit your needs.


1 Answers

The easiest way is to add Application Settings with these names (e.g. "SourceTableName") and then get them from Environment:

string srcTable = Environment.GetEnvironmentVariable("SourceTableName");

To really make it a parameter you would need to create a custom binding, similar to what I did in Authoring a Custom Binding for Azure Functions. Might be a bit of overkill.

like image 167
Mikhail Shilkov Avatar answered Nov 01 '22 18:11

Mikhail Shilkov