简体   繁体   English

Azure APIM 肥皂服务

[英]Azure APIM Soap Service

We use Azure APIM for hosting our SOAP services and integrating the C# code with service endpoints and making calls through HttpClient.We always create elements manually and add values to inner text of element and send it as string content to the service.This is simple for request few elements but when it comes to a huge request we have to spend lot of time in manually creating the soap xml request and is time consuming,error prone?我们使用 Azure APIM 来托管我们的 SOAP 服务并将 C# 代码与服务端点集成并通过 HttpClient 进行调用。我们总是手动创建元素并将值添加到元素的内部文本并将其作为字符串内容发送到服务。这对于请求很少的元素,但是当涉及到一个巨大的请求时,我们必须花费大量时间手动创建soap xml请求并且耗时,容易出错? Is there a better approach to send soap request to APIM and parse the soap reaponse?是否有更好的方法将soap请求发送到APIM并解析soap响应?

您可以使用APIManagementARMTemplateCreator自动执行它,它为您的 API 提取 ARM 模板,以便在 CI/CD 过程中使用。

You can write the inbound policy in APIM > API operation before sending sending the request to backend soap service.在将请求发送到后端soap服务之前,您可以在APIM > API操作中编写入站策略。

Please find the below reference.请找到以下参考。

<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>

You can define your SOAP-request body in an liquid template and parse the XML-response and convert it back to json like in this example:您可以在液体模板中定义您的 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