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.
Create API
Follow the steps in Add an API manually to create a blank API.
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.
Setting |
Value |
Display name |
Test backend |
URL |
GET |
URL |
/api/values |
Add the set-backend-service
policy to the test API.
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:
<set-backend-service backend-id="mysfbackend" sf-resolve-condition="@(context.LastError?.Reason == "BackendConnectionFailure")"/>
Select Save.
Note
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