0

So globally on my API service, I always wish to set the backend service URL based on certain calling regions.

According to this MSDN library article, using the set-backend-service policy sounds perfect for this, and it's a global policy according to its policy scope at the bottom.

However, even posting their exact example...

<policies>
    <inbound>
        <choose>
            <when condition="@(context.Request.Url.Query.GetValueOrDefault("version") == "2013-05")">
                <set-backend-service base-url="http://contoso.com/api/8.2/" />
            </when>
            <when condition="@(context.Request.Url.Query.GetValueOrDefault("version") == "2014-03")">
                <set-backend-service base-url="http://contoso.com/api/9.1/" />
            </when>
        </choose>
    </inbound>

....results in an error:

"Error in element 'set-backend-service' on line 0, column 0: Policy is not allowed in the specified scope"

I can't figure out how to make it any simpler of a situation to troubleshoot. I even removed the conditional statement and just left the policy alone and it still shows the scope error.

I know this global scope works okay, since I was able to put in an xml-to-json policy as a temporary test and save successfully.

I would figure someone ran into this issue already, as this must be a common use case for this policy. Otherwise, I think the MSDN article is out of date, unless anyone here can see any issues.

Here's my policy scope for global: enter image description here

Rudy Scoggins
  • 431
  • 3
  • 8

2 Answers2

0

Change the quotes to single quotes where "version" is. I just ran the following no problem:

<policies>
    <inbound>
        <choose>
            <when condition="@(context.Request.Url.Query.GetValueOrDefault('version') == '2013-05')">
                <set-backend-service base-url="http://contoso.com/api/8.2/" />
            </when>
            <when condition="@(context.Request.Url.Query.GetValueOrDefault('version') == '2014-03')">
                <set-backend-service base-url="http://contoso.com/api/9.1/" />
            </when>
        </choose>
        <base />
    </inbound>
    <outbound>
        <base />
    </outbound>
</policies>
justin peterson
  • 367
  • 1
  • 6
  • 17
  • Thanks for the reply Justin, but no luck. I'm getting a new error with this version: "Too many characters in character literal". Changing your single quotes back into double quotes removes that error and goes back to my original error. Also, I know this wouldn't work on a global policy. wouldn't be allowed on a global policy, as there isn't anything more base than global :) Are you trying it there? – Rudy Scoggins Sep 28 '16 at 14:28
  • @RudyScoggins that is the behavior i see as well. without copying code if you see in global policy scope if we place the cursor within and the set backend service policy statement is grayed out contrary to what is listed in the document. i hope some one from Azure APIM team takes a note of the same. – Gopi Kolla Nov 15 '16 at 06:39
0

There is something wrong in your expression. The method GetValueOrDefault requires a second parameter as default value.

This is working for me:

<when condition="@(context.Request.Url.Query.GetValueOrDefault("version", "") == "1")">
                ...
            </when>
Sven
  • 2,345
  • 2
  • 21
  • 43