Simple Web Services

Building SOAP Web Services

Building Quick and Simple Web Services

Have you ever needed to stand up a quick and simple web service?  Building quick and simple web services is easy when you have working examples.  We work with SOAP and REST based web services on a regular basis.  They have become the essential go-to solution for system to system communication.

There are times when a full blown project is needed to develop out a complicated web service architecture, but not always - sometimes it is best to KISS (Keep it Simple Stupid).

If you're working with a Windows based system, and need a quick reference to standing up a SOAP based web service; the following C# .NET example may help.

Our example will showcase a SOAP web service that runs a parametrized DB Query and returns XML.  We work with a lot of XML and this type of approach comes in handy, especially if we want to simply run some code, query a database, to then return the result(s).

Implementation Steps:

  • Using a text editor, create an ".asmx" file that will be the endpoint a client application will call.  This will become the endpoint URL that will be hosted on your IIS web server.
  • Populate the new text file with the following code snippet.  In my case, this example can be used to extract information from a database, which we then wrap in an XML container and return to the calling client application.
      <%@ WebService language="C#" class="RapportEchoXML" %>  using System; using System.Web.Services; using System.Xml; using System.Data.SqlClient; using System.Data;  [WebService(Namespace="")] public class ExampleEchoXML : WebService {     [WebMethod]      public System.Xml.XmlNode ReturnXML(string inputKey,XmlDocument echoXml)     {         XmlDocument xml = new XmlDocument();         try         {             string connetionString;                         SqlConnection conn;             string sql = "SELECT COLUMN_1 FROM TABLE_1 WHERE INPUT_KEY = @InputKey";              connetionString = @"Data Source=INSTANCE-NAME;Initial Catalog=DB-Name;User ID=Username;Password=ReallyComplexPassword;";             conn = new SqlConnection(connetionString);              SqlCommand cmd = new SqlCommand(sql, conn);             cmd.Parameters.Add("@InputKey", SqlDbType.VarChar);             cmd.Parameters["@InputKey"].Value = inputKey;                          conn.Open();                 xml.LoadXml("<root><rules>" + (string)cmd.ExecuteScalar() + "</rules>" + echoXml.OuterXml.ToString() + "</root>");             conn.Close();             return xml;         }         catch (Exception e)         {             xml.LoadXml("<error>" + e.Message + "</error>");             return xml;         }     } } 
  • You will also need to have a web.config file, in the same virtual directory of your application.  Here is a simplified web.config file that will work nicely for a basic out of the box SOAP web service.
  <?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer>     <handlers accessPolicy="Read, Execute, Script" /> </system.webServer> </configuration> 
  • You will need to create a virtual directory IIS, marked as an application, and we would suggest it have its own application pool.
  • Having different IIS application pools, helps to separate out worker processes - this is good for performance and keeping different identities segregated.

About Fizen Technology®

Fizen Technology® is a B2B IT Outsourcing and Technology Partner. Fizen Technology® specializes in hosting and maintaining financial and accounting platforms, MSSP services such as SIEM Logging and Network Monitoring, and API Integrations. Their resource model is designed to facilitate IT Support for not only the day-to-day operational needs of a business, but to allow for deeper ongoing partnerships. They are IT practitioners that strive to deliver real solutions for their customers.

For more information, visit opens in a new