Azure Functions App (Linux) with DevExpress Reporting - Create and Deploy
- 5 minutes to read
In this topic, you will learn how to create and deploy an Azure Functions project that generates a report based on user input and exports the report to a PDF file. This tutorial uses command-line utilities.
Prerequisites
- An Azure account with an active subscription.
- The latest version of Azure Command-Line Interface (CLI). For instructions, review the following topic: Install Azure CLI on Windows.
- Azure Functions Core Tools
- Your personal DevExpress NuGet credentials. Refer to the following help topic for instructions on how to obtain the feed URL and authorization key, and register the online NuGet feed in your development environment: Choose Between Offline and Online DevExpress NuGet Feeds.
Important Disclaimer
The deployment recommendations do not apply to all possible configurations and should not be considered comprehensive. We offer these instructions as a getting-started reference. Steps may vary depending on your operating system, installed software, and DevExpress versions. You, the developer, are responsible for the application, database, network, and other configurations based on your client, security, environment, and other requirements. We recommend that you review these settings with your database, network, and IT infrastructure administrators and consider tailoring their recommendations to your case.
Create a New Azure Functions Project Using Template
This section uses Azure Functions Core Tools commands to create, manage, and deploy an Azure Functions project. For more information, review the following document: Azure Functions Core Tools reference.
To create a new Azure functions project, you can run the
func init
command. Thefunc init
command prompts you to select a language runtime for the application and customizes the contents of the project folder based on that.This tutorial uses a command to create a folder with the
DXReportingAzureFuncExport
project. The isolated worker model is specified because it is flexible and adaptable to many applications. For more information, review the following document: Guide for running C# Azure Functions in an isolated worker process.Run the following command to create a project:
func init DXReportingAzureTestFunc --worker-runtime dotnet-isolated
Create a new function
TestExportFunction
that uses an HTTP trigger to invoke a function with an HTTP request. It is important to note that functions may require authorization for an HTTP trigger. In this example, the authorization level is set toanonymous
.To run the command, change to the project folder. Enter the following:
cd DXReportingAzureTestFunc func new --template "Http Trigger" --name TestExportFunction --authlevel anonymous
Add NuGet Packages to the Project
To use DevExpress Reporting components, you should install NuGet packages using the following commands:
dotnet add package DevExpress.Reporting.Core
dotnet add package DevExpress.Drawing.Skia
Create a Test Report
The application loads a report from a .REPX file, passes parameters specified in the query string, exports the report to PDF, and displays the resulting PDF in the browser. The report is a conference ticket that contains the owner’s name from the query string, a linear bar code, and a QR code to verify the quality of the print.
The report is created in DevExpress Report Designer and is saved to the TestReport.repx
XML file.
Reference a Report in the Azure Function Project
Copy the TestReport.repx
file to the project root folder. Include a report file to the project as a resource. To do this, open the DXReportingAzureTestFunc.csproj
file using a text editor and add the following tags:
<ItemGroup>
<EmbeddedResource Include="TestReport.repx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>
Implement TestExportFunction
Using any text editor, open the TestExportFunction.cs
file and replace the Run
method with a custom implementation. The modified file should contain the following content:
using System.Net;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
namespace DXReportingAzureTestFunc {
public class TestExportFunction {
private readonly ILogger _logger;
public TestExportFunction(ILoggerFactory loggerFactory) {
_logger = loggerFactory.CreateLogger<TestExportFunction>();
}
[Function("TestExportFunction")]
public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req) {
_logger.LogInformation("C# HTTP trigger function processed a request.");
var firstName = req.Query.GetValues("firstName")?.FirstOrDefault();
var lastName = req.Query.GetValues("lastName")?.FirstOrDefault();
if(string.IsNullOrEmpty(firstName) || string.IsNullOrEmpty(lastName)) {
var response = req.CreateResponse(HttpStatusCode.BadRequest);
response.Headers.Add("Content-Type", "text/plain; charset=utf-8");
await response.WriteStringAsync("Required query parameters are missing: 'firstName', 'lastName'.");
return response;
}
using var ms = new MemoryStream();
var currentAssembly = System.Reflection.Assembly.GetAssembly(typeof(TestExportFunction));
Stream fileStream = currentAssembly.GetManifestResourceStream(typeof(TestExportFunction),"TestReport.repx");
using var report = new DevExpress.XtraReports.UI.XtraReport();
report.LoadLayoutFromXml(fileStream);
report.Parameters["firstName"].Value = firstName;
report.Parameters["lastName"].Value = lastName;
await report.ExportToPdfAsync(ms);
var okResponse = req.CreateResponse(HttpStatusCode.OK);
okResponse.Headers.Add("Content-Type", "application/pdf; charset=utf-8");
await okResponse.WriteBytesAsync(ms.ToArray());
return okResponse;
}
}
}
Tip
You can use Visual Studio Code to edit, debug, run, and publish Azure Functions. Review the following document for more information: Develop Azure Functions by using Visual Studio Code.
Test Azure Functions Locally
Run the following command to build and run the project on a local host:
func start
Publish the Project
Log in to Azure if you are not already logged in:
az login
The authentication process launches a browser window and begins Microsoft authentication.
Open the Microsoft Azure portal. Create a new Function App in any available group. Specify a unique name for the Function App.
Run the following command in the project folder to zip deploy the local
DXReportingAzureTestFunc
project to theDxReportingExportTest
Function App in the Azure cloud:func azure functionapp publish DxReportingExportTest
Test the Published Application
After publishing succeeds, the console shows the function’s invoke URL. In this example, the URL is https://dxreportingexporttest.azurewebsites.net/api/testexportfunction
.
To test the application, enter the following URL into your browser:
https://dxreportingexporttest.azurewebsites.net/api/testexportfunction?firstName=John&lastName=Doe
The function runs and the browser opens resulting PDF content: