在C#中从AWS Lambda调用SOAP服务。

huangapple go评论82阅读模式
英文:

Call soap from aws lambda in C#

问题

我对无服务器 Lambda 函数还不太熟悉,但是我可以帮你翻译一下你的问题。

你想知道如何从 Lambda 函数中调用一个使用 C# 编写的简单 SOAP API。在 Visual Studio 中,你可以添加引用,但是在 Lambda 中应该怎么做呢?我无法找到相关选项。

我尝试过添加引用,但没有成功。

英文:

I’m new on serverless lambda functions and I have deployed a simple soap api by using C# and I want to know how to call it from a lambda function.
In VS you add the reference but what about lambda? I can’t figure out the option

I’ve tried to add the reference with no luck

答案1

得分: 1

没有预先构建的方法可以使用Lambda函数与SOAP服务进行交互。您需要通过创建XML输入,通过HTTP请求发送它,然后解析响应来手动处理它。

以下是使用Python的示例,但您可以在Lambda支持的任何编程语言中实现此功能:

import json
import requests
from datetime import datetime, timedelta
from xml.dom import minidom

import os

url = os.environ['SERVICE_URL']

query_service_template = """
    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
        <soap:Body>
            <SimpleQuery xmlns="http://example.ws.localhost/">
                <m_field>%s</m_field>
            </SimpleQuery>
        </soap:Body>
    </soap:Envelope>
    """

def build_body(field):
  body = query_service_template % (field)
  body = body.replace("\n", "")
  return body.encode('utf-8')

def build_headers(request):
  return {
    "Content-Type": "text/xml; charset=utf-8",
    "Content-Length": str(len(request))
  }    

def extract_data_from_xml(response_body):
  xmldoc = minidom.parseString(response_body)
  outputField = xmldoc.getElementsByTagName('outputField').item(0).firstChild
  
  return {
    'outputField': outputField,
  }
  
def lambda_handler(event, context):
  # Build request
  request = build_body(event['input_field'])
  
  # Build session
  session = requests.session()
  session.headers = build_headers(request)
  
  # Post request
  response = session.post(url=url, data=request, verify=True)
  
  # Extract data from response
  
  data = extract_data_from_xml(str(response.content, "UTF-8"))
  
  return data  
英文:

There isn't a pre-scaffolded method to interact with a SOAP service using a Lambda function.
You'd need to handle it "manually" by creating the XML input, sending it through an HTTP request, and then parsing the response.

Here's a sample using Python, but you can achieve this in any programming language that Lambda supports:

import json
import requests
from datetime import datetime, timedelta
from xml.dom import minidom

import os

url = os.environ[&#39;SERVICE_URL&#39;]

query_service_template = &quot;&quot;&quot;
    &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
    &lt;soap:Envelope xmlns:soap=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;&gt;
    \t&lt;soap:Body&gt;
    \t\t&lt;SimpleQuery xmlns=&quot;http://example.ws.localhost/&quot;&gt;
    \t\t\t&lt;m_field&gt;%s&lt;/m_field&gt;
    \t\t&lt;/SimpleQuery&gt;
    \t&lt;/soap:Body&gt;
    &lt;/soap:Envelope&gt;
    &quot;&quot;&quot;

def build_body(field):
  body = query_service_template % (field)
  body = body.replace(&quot;\n&quot;, &quot;&quot;)
  return body.encode(&#39;utf-8&#39;)

def build_headers(request):
  return {
    &quot;Content-Type&quot;: &quot;text/xml; charset=utf-8&quot;,
    &quot;Content-Length&quot;: str(len(request))
  }    

def extract_data_from_xml(response_body):
  xmldoc = minidom.parseString(response_body)
  outputField = xmldoc.getElementsByTagName(&#39;outputField&#39;).item(0).firstChild
  
  return {
    &#39;outputField&#39;: outputField,
  }
  
def lambda_handler(event, context):
  # Build request
  request = build_body(event[&#39;input_field&#39;])
  
  # Build session
  session = requests.session()
  session.headers = build_headers(request)
  
  # Post request
  response = session.post(url=url, data=request, verify=True)
  
  # Extract data from response
  
  data = extract_data_from_xml(str(response.content, &quot;UTF-8&quot;))
  
  return data  

huangapple
  • 本文由 发表于 2023年8月9日 00:25:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76861491.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定