Simple Web Service
Have you ever needed to build 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.
This is an example of a SOAP based web service.
KISS (Keep it Simple Stupid)
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#" %>
using System;
using System.Web.Services;
using System.Xml;
using System.Data.SqlClient;
using System.Data;
[WebService(Namespace="http://www.FizenTech.com/")]
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;
}
}
}
Create the Config File
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>
Be aware, you will need to create a virtual directory IIS, marked as an application. We would suggest it have its own application pool.
Also, having different IIS application pools, helps to separate out worker processes. This is good not only for performance, but also for keeping identities separated out.
About Fizen™
Fizen™ is an IT Outsourcing and Technology Partner. Fizen™ specializes in hosting and maintaining financial and accounting platforms, MSSP services such as SIEM Logging and Network Monitoring, and API Integrations. Our 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. We are IT practitioners that strive to deliver practical solutions for our customers.
For more information, please contact us directly.