xtrareports-404823-cloud-integration-create-and-publish-azure-functions-app.md
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.
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.
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. The func init command prompts you to select a language runtime for the application and customizes the contents of the project folder based on that.
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 to anonymous.
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
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.
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>
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.
Run the following command to build and run the project on a local host:
func start
Log in to Azure if you are not already logged in:
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 the DxReportingExportTest Function App in the Azure cloud:
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: