Saturday, July 12, 2025

Exposing Azure Document Intelligence Service through Azure Front Door

In this post, let's see how we can expose an Azure Document Intelligence (DI) Service through Azure Front Door (AFD) and consume it via a  .NET Client Application that uses Azure.AI.DocumentIntelligence package.

Say, we have a DI service,

https://{document-intelligence-service-name}.cognitiveservices.azure.com/

And we need this service to be consumed via,

https://{front-door-name}.azurefd.net/di-api/

For an example, 

POST https://{document-intelligence-service-name}.cognitiveservices.azure.com/documentintelligence/documentClassifiers/{modelId}:analyze
will now be consumed via,
POST https://{front-door-name}/di-api/documentintelligence/documentClassifiers/{modelId}:analyze

To start off, following are already created.

  • An Azure Document Intelligence Service
  • Azure Front Door and CDN profiles (Azure Front Door Standard with Quick Create)
First step is adding Origin Group with Origins in AFD.
Add Origin Group
Added Origin is as follows:
Add Origin
Once these are added, next we need to configure how to route the requests to this Origin group.

We can do it two ways.

1. Using default-route, with a Rule set to Override origin group
2. Creating a new route with Pattens to match and Origin path

Now let's see how we can configure both these ways.

1. Using default-route, with a Rule set to Override origin group

With this approach, first we need to create a Rule set as follows.
Rule set configuration
Now we need to associate this rule set to the default-route.
Update default route

2. Creating a new route with Pattens to match and Origin path

In this approach, we don't need to create a Rule set. Instead, we can create a new Route with Pattens to match and Origin path.
Add new route
Now I have a .NET Client Application that uses Azure.AI.DocumentIntelligence package, that I was using to test the DI functionality via AFD.
using Azure;
using Azure.AI.DocumentIntelligence;

string endpoint = "https://{front-door-name}.azurefd.net/di-api/";
string apiKey = "{document-intelligence-service-api-key}";
DocumentIntelligenceClient documentIntelligenceClient = new (new Uri(endpoint)new AzureKeyCredential(apiKey));

string classifierId = "{some-classification-model-id}";
string testFilePath = "path\to\test\file.pdf";

using FileStream fileStream = new FileStream(testFilePathFileMode.Open, FileAccess.Read);
BinaryData binaryData = BinaryData.FromStream(fileStream);

ClassifyDocumentOptions classifyDocumentOptions = new(classifierIdbinaryData);

Operation<AnalyzeResult> operation = 
    await documentIntelligenceClient.ClassifyDocumentAsync(WaitUntil.Completed, classifyDocumentOptions);

AnalyzeResult result = operation.Value;

foreach (AnalyzedDocument document in result.Documents)
{
    Console.WriteLine($"Found a document of type: '{document.DocumentType}'");
}
And I can see this is working with both the approaches.

Hope this helps.

Happy Coding.

Regards,
Jaliya

No comments:

Post a Comment