我有需要接收XML字符串的WCF Soap Web服務。儘管我對webservices的使用經驗不多,但我仍然嘗試過。這個webservice應該是從SAP PI調用的。在Soap XML信封中發送「XML數據作爲字符串」
請求消息的結構是如下所示:
public class DtoRequest<T> : IDtoRequest where T : class
{
public InteractionContext Context { get; set; }
public T Request { get; set; }
}
其中類型是一個類的以下
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一起使用nd使用簡單的控制檯應用程序對其進行測試:
// 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服務時出現問題。當XML數據在RfqXmlData標記中傳遞時,會出現問題。我試着在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 & 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的另一端。
你可以試試來包裝xml內容 – Anand 2014-11-21 11:32:33
@Anand:是的。 正是我所期待的。 – Carel 2014-11-21 11:46:09