Click here to Skip to main content
15,895,084 members
Articles / Programming Languages / C#

Exposing Windows Service

Rate me:
Please Sign up or sign in to vote.
3.44/5 (7 votes)
8 Nov 20042 min read 44.1K   261   16  
This article is about exposing .NET Windows Service status information.
using System;
using System.Diagnostics; 
using System.Data;
using System.Data.SqlClient;
using System.Xml;
using System.Collections; 
using System.Xml.Serialization;  
using System.Reflection; 
using System.Management.Instrumentation;  
[assembly:Instrumented("root/MyService")]
namespace WindowsService
{
    public enum eStatus
    {
		Created,
		Populated,
		Sent,
		Received,
		Completed,
		NoRecord,
		Error
    }

	/// <devdoc>
	///    <para>
	///       The WindowsService.Context class handles state info and database interaction
	///       as well as handling the XML serialization requests.
	///    </para>
	/// </devdoc>	
	[ InstrumentationClass(InstrumentationType.Instance) ]
	public class ServiceContext
	{
		private eStatus m_Status = eStatus.Created;

		private string TransactionStatus = "P";
				
		private string m_ResponseDetails = String.Empty;
		
		[XmlElement]
		public string TransactionType;

		[XmlElement]
		public string TransactionXML;

		[XmlElement]
		public string ResponseXML
		{
			set
			{
				if (value!=null)
					PopulateResponse((string)value);
			}
			get
			{
				return m_ResponseDetails;
			}
		}

		[XmlElement]
		public string ErrorSource;

		[XmlElement]
		public string ErrorDescription = String.Empty;

		[XmlElement]
		public string ContextID = String.Empty;

		/// <summary>
		/// Constructor which initializes the object
		/// </summary>
		public ServiceContext() 
		{
			ContextID = String.Empty;
			ErrorSource = String.Empty; 
			ErrorDescription = String.Empty;
			TransactionStatus  = String.Empty; 
			TransactionType  = String.Empty;
			TransactionXML  = String.Empty;
			ResponseXML = String.Empty; 
		}

		/// <summary>
		/// Updates the Context objects status on the SQL database
		/// </summary>
		public int Status
		{
			get {	return (int)m_Status;  }
			set	{	UpdateStatus((eStatus)value);	}
		}

		/// <summary>
		/// Does the actual update removed the database interaction code
		/// </summary>
		/// <param name="oStatus"></param>
		private void UpdateStatus(eStatus oStatus)
		{
			m_Status = oStatus;
			switch (oStatus)
			{
				case eStatus.Populated:
				{
					//Database implementation code
					break;
				}
				case eStatus.Error:
				{
					//Database implementation code
					break;
				}
				case eStatus.Completed:
				{
					//Database implementation code
					break;
				}
				case eStatus.Sent:
				{
					//Database implementation code
					break;
				}
				default:
				{
					break;
				}
			}
		}

		/// <summary>
		/// Parses the Response Xml message and checks the error code and description.
		/// </summary>
		/// <param name="XmlString"></param>
		private void PopulateResponse(string XmlString)
		{
			m_ResponseDetails = XmlString;
			if (XmlString.Length>0)
			{
				XmlDocument doc = new XmlDocument();
				doc.LoadXml(XmlString);
				XmlNode root = doc.DocumentElement; 
				if (root!=null)
				{
					if (root.Name=="ERROR")
					{
						ErrorDescription = root.Attributes["Text"].InnerText;  
						Status = (int)eStatus.Error;
					}
					else if (root.Name!="ACK")
					{
						ErrorDescription = "Response does not conform to recognised Xml layout!";
						m_Status = eStatus.Error;
					}
				}
			}
		}
	}// END CLASS DEFINITION Context
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
South Africa South Africa
I studied IT in 1987 and have been working ever since in on projects that range from coding animation in pascal and assembler to writing secure internet banking web sites.

I live in South Africa and I think because of it's size we're required to do more for less which makes highly skilled in a wide range of skills and technologies.

I have the bonus of living in a coastal town so time away from computers is spent around the sea and in sunshine and nature.

Comments and Discussions