Data Management Global

 View Only

Azure Functions using Db2 .NET Core drivers

By Hosathota Vishwanatha posted Mon August 16, 2021 04:56 AM

The blog is based on inputs from Archana Soni and others.

The blog explains step by step process to be followed to create a Httptrigger based Azure Function using Db2 .NET Core driver.

1. Create a new Project by choosing Azure Functions from template in Visual Studio

2. Specify a name and location for the project
AF Specify name and location

3. In create Azure Function Wizard, choose Httptrigger as the trigger, choose .NET Core 3 (LTS) and Authorization level as Anonymous

4. Once the project is created, set the solution platform as x64 by creating a new platform from AnyCPU

5. Using Package Manager, browse and add IBM.Data.DB2.Core version package from NuGet

6. Open the default function class Function1.cs file and rename the Function name from Function1 to HttpExample

7. Modify the default code to have connection open sample code with Db2 .NET Core driver

8. Add the following lines in the project file, the highlighted ones are the new entries needed.
<!--For publishing-->
<Target Name="CopyFilesAfterBuild" AfterTargets="Publish">
          <Exec Command="(robocopy $(PublishDir)clidriver\ $(PublishDir)bin\clidriver /E) ^&amp; IF %25ERRORLEVEL%25 LSS 8 SET ERRORLEVEL = 0" />
<!--For debugging-->
<Target Name="CopyFilesAfterBuildToPublish" AfterTargets="AfterBuild">
          <Exec Command="(robocopy $(TargetDir)clidriver\ $(TargetDir)bin\clidriver /E) ^&amp; IF %25ERRORLEVEL%25 LSS 8 SET ERRORLEVEL = 0" />

9. Open the default code file Function1.cs and include any application specific logic. Here is a sample code snippet for Httptrigger example
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using IBM.Data.DB2.Core;
namespace FunctionApp1
public static class Function1
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log)
    log.LogInformation("C# HTTP trigger function processed a request."); string name = req.Query["name"]; string requestBody = await new         StreamReader(req.Body).ReadToEndAsync(); dynamic data = JsonConvert.DeserializeObject(requestBody); name = name ?? data?.name; string responseMessage = string.IsNullOrEmpty(name) ? "This HTTP triggered function executed successfully. Welcome " : $"Hello, \{name
. This HTTP triggered function executed successfully.";
responseMessage += " DB open will be tried "; //Replace the values in side <> with appropriate values DB2Connection connection = new DB2Connection("DATABASE = <yourdbname>; server = <servername>:<portnumber>; UID = <userid>; PWD = <password>;"); connection.Open(); responseMessage += " DB opened" + connection.ServerVersion; }
catch (Exception ex)
{ responseMessage += " Db error " + ex.Message; }
return new OkObjectResult(responseMessage);

10. Compile the code and if needed can be debugged in local Function Emulator.

11. Once the application is ready, next step is to publish to the Azure Portal

12. Choose the project created above, right click on it and select Publish

13. In the Publish profile wizard choose Azure and then Azure Functions(Windows) and follow additional steps below, some of them being default options presented

13.1 Azure Function App (Windows)

13.2 Function App existing or new.

13.3 If needed deselect the "Run from package file" as above to do Web Deploy instead of the default zip deployment

13.4 Complete the creation of publish profile.

14. Once the profile is created do few more changes
14.1 change Release configuration from Release Any CPU to Release x64

14.2 Target runtime to Portable to win-x64 and Save the changes

15. Use the publish option to publish to Azure environment

16. Once the publishing is successfully completed, go to Azure Portal and select the just published Azure function

17. Select Configuration and then General Configuration make sure the Platform is 64 bit

18. In some cases if needed, change the Azure Function runtime version from ~3 to ~1

19. In case the connection is with IBM i or zOS database servers, please add an environment variable DB2_COMMON_APP_DATA_PATH to a writable location as shown below for a different Azure Function. Please refer to Frequently Asked Questions for more details.

20. Finally invoke the Azure Function through the web browser and selecting the link provided in the Azure Function and then appending the function name

21. Based on the code used above, it should open a connection to IBM database server and display the server version



Thu October 21, 2021 02:10 AM

please contact IBM technical support for further assistance on this.

Tue October 19, 2021 11:08 AM

We don't have license file. Is there any other workaround for this? It seems that the way its setup with our organization, we don't need license file. Any thoughts?

Tue October 19, 2021 04:31 AM

The license file should be kept inside the clidriver of your application's output folder as explained in this link.

Sat October 16, 2021 01:06 AM

I tried the above steps but its giving error while opening a connection to the database. The error is related to License details. Does the nuget package needs a license to connect to DB2 from Azure functions? Here is the error message - "ERROR [42968] [IBM] SQL1598N An attempt to connect to the database server failed because of a licensing problem. SQLSTATE=42968"