I have been asked to create a WCF Web Service endpoint which will take data from an xml file as a parameter passed through to it.
I have tried creating a simple WCF Web Service based on a dummy xml file
The Web Service I created below is in VB.NET and .NET Framework 4.6 and I generated the xml schema in code as OrderedItem at the bottom of the Web Service code, which gets passed in as a parameter to the PushOrderItem OperationContract which is what the customer wants.
<ServiceContract()>
Public Interface IService
<OperationContract()>
Function PushOrderItem(ByVal data As OrderedItem) As String
End Interface
Imports System.Xml.Serialization
Public Class Service
Implements IService
Public Sub New()
End Sub
Function PushOrderItem(ByVal data As OrderedItem) As String Implements IService.PushOrderItem
Return "TEST"
End Function
End Class
<XmlRoot(ElementName:="OrderedItem")>
Public Class OrderedItem
<XmlElement(ElementName:="ItemName", [Namespace]:="http://www.cpandl.com")>
Public Property ItemName As String
<XmlElement(ElementName:="Description", [Namespace]:="http://www.cpandl.com")>
Public Property Description As String
<XmlElement(ElementName:="UnitPrice", [Namespace]:="http://www.cohowinery.com")>
Public Property UnitPrice As String
<XmlElement(ElementName:="Quantity", [Namespace]:="http://www.cpandl.com")>
Public Property Quantity As String
<XmlElement(ElementName:="LineTotal", [Namespace]:="http://www.cohowinery.com")>
Public Property LineTotal As String
<XmlAttribute(AttributeName:="inventory", [Namespace]:="http://www.w3.org/2000/xmlns/")>
Public Property Inventory As String
<XmlAttribute(AttributeName:="money", [Namespace]:="http://www.w3.org/2000/xmlns/")>
Public Property Money As String
End Class
The problem I am having though is when I create a WCF Client to try and consume the new end point, it doesn't appear to be working as I expect.
Below is the code for the Console Application I created which reads the xml file (contents added above) and deserialises it as the ServiceReference1.OrderedItem but the request instance of ServiceReference1.OrderedItem just contains all nulls?
ServiceReference1.ServiceClient client = new ServiceReference1.ServiceClient();
var reader = new System.Xml.Serialization.XmlSerializer(typeof(ServiceReference1.OrderedItem));
var file = new System.IO.StreamReader(@"C:\OrderItem.xml");
ServiceReference1.OrderedItem request = (ServiceReference1.OrderedItem)reader.Deserialize(file);
var response = client.PushOrderItem(request);
Can anyone please let me know what I could be doing wrong here?
CodePudding user response:
You can refer to the following example, which successfully deserializes xml:
Message m = Message.CreateMessage(XmlReader.Create("C:\OrderItem.xml"), int.MaxValue, MessageVersion.Soap11);
SoapReflectionImporter importer = new SoapReflectionImporter(new SoapAttributeOverrides(), "xxx");
XmlTypeMapping mapp = importer.ImportTypeMapping(typeof(Class name));
XmlSerializer xmlSerializer = new XmlSerializer(mapp);
var o = (Class name)xmlSerializer.Deserialize(m.GetReaderAtBodyContents());
CodePudding user response:
Instead of creating my own client, I am using SoapUI Open Source https://www.soapui.org/ which is working fine to test my WCF Service.