1

我为 Soap XML 请求编写了 c# 代码,我已经验证了此代码生成 XML 类。

我的问题是如何使用 c# 代码发送请求和接收响应。

请善待我的简单或基本错误,因为我是 XML 的新手,但非常感谢您的帮助。

SOAP XML 请求:

 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ce="http://www." xmlns:os="http://www.domainname.com/schema/soap/v1" xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
    <soapenv:Header /> 
    <soapenv:Body>
        <ce:message>
            <ce:m_control>
                <os:control_timestamp>2001-12-31T12:00:00</os:control_timestamp> 
                <os:message_id>000000000000000000000000000000000</os:message_id> 
                <os:message_type>Contract Enquiry Request</os:message_type> 
                <os:message_version>ce/v2.2/NameContractRequest</os:message_version> 
                <os:expected_response_type>synchronous</os:expected_response_type> 
                <os:initiator_id>initiator_id</os:initiator_id> 
                <os:initiator_orchestration_id>initiator_orchestration_id</os:initiator_orchestration_id> 
                <os:KeyInfo>
                    <ds:X509Data>
                        <ds:X509IssuerSerial>
                            <ds:X509IssuerName>CN=OSIS Customer CA, O=Origo Secure Internet Services Ltd., CN=OSIS Customer CA</ds:X509IssuerName> 
                            <ds:X509SerialNumber>111111111111111111111111111111111111</ds:X509SerialNumber> 
                        </ds:X509IssuerSerial>
                        <ds:X509SubjectName>C=GB, O=FirmID3400010000023NR11QQ, OU=CPS - www.unipass.co.uk/cps, OU=Warning/Terms of Use - www.unipass.co.uk/tou, OU=EmployeeID10101101010101, OU=TPSP2, OU=BPNR1 1QQ, CN=Testt Orgg/emailAddress=Fname.Lname@aviva.co.uk</ds:X509SubjectName> 
                    </ds:X509Data>
                </os:KeyInfo>
                <os:responder_id>os:responder_id</os:responder_id> 
            </ce:m_control>
            <ce:m_content>
                <ce:b_control>
                    <ce:contract_enquiry_reference>TestRequest</ce:contract_enquiry_reference> 
                </ce:b_control>
                <ce:intermediary>
                    <ce:FirmFSARef id="idvalue14">456123</ce:FirmFSARef> 
                </ce:intermediary>
                <ce:request_scope>
                    <ce:contract_details_required_ind>No</ce:contract_details_required_ind> 
                    <ce:valuation_currency>GBP</ce:valuation_currency> 
                    <ce:fund_code_type_required>SEDOL</ce:fund_code_type_required> 
                    <ce:valuation_request ce:type="Current" /> 
                </ce:request_scope>
                <ce:contract>
                    <ce:contract_reference_number>TL12345678</ce:contract_reference_number> 
                </ce:contract>
            </ce:m_content>
        </ce:message>
    </soapenv:Body>
</soapenv:Envelope>
###############################################c#代码##################

