Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 连接到.Net核心控制台应用程序中现有的SOAP API服务_C#_Asp.net Core_Wcf_Soapui - Fatal编程技术网

C# 连接到.Net核心控制台应用程序中现有的SOAP API服务

C# 连接到.Net核心控制台应用程序中现有的SOAP API服务,c#,asp.net-core,wcf,soapui,C#,Asp.net Core,Wcf,Soapui,我正在创建一个.Net核心控制台应用程序,在这里我需要连接到SOAP服务来调用POST端点,并通过XML信封传递一些信息。该服务的创建者给了我一个WCF文件,我将它作为一个连接的服务导入到我的项目中,但我被卡住了,不知道下一步要做什么。(我过去使用HttpClient连接WebApi服务并创建了WebApi服务,但从未使用SOAP) 使用soapui,我添加了WCF,更新了URL,并创建了一个新的POST请求,在这里我传递了XML,它连接起来没有任何问题。下面是SOAPUI页面的屏幕截图。 在

我正在创建一个.Net核心控制台应用程序,在这里我需要连接到SOAP服务来调用POST端点,并通过XML信封传递一些信息。该服务的创建者给了我一个WCF文件,我将它作为一个连接的服务导入到我的项目中,但我被卡住了,不知道下一步要做什么。(我过去使用HttpClient连接WebApi服务并创建了WebApi服务,但从未使用SOAP)

使用soapui,我添加了WCF,更新了URL,并创建了一个新的POST请求,在这里我传递了XML,它连接起来没有任何问题。下面是SOAPUI页面的屏幕截图。


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

可能会有所帮助。所以你需要第一个电话:

     //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);
        }
    }
     
//创建请求操作
私有静态HttpWebRequest CreateWebRequest(字符串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”;
返回webRequest;
}
//创建SOAP请求主体
私有静态XmlDocument CreateSoapEnvelope(字符串xxxxx,字符串xxxxx)
{
XmlDocument SOAPEnvelopedDocument=新的XmlDocument();
串肥皂=
@"
“+xxxx+@”
“+xxx+@”
xxxxx
xxxxx
";
LoadXml(soap);
返回SOAPEnveloped文档;
}
//将SOAP正文插入WEB请求
私有静态void InsertSoapEnvelopeIntoWebRequest(XmlDocument SoapEnvelopeExML,HttpWebRequest webRequest)
{
使用(Stream-Stream=webRequest.GetRequestStream())
{
soapEnvelopeXml.Save(流);
}
}

XML信封中的数据只是测试数据。您可以使用扩展名创建客户端代理。这将生成一个代理,其中所有SOAP操作作为方法公开,所有文档、DTO作为类公开。更重要的是,它实现了WS-*互操作规范和协议。我已经创建了代理,并在我的项目中包含了服务。我被困在如何使用我生成的内容上。在创建的各种方法中,我找不到使用SOAP UI调用的端点的任何部分。创建名为
whateverClient
的类并调用其方法。到目前为止,您尝试了什么?如果您使用的是.NET Framework,并且必须使用HttpWebRequest,那么您还可以创建SOAP服务代理。即使您必须手工构建请求,这种连接也是进行SOAP调用的最不安全和内存密集的方式。无法判断字符串是否有效,多个字符串操作最终使用的内存是它们应该使用的内存的3倍。SOAP请求非常庞大,因此此代码将很快耗尽RAM并导致过度的垃圾收集