簡體   English   中英

Azure APIM 肥皂服務

[英]Azure APIM Soap Service

我們使用 Azure APIM 來托管我們的 SOAP 服務並將 C# 代碼與服務端點集成並通過 HttpClient 進行調用。我們總是手動創建元素並將值添加到元素的內部文本並將其作為字符串內容發送到服務。這對於請求很少的元素,但是當涉及到一個巨大的請求時,我們必須花費大量時間手動創建soap xml請求並且耗時,容易出錯? 是否有更好的方法將soap請求發送到APIM並解析soap響應?

您可以使用APIManagementARMTemplateCreator自動執行它,它為您的 API 提取 ARM 模板,以便在 CI/CD 過程中使用。

在將請求發送到后端soap服務之前,您可以在APIM > API操作中編寫入站策略。

請找到以下參考。

<policies>
<inbound>
    <base />
    <rewrite-uri template="/your_soap_service_url.svc" copy-unmatched-params="false" />
    <set-body template="none">@{
          string message = context.Request.Body.As<String>(preserveContent:true);
            try
            {
                 you can write your logic here, if you want to manipulale your request

            }
            catch{}

           string soapPacketStart = "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:yourNamespace=\"namespace url, if you have namespace in your message">
                <soap:Header xmlns:wsa=\"http://www.w3.org/2005/08/addressing\">
                    <wsa:Action>YourAction</wsa:Action>
                    <wsa:To>https://your_backend_url.svc</wsa:To>
                </soap:Header>
                <soap:Body>";
            string soapPacketEnd ="</soap:Body>
            </soap:Envelope>";
            message = string.Format("{0}{1}{2}",soapPacketStart,message,soapPacketEnd); //your soap message is ready
            return message;
        }</set-body>
        <set-header name="Content-Type" exists-action="override">
            <value>application/soap+xml; Action="YourAction"</value>
        </set-header>
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
                //like wise you can set your response
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

您可以在液體模板中定義您的 SOAP 請求正文並解析 XML 響應並將其轉換回 json,如下例所示:

<policies>
    <inbound>
        <base />
        <set-header name="Content-Type" exists-action="override">
            <value>text/xml;charset=UTF-8</value>
        </set-header>
        <set-method>POST</set-method>
        <set-body template="liquid">
            <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tim="http://services.test.com/Service">
                <soapenv:Header />
                <soapenv:Body>
                    <tim:GetTimesheet>
                        <tim:input>
                            <tim:DateTimeFrom>{{context.Request.MatchedParameters["DateTimeFrom"]}}</tim:DateTimeFrom>
                            <tim:DateTimeTo>{{context.Request.MatchedParameters["DateTimeTo"]}}</tim:DateTimeTo>
                            <tim:ResourceId>{{context.Request.MatchedParameters["ResourceId"]}}</tim:ResourceId>
                        </tim:input>
                    </tim:GetTimesheet>
                </soapenv:Body>
            </soapenv:Envelope>
        </set-body>
        <set-backend-service base-url="https://webservices/service.svc" />
        <rewrite-uri template="/" copy-unmatched-params="false" />
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
        <xsl-transform>
            <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
                <xsl:output method="xml" indent="yes" />
                <xsl:template match="* | @* | node()">
                    <xsl:apply-templates select="@* | node()|*" />
                </xsl:template>
                <xsl:template match="PeriodList">
                    <item>
                        <xsl:for-each select="PeriodType">
                            <timeSheet>
                                <DateTimeFrom>
                                    <xsl:value-of select="*[local-name() = 'DateFrom']" />
                                </DateTimeFrom>
                                <DateTimeTo>
                                    <xsl:value-of select="*[local-name() = 'DateTo']" />
                                </DateTimeTo>
                                <Status>
                                    <xsl:value-of select="*[local-name() = 'Status']" />
                                </Status>
                                <EntryList>
                                    <xsl:for-each select="EntryList/Entry/WorkDayList/WorkDay">
                                        <Entry>
                                            <Day>
                                                <xsl:value-of select="Day" />
                                            </Day>
                                            <HoursWorked>
                                                <xsl:value-of select="HoursWorked" />
                                            </HoursWorked>
                                        </Entry>
                                    </xsl:for-each>
                                </EntryList>
                            </timeSheet>
                        </xsl:for-each>
                    </item>
                </xsl:template>
            </xsl:stylesheet>
        </xsl-transform>
        <set-body template="liquid">
        [
        {%- JSONArrayFor ts in body.item -%}        
        {
            "dateTimeFrom" : "{{ts.timeSheet.DateTimeFrom}}",
            "dateTimeTo" : "{{ts.timeSheet.DateTimeTo}}",
            "periodId" : "{{ts.timeSheet.PeriodId}}",
            "days": [
                    {%- JSONArrayFor wd in ts.timeSheet.EntryList -%}   
                    {
                        "day" : "{{wd.Entry.Day}}",
                        "hoursWorked" : "{{wd.Entry.HoursWorked}}"
                    }
                    {%- endJSONArrayFor -%}
                    ]
        }
        {%- endJSONArrayFor -%}
        ]
        </set-body>
        <set-header name="Content-Type" exists-action="override">
            <value>application/json</value>
        </set-header>
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM