在Soap XML信封中发送'XML data as string'

时间:2014-11-21 08:56:31

标签: xml web-services wcf soap

我有WCF Soap webservice需要接收XML字符串。尽管事实上我对网络服务的经验很少,但我仍然对此表示了不满。应该从SAP PI调用此Web服务。

请求消息的结构如下:

public class DtoRequest<T> : IDtoRequest where T : class
{
    public InteractionContext Context { get; set; }
    public T Request { get; set; }
}

其中type是以下类的

public class SendRfqRequest : DtoRequest<ImportRfqDataDto>
{
}

[DataContract]
public class ImportRfqDataDto
{
    [DataMember]
    public string RfqXmlData { get; set; }
}

InteractionContext采用以下字段:

public class InteractionContext
{
    public string Application { get; set; }
    public string License { get; set; }
    public DateTime TimeStamp { get; set; }
    public Guid Id { get; set; }
}

我在webservicesdev服务器上部署了它,生成了与wsdl.exe一起使用的代码,并使用简单的控制台应用程序对其进行了测试:

// Generated from wsdl.exe tool
RfqImportServiceClient client = new RfqImportServiceClient();
// Xml Document
XDocument rfqXml = XDocument.Load("..\\..\\RfqValidGood.xml");

SendRfqRequest request = new SendRfqRequest
{
    Context = new CityOfCapeTown.Services.Common.InteractionContext
    {
        Id = Guid.NewGuid(),
        Application = "TestApplication",
        License = "TestLicenseKey",
        TimeStamp = DateTime.Now,
    },
    Request = new DataTransferObjects.ImportRfqDataDto
    {
        RfqXmlData = rfqXml.ToString()
    }
};

Console.WriteLine("\n\n- Attempting connection to web-service");
SendRfqResponse response = client.ProcessRfq(request);
Console.WriteLine("- Successfully connected to web-service");
Console.WriteLine("- Status of response: " + (ResponseType)response.Result);

这一切都很完美。从SAP PI测试Web服务时出现问题。在RfqXmlData标记中传递XML数据时出现问题。我尝试在SOAP UI中测试它。它从wsdl:

生成以下SOAP信封
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/" xmlns:cit="http://schemas.datacontract.org/2004/07/CityOfCapeTown.Services.Common" xmlns:proc="http://schemas.datacontract.org/2004/07/ProcurementAdministrationPortal.WebService.DataTransferObjects">
   <soapenv:Header/>
   <soapenv:Body>
      <tem:ProcessRfq>
         <!--Optional:-->
         <tem:request>
            <!--Optional:-->
            <cit:Context>
               <!--Optional:-->
               <cit:Application>SAPClient</cit:Application>
               <!--Optional:-->
               <cit:Id>1a975cab-bc7c-47e3-aa77-ac9dc407721d</cit:Id>
               <!--Optional:-->
               <cit:License>1a975cab-bc7c-47e3-aa77-ac9dc407721d</cit:License>
               <!--Optional:-->
               <cit:TimeStamp>2014-01-01</cit:TimeStamp>
            </cit:Context>
            <!--Optional:-->
            <cit:Request>
               <!--Optional:-->
               <proc:RfqXmlData>
                xmldata
               </proc:RfqXmlData>
            </cit:Request>
         </tem:request>
      </tem:ProcessRfq>
   </soapenv:Body>
</soapenv:Envelope>

当在标记中传递一些随机字符串时,这适用于SOAP UI:

<proc:RfqXmlData>
    random string
</proc:RfqXmlData>

但在传递所需的XML数据时出错:

<proc:RfqXmlData>
    <Rfq><IsAbove30k>true</IsAbove30k><ReferenceNumber>ReferenceNumber1</ReferenceNumber><ContactPersonName>ContactPersonName1</ContactPersonName><ContactPersonTelephoneNumber>ContactPersonTe1</ContactPersonTelephoneNumber><ContactPersonCellPhone>ContactPersonCe1</ContactPersonCellPhone><BuyerName>BuyerName1</BuyerName><BuyerTelephoneNumber>BuyerTelephoneN111111111111111111111</BuyerTelephoneNumber><BuyerEmailAddress>BuyerEmailAddress1</BuyerEmailAddress><ProcurementItem><Title>Title111111111111111111111111</Title><ClosingDate>1900-01-01T01:01:01+02:00</ClosingDate><Description>Description11111111111111111</Description><CaptureDate>1900-01-01T01:01:01+02:00</CaptureDate></ProcurementItem><RfqGood><DeliveryTo>DeliveryTo1</DeliveryTo><DeliveryAddress>DeliveryAddress1</DeliveryAddress><DeliverySuburb>DeliverySuburb1</DeliverySuburb><DeliveryPostalCode>1</DeliveryPostalCode><SubmissionFax>SubmissionFax1</SubmissionFax><Specification>Specification1</Specification><SubmissionFax2>SubmissionFax21</SubmissionFax2><DeliveryDate>1900-01-01T01:01:01+02:00</DeliveryDate><Good>Tools &amp; Machinery</Good></RfqGood></Rfq>
</proc:RfqXmlData>

错误:

<faultstring xml:lang="en-ZA">The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:request. The InnerException message was 'There was an error deserializing the object of type ProcurementAdministrationPortal.WebService.Messages.SendRfqRequest. End element 'RfqXmlData' from namespace 'http://schemas.datacontract.org/2004/07/ProcurementAdministrationPortal.WebService.DataTransferObjects' expected. Found element 'Rfq' from namespace ''. Line 22, position 10.'.  Please see InnerException for more details.</faultstring>

看起来它试图反序列化<Rfq>...</Rfq>。在我看来,我必须做的是让服务将XML数据视为字符串而不是XML。不过,我仍然需要将它作为XML。

1 个答案:

答案 0 :(得分:1)

将这段XML包裹在<![CDATA[]]>标记中。

  

CDATA代表Character Data,表示数据   在这些标签之间包括可以解释为XML的数据   标记,但不应该。“