Click here to Skip to main content
15,896,063 members
Articles / Web Development / ASP.NET

Monitor your Web Services usage via .NET SOAP Extensions

Rate me:
Please Sign up or sign in to vote.
4.98/5 (48 votes)
6 Apr 2009CPOL27 min read 110.5K   3K   167  
This article demonstrates how you can monitor usage of your Web Services using .NET and SOAP Extensions.
using System;
using System.IO;
using System.Text;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data.SqlClient;
using System.Data;
using WSDM_WebServices;
namespace UserValidation
{
	/// <summary>
	/// Summary description for UserValidationExtension.
	/// </summary>
	/// 
	// configuration parameter

	public class UserValidationExtension : SoapExtension
	{
		private string connectionString;

		public override object GetInitializer(System.Type serviceType)
		{
			return null;
		}

		public override object GetInitializer(LogicalMethodInfo methodInfo,
			SoapExtensionAttribute attribute)
		{
			WSDM_Configuration config = new WSDM_Configuration();
			config.connectionString = System.Configuration.ConfigurationSettings.AppSettings.Get("ConnectionString");
			return config;
		}

		public override void Initialize(object initializer)
		{
			WSDM_Configuration param = (WSDM_Configuration)initializer;
			connectionString = param.connectionString;
			return;
		}

		public override void ProcessMessage(System.Web.Services.Protocols.SoapMessage message)
		{
			switch(message.Stage)
			{
				case SoapMessageStage.BeforeSerialize:
					break;

				case SoapMessageStage.AfterSerialize:
					break;

				case SoapMessageStage.BeforeDeserialize:
					break;

				case SoapMessageStage.AfterDeserialize:
					// Initialize the RegKey
					string regKey = "";
					try
					{
						// Loop through the Soap headers in the message
						foreach (SoapHeader aHeader in message.Headers)
						{
							// If it's our header (decalared in DBManager), 
							// get the RegisterationKey and assign it to RegKey
							if (aHeader is ClientMessageHeader)
							{
								ClientMessageHeader newHeader = (ClientMessageHeader)aHeader;
								regKey = (string)newHeader.RegistrationKey;	
							}
						}
						// If RegKey is still blank, throw a SOAP exception
						if (regKey == "" || regKey == null)
							throw new CustomSoapException("No registration key supplied");

						// Call ValidateUser, and throw an exception if not successful
						if (ValidateUser(message.MethodInfo.Name,regKey) == 0)
							return;
						else
							throw new CustomSoapException("Invalid account details supplied");
					}
					catch(CustomSoapException ex) 
					{
						throw ex;
						// not shown here but its a good idea to log the exceptions for any further investigations needed
					}

					catch(SoapException ex) 
					{
						throw new SoapException("Unknown error encountered", SoapException.ServerFaultCode);
						// not shown here but its a good idea to log the exceptions for any further investigations needed
					}
					catch(Exception ex)
					{	
						throw new SoapException("Unknown error encountered", SoapException.ServerFaultCode);
						// not shown here but its a good idea to log the exceptions for any further investigations needed

					}

					
			}
		}

		private int ValidateUser(string serviceName, string RegKey)
		{
			DBManager db = new DBManager(connectionString);
			int x = db.ValidateUser(serviceName, RegKey);
			return x;
		}

	}
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Canada Canada
Kamran Bilgrami is a seasoned software developer with background in designing mission critical applications for carrier grade telecom networks. More recently he is involved in design & development of real-time biometric based security solutions. His areas of interest include .NET, software security, mathematical modeling and patterns.

He blogs regularly at http://WindowsDebugging.Wordpress.com

Comments and Discussions