Here is a generic code sample of a service I wrote. It uses SOAP to contact the service and the service returns XML.
I use a stored procedure to handle the data that is passed to the service so that it can be used for multiple purposes.
ISercie.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Data;
namespace MyFirstService
{
[ServiceContract]
public interface IService1
{
[OperationContract]
DataTable MyService(string storedProcedure, string data);
}
}
Service1.svc.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Xml.Serialization;
using System.IO;
namespace MyFirstService
{
public class Service1 : IService1
{
public DataTable MyService(string storedProcedure, string data)
{
string connString = ConfigurationManager.ConnectionStrings["MyServiceConnString"].ConnectionString;
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(storedProcedure, conn);
SqlDataReader reader = null;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@data", data);
try
{
conn.Open();
reader = cmd.ExecuteReader();
DataTable dataTable = new DataTable();
dataTable.TableName = "ReturnData";
dataTable.Load(reader);
StringWriter xmlWriter = new StringWriter();
dataTable.WriteXml(xmlWriter, XmlWriteMode.IgnoreSchema, false);
conn.Close();
return dataTable;
}
catch (Exception)
{
throw;
}
}
}
}
VB Script with the SOAP call
Dim soapServer, soapMessage, data
For x = 1 To Request.Form.Count()
fieldName = Request.Form.Key(x)
fieldValue = Request.Form.Item(x)
If fieldName <> "Submit" And fieldName <> "Password" Then
data = data & fieldName & "~" & Replace(Replace(Replace(fieldValue, "~", ""), "|", ""), "&", "and") & "|"
End If
Next
'service location - Production
soapServer = "http://locationofyourservice/MyFirstService/Service1.svc"
'message including soap envelope wrapper, to send to the service
soapMessage = "<s:Envelope xmlns:s=" & Chr(34) & "http://schemas.xmlsoap.org/soap/envelope/" & Chr(34) & ">" & _
"<s:Body>" & _
"<MyService xmlns=" & Chr(34) & "http://tempuri.org/" & Chr(34) & ">" & _
"<storedProcedure>" & storedProcedure & "</storedProcedure>" & _
"<data>" & data & "</data>" & _
"</MyService>" & _
"</s:Body>" & _
"</s:Envelope>"
Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")
xmlhttp.open "POST", soapServer, False
xmlhttp.setRequestHeader "Man", POST & " " & soapServer & " HTTP/1.1"
xmlhttp.setRequestHeader "SOAPAction", "http://tempuri.org/IService1/MyService"
xmlhttp.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
'Calling the service'
xmlhttp.send(soapMessage)
Response.Write xmlhttp.responseText