Search code examples
xmlwcfservicewsdl

Create C# Web Service to consume a return messages, no WSDL provided by the Third Company just xml samples an some description of them


Newbie implementing WS, and according to our infrastructures we have to use C#, so maybe will use WCF maybe,

The thing is that our system must store some info of payments that a third company will send us

  1. They send a request message (is a valid payment?)
  2. We Will validate the info and respond with message given an ok or not ok (will check in if the payment apply)
  3. If ok, they will send a message with the transaction done
  4. We will process the message and store the payment in our DB

But they just give us some xml messages examples and an excel files explaining the messages as xpath format (indicating the meanings, types and if it is mandatory). Once implemented we must notified them (no even a endpoint was given y, so I suppose we have to consider it as a variable). Seems that they have their ws implemented in java.

Reviewing tutorials and books, I think they should give us a WSDL file or is it possible to implement the services with just those xml samples? and if so what should be the process? How to build the proxy?


Solution

    1. One of the better ways to approach this is using the standards of the SOA. You can achieve this by creating a simple WCF service with the necessary Service Contracts.
    2. Create a helper for XML serialization that is going to help you create requests and consume the responses. Use the System.Xml.Serialization.XmlSerializer class.
    3. Create another helper to create a Http POST helper to hit your third party's endpoint. The following code should atleast get you

      private static string PostData(string url, string postData, string contentType, string methodType)
      {
          var uri = new Uri(url);
      
          var request = (HttpWebRequest)WebRequest.Create(uri);
      
          request.Method = methodType;
      
          request.ContentType = contentType;
      
          request.ContentLength = postData.Length;
      
          using (Stream writeStream = request.GetRequestStream())
          {
              var encoding = new UTF8Encoding();
      
              byte[] bytes = encoding.GetBytes(postData);
      
              writeStream.Write(bytes, 0, bytes.Length);
          }
      
          string result;
      
          using (var response = (HttpWebResponse)request.GetResponse())
          {
              using (var responseStream = response.GetResponseStream() ?? new MemoryStream())
              {
                  using (var readStream = new StreamReader(responseStream, Encoding.UTF8))
                  {
                      result = readStream.ReadToEnd();
                  }
              }
          }
      
          return result;
      }