【问题标题】:Connect to existing SOAP API service in .Net Core console application连接到 .Net Core 控制台应用程序中的现有 SOAP API 服务
【发布时间】:2020-12-07 11:01:07
【问题描述】:

我正在创建一个 .Net Core 控制台应用程序,我需要在其中连接到 SOAP 服务以调用 POST 端点并通过 XML 信封传递一些信息。该服务的创建者向我提供了作为连接服务导入项目中的 WCF 文件,但我被卡住了,不明白接下来要做什么。 (我过去使用 HttpClient 连接到它们,但从不使用 SOAP 来使用和创建 WebApi 服务。

使用 SOAP UI,我添加了 WCF,更新了 URL 并创建了一个新的 POST 请求,我在其中传递了 XML,它可以毫无问题地连接。下面是 SOAP UI 页面的屏幕截图。

我可以参考什么来在我的控制台应用程序中做同样的事情?

【问题讨论】:

  • XML Envelope 中的数据只是测试数据。
  • 您可以使用WCF Web Service Reference 扩展名创建客户端代理。这会生成一个代理,其中所有 SOAP 操作都作为方法和所有文档、DTO 作为类公开。更重要的是,它实现了 WS-* 互操作规范和协议
  • 我已经创建了代理并将服务包含在我的项目中。我被困在如何使用我生成的东西上。在创建的各种方法中,我找不到使用 SOAP UI 调用的端点的任何部分。
  • 创建名为whateverClient 的类并调用其方法。到目前为止,您尝试过什么?

标签: c# asp.net-core wcf soapui


【解决方案1】:

可能会有所帮助。所以你需要先打电话:

     //CREATE REQUEST ACTION
    private static HttpWebRequest CreateWebRequest(string url)
    {
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
        //webRequest.Headers.Add("SOAPAction", action);
        webRequest.ContentType = "text/xml;charset=\"utf-8\"";
        webRequest.Accept = "text/xml";
        webRequest.Method = "POST";
        return webRequest;
    }



 //CREATE SOAP REQUEST BODY
    private static XmlDocument CreateSoapEnvelope(string xxxxx,string xxxxx)
    {
        XmlDocument soapEnvelopeDocument = new XmlDocument();
        
        string soap =
            @"<?xml version=""1.0"" encoding=""utf-8""?>
            <soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" 
               xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" 
               xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
              <soap:Body>
                <get_DeclarationInfo xmlns=""http://tempuri.org/"">
                 <input>
                    <EgbNo xmlns="""">"+xxxx+@"</EgbNo>
                    <VoenEgb xmlns = """" >"+xxx+@"</VoenEgb>
                    <AgencyCode xmlns="""">xxxxx</AgencyCode>
                    <Pasw xmlns="""">xxxxx</Pasw>
                  </input>
                </get_DeclarationInfo>
              </soap:Body>
            </soap:Envelope>";

        soapEnvelopeDocument.LoadXml(soap);
        return soapEnvelopeDocument;
    }

    //INSERT SOAP BODY TO WEB REQUEST
    private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
    {
        using (Stream stream = webRequest.GetRequestStream())
        {
            soapEnvelopeXml.Save(stream);
        }
    }
     

【讨论】:

  • 如果您使用 .NET Framework 并且必须使用 HttpWebRequest,您还可以创建一个 SOAP 服务代理。即使您必须手动构建请求,这种连接也是进行 SOAP 调用的最不安全和内存密集型的方式。没有办法判断字符串是否有效并且多个字符串操作最终使用了它们应该使用的 3 倍 RAM。 SOAP 请求很庞大,所以这段代码会很快耗尽内存并导致过多的垃圾收集
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-11
相关资源
最近更新 更多