0

I am creating a web service to receive data from another source which will have the following format when the request is made.

The SOAP request for the call has been provided (see below) so I need to accept the data in this format. It is also going into an existing set of web services which can not be updated at this time. They are written in C# using the older style WSDL Web Service.

<?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>
    <GetDepartmentData xmlns="webservice.localhost/">
      <ID>123</ID>
      <Name>John Smith</Name>
      <Departments>
        <DepartmentID>1</DepartmentID>
        <DepartmentID>2</DepartmentID>
        <DepartmentID>4</DepartmentID>
      </Departments>
      <Phone>0123456789</Phone>
    </GetDepartmentData>
  </soap:Body>
</soap:Envelope>

I need to take this request then extract all the data and send into a SQL stored procedure for processing.

Everything is pretty straight forward apart from the 'Departments' section as I am unsure of the correct data type to use and how best to handle this as all the other web service just have single non-complex data types.

The code for the method is as follows:

[WebMethod]
    public string GetDepartmentData(int ID, string Name, ???? Departments, string Phone)
    {
        // Do something with the input here
    }

My plan is to convert the Departments into either a XML data type which I can send into SQL and then split into a table or create a delimited string from the DepartmentID values and then split that in SQL. Not sure yet but that will be the easier part.

The problem I am having is I am unsure of the best way to create the method to handle the SOAP request I am being sent.

Thanks.

Lee
  • 37
  • 7
  • You can use a controller to parse the receive data. A controller can be used either in the client to parse the response of in the server to parse the request. See : https://learn.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/overview/understanding-models-views-and-controllers-cs – jdweng Mar 11 '21 at 03:17
  • Did you add the service reference to your project (Visual Studio)? It will create the classes you need... See: https://stackoverflow.com/questions/49011073/how-to-consume-wsdl-web-service – TheMixy Mar 11 '21 at 04:45
  • @jdweng it is not a MVC project and due to constraints with other services on this project it can not be updated either. – Lee Mar 11 '21 at 10:49
  • @TheMixy The SOAP request is incoming to our web service from another application. I know what you mean about adding the reference as I have done that before when we are calling other services. – Lee Mar 11 '21 at 11:02
  • 1
    You can still parse the string usjng XML Deserialization with the same classes you would use in a controller. You use StringReader reader = new StringReader(xml string). Then XmlReader xReader = XmlReader.Create(reader). Then use normal XmlSerialization methods. – jdweng Mar 11 '21 at 11:22
  • @jdweng This is how I thought it would work however if I set the type of Departments to string and then try and load this as XML all I am seeing when it throws an error is that the value set to Departments is 1, or the first value. This is no tag surrounding this value either. – Lee Mar 11 '21 at 16:03
  • @jdweng Also is I change the Departments type to XmlDocument and the return type to XmlDocument and just return Departments all it shows is 1 so again just the 1st value and is dropping the other two. – Lee Mar 11 '21 at 16:11
  • 1
    I just answered a very similar question here : https://stackoverflow.com/questions/66568148/ipc-wcf-pass-list-of-t/66583056#66583056 – jdweng Mar 11 '21 at 16:21
  • @jdweng Thanks for that, looks like they were having a similar issue as myself. I noticed if I adjust the SOAP call to add in another node under the Depatments, for example DeptList then it returns all the data. The issue seems to be that because Departments is the top level it is dropping that as the content is just a series of DepartmentID but no declared root. – Lee Mar 11 '21 at 16:29
  • You must have an array or list. Otherwise you get only one item. – jdweng Mar 11 '21 at 16:45
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/229781/discussion-between-lee-and-jdweng). – Lee Mar 11 '21 at 17:10

0 Answers0