Set up a Service Fabric backend in API Management using the Azure portal
How-to
This article shows how to configure a Service Fabric service as a custom API backend using the Azure portal. For demonstration purposes, it shows how to set up a basic stateless ASP.NET Core Reliable Service as the Service Fabric backend.
Prerequisites to configure a sample service in a Service Fabric cluster running Windows as a custom backend:
Windows development environment - Install Visual Studio 2019 and the Azure development, ASP.NET and web development, and .NET Core cross-platform development workloads. Then set up a .NET development environment.
These steps create a basic stateless ASP.NET Core Reliable Service using the default Web API project template. Later, you expose the HTTP endpoint for this service through Azure API Management.
Take note of the application name, for example fabric:/myApplication/myService.
API Management instance - An existing or new API Management instance in the Premium or Developer tier and in the same region as the Service Fabric cluster. If you need one, create an API Management instance.
Virtual network - Add your API Management instance to the virtual network you created for your Service Fabric cluster. API Management requires a dedicated subnet in the virtual network.
Add Service Fabric cluster certificate to API Management
The Service Fabric cluster certificate is stored and managed in an Azure key vault associated with the cluster. Add this certificate to your API Management instance as a client certificate.
We recommend adding the certificate to API Management by referencing the key vault certificate.
Add Service Fabric backend
In the Azure portal, navigate to your API Management instance.
Under APIs, select Backends > + Add.
Enter a backend name and an optional description
In Type, select Service Fabric.
In Runtime URL, enter the name of the Service Fabric backend service that API Management will forward requests to. Example: fabric:/myApplication/myService.
In Maximum number of partition resolution retries, enter a number between 0 and 10.
Enter the management endpoint of the Service Fabric cluster. This endpoint is the URL of the cluster on port 19080.
In Client certificate, select the Service Fabric cluster certificate you added to your API Management instance in the previous section.
In Management endpoint authorization method, enter a thumbprint or server X509 name of a certificate used by the Service Fabric cluster management service for TLS communication.
Enable the Validate certificate chain and Validate certificate name settings.
In Authorization credentials, provide credentials, if necessary, to reach the configured backend service in Service Fabric. For the sample app used in this scenario, authorization credentials aren't needed.
Select Create.
Use the backend
To use a custom backend, reference it using the set-backend-service policy. This policy transforms the default backend service base URL of an incoming API request to a specified backend, in this case the Service Fabric backend.
The set-backend-service policy can be useful with an existing API to transform an incoming request to a different backend than the one specified in the API settings. For demonstration purposes in this article, create a test API and set the policy to direct API requests to the Service Fabric backend.
In the API settings, leave the Web service URL blank.
Add an API URL suffix, such as fabric.
Add GET operation to the API
As shown in Deploy a Service Fabric back-end service, the sample ASP.NET Core service deployed on the Service Fabric cluster supports a single HTTP GET operation on the URL path /api/values.
The default response on that path is a JSON array of two strings:
["value1", "value2"]
To test the integration of API Management with the cluster, add the corresponding GET operation to the API on the path /api/values:
Select the API you created in the previous step.
Select + Add Operation.
In the Frontend window, enter the following values, and select Save.
On the Design tab, in the Inbound processing section, select the code editor (</>) icon.
Position the cursor inside the <inbound> element
Add the set-service-backend policy statement.
In backend-id, substitute the name of your Service Fabric backend.
The sf-resolve-condition is a condition for re-resolving a service location and resending a request. The number of retries was set when configuring the backend. For example:
If one or more nodes in the Service Fabric cluster goes down or is removed, API Management does not get an automatic notification and continues to send traffic to these nodes. To handle these cases, configure a resolve condition similar to: sf-resolve-condition="@((int)context.Response.StatusCode != 200 || context.LastError?.Reason == "BackendConnectionFailure" || context.LastError?.Reason == "Timeout")"
Test backend API
On the Test tab, select the GET operation you created in a previous section.
Select Send.
When properly configured, the HTTP response shows an HTTP success code and displays the JSON returned from the backend Service Fabric service.