指导 CEGuid = Guid.NewGuid(); 字符串 GuidString = CEGuid.ToString();

        string CEVersion = "";
        string URL = "";
        string ResponderId = "";
        string ContractDetailsRequired = "Yes";

        using (XmlTextWriter xmlRequestWriter = new XmlTextWriter(@"C:/Unipass/PensionRequest.xml", Encoding.UTF8))
        {
            xmlRequestWriter.WriteStartDocument();

            xmlRequestWriter.WriteComment("This file is generated by the program.");

            xmlRequestWriter.WriteStartElement("soapenv:Envelope");

            xmlRequestWriter.WriteAttributeString("xmlns:soapenv", null, "http://schemas.xmlsoap.org/soap/envelope/");
            xmlRequestWriter.WriteAttributeString("xmlns:ce", null, "http://www.WhateveryDomain.com/schema/ce/v2.2/WhateverRequest");
            xmlRequestWriter.WriteAttributeString("xmlns:os", null, "http://www.WhateveryDomain.com/schema/soap/v1");
            xmlRequestWriter.WriteAttributeString("xmlns:ds", null, "http://www.w3.org/2000/09/xmldsig#");

            xmlRequestWriter.WriteStartElement("soapenv:Header");
            xmlRequestWriter.WriteEndElement();

            xmlRequestWriter.WriteStartElement("ce:message");
            xmlRequestWriter.WriteStartElement("ce:m_control");
            xmlRequestWriter.WriteElementString("os:control_timestamp", DateTime.Now.ToString("s"));
            xmlRequestWriter.WriteElementString("os:message_id", GuidString);
            xmlRequestWriter.WriteElementString("os:message_type", "Contract Enquiry Request");
            xmlRequestWriter.WriteElementString("os:message_version", "ce/v2.2/WhateverRequest");
            xmlRequestWriter.WriteElementString("os:expected_response_type", "synchronous");
            xmlRequestWriter.WriteElementString("os:initiator_id", "initiator_id");
            xmlRequestWriter.WriteElementString("os:initiator_orchestration_id", "initiator_orchestration_id");
            xmlRequestWriter.WriteStartElement("os:KeyInfo");
            xmlRequestWriter.WriteStartElement("ds:X509Data");
            xmlRequestWriter.WriteStartElement("ds:X509IssuerSerial");
            xmlRequestWriter.WriteElementString("ds:X509IssuerName", "CN=OSIS Customer CA, O=Origo Secure Internet Services Ltd., CN=OSIS Customer CA");
            xmlRequestWriter.WriteElementString("ds:X509SerialNumber", "111111111111111111111111111111111");
            xmlRequestWriter.WriteEndElement();
            xmlRequestWriter.WriteElementString("ds:X509SubjectName", "C=GB, O=FirmID3400010000023NR11QQ, OU=CPS - www.unipass.co.uk/cps, OU=Warning/Terms of Use - www.unipass.co.uk/tou, OU=EmployeeID01200012000003, OU=TPSP2, OU=BPNR1 1QQ, CN=Testt Orgg/emailAddress=fname.lastname@aviva.co.uk");
            xmlRequestWriter.WriteEndElement();
            xmlRequestWriter.WriteEndElement();
            xmlRequestWriter.WriteElementString("os:responder_id", "os:responder_id");
            xmlRequestWriter.WriteEndElement();



            xmlRequestWriter.WriteStartElement("ce:m_content");
            xmlRequestWriter.WriteStartElement("ce:b_control");
            xmlRequestWriter.WriteElementString("ce:contract_enquiry_reference", "TestRequest");
            xmlRequestWriter.WriteEndElement();
            xmlRequestWriter.WriteStartElement("ce:intermediary");
            xmlRequestWriter.WriteStartElement("ce:FirmFSARef");
            xmlRequestWriter.WriteElementString("id", "456123");//="idvalue14">
            xmlRequestWriter.WriteEndElement();
            xmlRequestWriter.WriteEndElement();
            xmlRequestWriter.WriteStartElement("ce:request_scope");
            xmlRequestWriter.WriteElementString("ce:contract_details_required_ind", "Yes");
            xmlRequestWriter.WriteElementString("ce:valuation_currency", "GBP");
            xmlRequestWriter.WriteElementString("ce:fund_code_type_required", "SEDOL");
            xmlRequestWriter.WriteStartElement("ce:valuation_request");
            xmlRequestWriter.WriteElementString("ce:type", "Current");//"ce:type","Current"
            xmlRequestWriter.WriteEndElement();
            xmlRequestWriter.WriteEndElement();
            xmlRequestWriter.WriteStartElement("ce:contract");
            xmlRequestWriter.WriteElementString("ce:contract_reference_number", "PP12345678");
            xmlRequestWriter.WriteEndElement();
            xmlRequestWriter.WriteEndElement();
            xmlRequestWriter.WriteEndElement(); 

            xmlRequestWriter.WriteEndElement();
            xmlRequestWriter.WriteEndDocument();
            xmlRequestWriter.Flush();

请您告诉我如何使用上述代码从 WCF 服务发送请求和接收响应。

即使我添加了 wsdl 引用,但我不知道如何在下面的行中传递 XmlElement[]。

serviceReference.getDetail(ref XmlElement[] Any);

这是参考类中的一个方法。

   [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    [System.ServiceModel.ServiceContractAttribute(Namespace="http://www.domainname.com/whatever/webname/schema/ce/v2.2/NameContractServic" +
        "e", ConfigurationName="TestingPension.NameContractServiceDetailType")]
    public interface NameContractServiceDetailType {

        // CODEGEN: Generating message contract since the wrapper namespace (http://www.domainname.com/schema/ce/v2.2/NameContractRequest) of message getDetailRequest does not match the default value (http://www.domainname.com/name/name/schema/ce/v2.2/NameContractService)
        [System.ServiceModel.OperationContractAttribute(Action="http://www.origostandards.com/schema/ce/v2.2/CEPensionSingleContract#getDetail", ReplyAction="*")]
        [System.ServiceModel.FaultContractAttribute(typeof(SoapReqResWebApplication.TestingPension.Error[]), Action="http://www.domainname.com/schema/ce/v2.2/NameContract#getDetail", Name="errors", Namespace="http://www.domainname.com/schema/tech/v1.0/SOAPFaultDetail")]
        [System.ServiceModel.XmlSerializerFormatAttribute(SupportFaults=true)]
        SoapReqResWebApplication.TestingPension.getDetailResponse getDetail(SoapReqResWebApplication.TestingPension.getDetailRequest request);
    }

    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
    [System.ServiceModel.MessageContractAttribute(WrapperName="message", WrapperNamespace="http://www.domainname.com/schema/ce/v2.2/NameContractRequest", IsWrapped=true)]
    public partial class getDetailRequest {

        [System.ServiceModel.MessageBodyMemberAttribute(Namespace="", Order=0)]
        [System.Xml.Serialization.XmlAnyElementAttribute()]
        public System.Xml.XmlElement[] Any;

        public getDetailRequest() {
        }

        public getDetailRequest(System.Xml.XmlElement[] Any) {
            this.Any = Any;
        }
    }

@CodeCaster我将您的代码尝试为: var documentToSend = new XmlDocument(); //// TODO: 添加你喜欢
使用的所有元素 (XmlTextWriter xmlRequestWriter = new XmlTextWriter(@"C:/Unipass/Request.xml", Encoding.UTF8)) { xmlRequestWriter.WriteStartDocument(); xmlRequestWriter.WriteStartElement("ce:message"); xmlRequestWriter.WriteEndElement(); xmlRequestWriter.WriteEndDocument(); xmlRequestWriter.Flush(); documentToSend.Save(xmlRequestWriter); //// 创建一个以根消息节点为唯一元素的数组。var xmlToSend = new XmlElement[] { documentToSend.DocumentElement }; //xmlToSend = null
可能是我做错了,请你告诉我为什么 xmlToSend 为空。

4

2 回答 2

1

您似乎正在手动编写代码以将消息序列化为 SOAP/XML。SOAP 请求示例是一个实际的实例文档而不是 WSDL,是吗?这是 WCF 为您做的事情之一 - 您不需要生成自己的 SOAP 消息。

在 WCF 中,您专注于要传输的对象类型和要支持的操作。然后 WCF 为您完成所有繁重的 SOAP 序列化工作。这确实是 WCF 的重点。

很难从您的示例 SOAP 消息中解读您到底想要做什么,所以我建议您查看一个简单的 WCF 示例,例如从这里

http://msdn.microsoft.com/en-us/library/vstudio/ms751514(v=vs.90).aspx

使用 wsHttpBinding 或 basicHttpBinding(这些是 WCF 中的 SOAP 绑定)启动并运行一个简单的。然后使用 Fiddler 或类似工具捕获 HTTP 请求,并查看 WCF 在 SOAP 信封生成方面为您做了什么。

然后,您应该能够将其转化为您自己的案例。

于 2013-09-25T11:27:39.477 回答
0

鉴于生成的方法签名是getDetailRequest(System.Xml.XmlElement[] Any). SoapUI之类的工具对 WSDL 有什么看法?

解决这个问题的一种方法是确实手动生成消息,这只是<ce:message>..</ce:message>块。当您调用服务方法时,WCF 会将其包装在适当的 SOAP 信封中。

你必须在XmlDocument课堂上玩一点,但我认为这样的事情会做到这一点:

// Create an XmlDocument and fill it
var documentToSend = new XmlDocument();

// TODO: add all elements you like

// Create an array with the root message node as only element.
var xmlToSend = new XmlElement[] { documentToSend.DocumentElement };

// Call the service.
var response = serviceReference.getDetail(xmlToSend);

另一种方法是在 C# 中手动重新创建请求类,并在发送请求时将其实例填充并序列化为 XML。最终的方法是修复 XML,以便添加服务引用 (SvcUtil) 可以从服务的元数据生成类。

至于您的编辑,您不是将 XML 元素documentToSend写入 ,而是将它们写入文件。

于 2013-09-25T10:44:02.583 回答