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

Secure Web Services via Message oriented Middleware

Rate me:
Please Sign up or sign in to vote.
4.96/5 (43 votes)
1 May 200372 min read 336.8K   821   198  
Describes an approach for delivery of Soap Messages serialised using ASP.NET Web Client Services over MSMQ and MQ
using System;
using System.Net;
using System.Web.Services.Protocols;


namespace WSAltRouteTest
{
	/// <summary>WSAltRouteTest.ProxyCommon</summary>
    /// <author>SGregory</author>
    /// <date>16 January 2003</date>
    /// <remarks>
    /// Some shared functions for the Proxy classes.  This means that
    /// several generated proxies e.g. localhost1, localhost2 etc. can all
    /// share these routines.
    /// </remarks>
    internal class ProxyCommon
	{
		public ProxyCommon()
		{
		}


		/// <summary>WSAltRouteTest.ProxyCommon.GetWebRequest</summary>
        /// <author>SGregory</author>
        /// <date>16 January 2003</date>
        /// <remarks>
        /// Called from the overridden GetWebRequest within the generated WS proxy.
        /// This method checks for specific protocols we support and registers it
        /// if needs be.  For Uri's sarting "http://", etc. no registration is required -
        /// the specific WebRequest instance can just be created.
        /// </remarks>
        /// <param name="uri" type="System.Uri">The Uri the call involves</param>
        /// <param name="client" type="System.Web.Services.Protocols.SoapHttpClientProtocol">The caller (generated proxy)</param>
        /// <returns type="System.Net.WebRequest">The cast base class version of the specific protocol handler.</returns>
        /// <preconditions>
        /// Uri scheme (protocol) is either already known (e.g. "HTTP://" and hence does not 
        /// need to be registered, or is a supported custom one - like MQ:// or MSMQ:// in our case.
        /// </preconditions>
        /// <postconditions></postconditions>
        static public WebRequest GetWebRequest(
			Uri uri, 
			SoapHttpClientProtocol client)
		{
			// Check for any custom Uri's we support.
			// We need to register the custom ones
			if (uri.AbsoluteUri.ToLower().IndexOf("msmq:") == 0)
			{
				// Register the "msmq:" prefix here for WebRequest
				MSMQRequestCreator Creator = new MSMQRequestCreator();
				WebRequest.RegisterPrefix("msmq", Creator);
			}
			else
			if (uri.AbsoluteUri.ToLower().IndexOf("mq:") == 0)
			{
				// Register the "mq:" prefix here for WebRequest
				MQRequestCreator Creator = new MQRequestCreator();
				WebRequest.RegisterPrefix("mq", Creator);
			}

			// Create a WebRequest dependent on URI scheme
			// Note - if we get here, we're in one of 3 modes:
			//
			//	1) We are a Uri with well known scheme e.g. http://
			//	2) We are a Uri not so well known but we've been registered above e.g. msmq://
			//	3) We are a Uri even less well known e.g. mailto: and are not supported.
			//     In this 3rd case, the following call to WebRequest.Create() will fail
			//	   with "Uri scheme not supported" or similar.
			//
			// This next call against the WebRequest base class forces the IWebRequestCreate
			// interface to be called for the relevant scheme.
			WebRequest newWebRequest = WebRequest.Create(uri);

			// If we get here, pass the callers wait timeout (in secs)
			newWebRequest.Timeout = client.Timeout;

			// If it's a known custom Uri, set up the caller for use later
			// we need this to allow the MSMQWebRequest / MQWebRequest to get at the
			// serialised Soap message in order to send it to the target.
			if (uri.AbsoluteUri.IndexOf("msmq:") == 0 ||
				uri.AbsoluteUri.IndexOf("mq:") == 0)
				((MyBaseWebRequest)newWebRequest).CallingProxy = (MySoapClientProtocol)client;

			// Return the specific WebRequest instance
			return newWebRequest;
		}


		/// <summary>WSAltRouteTest.ProxyCommon.GetWebResponse</summary>
        /// <author>SGregory</author>
        /// <date>17 January 2003</date>
        /// <remarks>Invokes the WebResponse logic of the specific protocol handler</remarks>
        /// <param name="webReq" type="System.Net.WebRequest">Specific WebRequest handler e.g. MSMQWebRequest</param>
        /// <returns type="System.Net.WebResponse">Specific WebResponse object</returns>
        static public WebResponse GetWebResponse(
			WebRequest webReq)
		{
			try
			{
				// Ask for a response for the request
				return webReq.GetResponse();
			}
			catch(Exception e)
			{
				// This is here simply to allow the debug IDE to observe it
				throw e;
			}
		}
	}
}

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
United Kingdom United Kingdom
I am the Technical Director for Myphones.com, which specialises in developing integrated VoIP products and services for the consumer and SME markets. Technology-wise, we are heavily into the 2nd generation WS stack atop .NET, and basically most things Microsoft, as well as C++ on Linux.

Comments and Discussions