访问状态代码或使用 <send-request> 提出的请求

Access the Status Code or a request made using <send-request>

我正在使用 <send-request> 策略发出请求,我需要检查请求的状态代码。为此,我正在尝试访问 response-variable-name 的 StatusCode 属性。但是,当使用此代码时 -

context.Variables["LogFailedEvent"].StatusCode

我看到这个错误 -

Error in element 'set-body' on line 83, column 4: 'object' does not contain a definition for 'StatusCode' and no extension method 'StatusCode' accepting a first argument of type 'object' could be found

根据docs-

The response-variable-name attribute is used to give access the returned response. The name defined in this property can be used as a key into the context.Variables dictionary to access the IResponse object.

要么我误解了文档中的内容,要么文档有误。有人能提出解决方案吗?


政策中的相关部分

<choose>
    <when condition="@(context.Response.StatusCode != 201 && context.Response.StatusCode != 202)">

        <send-request mode="new" response-variable-name="LogFailedEvent" timeout="20" ignore-error="true">
            <set-url>@(string.Format("{0}{1}?code={2}&statusCode={3}&statusReason={4}&message={5}", "{{BVT_LogEventsFailedFunction_Url}}", "{{BVT_LogEventsFailedFunction_Template}}", "{{BVT_LogEventsFailedFunction_Code}}", context.Response.StatusCode, context.Response.StatusReason, context.Response.Body.As<string>()))</set-url>
            <set-method>POST</set-method>
            <set-header name="Content-Type" exists-action="override">
                <value>application/json</value>
            </set-header>
            <set-body>@(context.Request.Body.As<string>())</set-body>
        </send-request>

        <set-body>@(string.Format("{{\"message\":\"event captured by LogFailedEvents\",\"requestId\":\"{0}\",\"statusCode\":\"{1}\",}}", context.RequestId.ToString(), context.Variables["LogFailedEvent"].StatusCode))</set-body>

    </when>
</choose>

你需要施放它:

((IResponse)context.Variables["LogFailedEvent"]).StatusCode

此外,由于您正在设置 ignore-error="true",您可能需要首先检查变量是否包含此键,因为在这种情况下,如果请求失败则根本不会创建此变量。