Click here to Skip to main content
15,884,388 members
Articles / Desktop Programming / Windows Forms

Windows Services Made Simple

Rate me:
Please Sign up or sign in to vote.
4.62/5 (10 votes)
27 Jun 2007CPOL10 min read 94.3K   6.9K   69  
Describes how to build a Windows Service using the Pegasus Library.
using System;
using System.Collections;
using System.Text;
using System.IO;
using System.Runtime.Serialization;

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Pegasus.Runtime.Serialization;
using Pegasus.Runtime.Serialization.Formatters.Xml;

namespace Pegasus.UnitTests.Runtime.Serialization.Formatters.Xml.XmlFormatter2Tests
{
	/// <summary>
	/// This is a test of the deserialization of a hashtable with an unknown type in it.
	/// </summary>
	[TestClass]
	public class XmlFormatter2UnknownTypeTest
	{
		// Local Instance Values
		private TestContext m_testContextInstance;

		/// <summary>
		/// Initializes a new instance of the <see cref="T:XmlFormatter2UnknownTypeTest"/> class.
		/// </summary>
		public XmlFormatter2UnknownTypeTest()
		{
		}

		/// <summary>
		/// Gets or sets the test context which provides information about and 
		/// functionality for the current test run.
		/// </summary>
		/// <value>The test context.</value>
		public TestContext TestContext
		{
			get
			{
				return m_testContextInstance;
			}
			set
			{
				m_testContextInstance = value;
			}
		}

		/// <summary>
		/// Test a hashtable with an unknown type.
		/// </summary>
		[TestMethod]
		public void UnknowTypeTestSubObject()
		{
			Hashtable table = (Hashtable) Deserialize( xmlHashtable1 );

			Assert.IsNotNull( table, "Table was not deserialized correctly" );
			Assert.IsTrue( table.Count == 1, "Table should only have one item it it" );
			Assert.IsNotNull( table[ "TheKey" ], "The key does not exist in the table" );
			Assert.IsTrue( table[ "TheKey" ].GetType() == typeof( object ), "Wrong type was returned should be an object" );
		}

		/// <summary>
		/// Unknows the type test sub object.
		/// </summary>
		[TestMethod]
		public void UnknowTypeTestRootObject()
		{
			object table = Deserialize( xmlHashtable2 );

			Assert.IsNotNull( table, "Table was not deserialized correctly" );
			Assert.IsTrue( table.GetType() == typeof( object ), "Wrong type was returned should be an object" );
		}

		/// <summary>
		/// Deserializes the specified XML.
		/// </summary>
		/// <param name="xml">The XML.</param>
		/// <returns></returns>
		private object Deserialize( string xml )
		{
			// Deserialize the buffer back to an object
			byte[] buffer = Encoding.UTF8.GetBytes( xml );
			using( MemoryStream memStream = new MemoryStream( buffer ) )
			{
				XmlFormatter2 xmlFormatter = new XmlFormatter2( null, new StreamingContext( StreamingContextStates.Persistence ) );
				( (ObjectSerializationBinder) xmlFormatter.Binder ).UnknownTypeReplacement = true;
				return xmlFormatter.Deserialize( memStream );
			}
		}

		/// <summary>
		/// These are a serialed hashtable
		/// </summary>
		private const string xmlHashtable1 = @"
			<XmlFormatter Version='2.0'>
			  <ObjectGraph TypeRef='1' ObjectId='1' ISerializable='true'>
				<LoadFactor TypeRef='2' StringValue='0.72'>
				  <Bytes TypeRef='3'>
					<Bytes_element TypeRef='4'>236</Bytes_element>
					<Bytes_element TypeRef='4'>81</Bytes_element>
					<Bytes_element TypeRef='4'>56</Bytes_element>
					<Bytes_element TypeRef='4'>63</Bytes_element>
				  </Bytes>
				</LoadFactor>
				<Version TypeRef='5'>1</Version>
				<Comparer TypeRef='6'>null</Comparer>
				<HashCodeProvider TypeRef='6'>null</HashCodeProvider>
				<HashSize TypeRef='5'>11</HashSize>
				<Keys TypeRef='7'>
				  <Keys_element TypeRef='8'>TheKey</Keys_element>
				</Keys>
				<Values TypeRef='7'>
				  <Values_element TypeRef='9' ObjectId='2' ISerializable='true'>
					<ticks TypeRef='10'>632897592327913064</ticks>
					<dateData TypeRef='11'>9856269629182688872</dateData>
				  </Values_element>
				</Values>
			  </ObjectGraph>
			  <TypeReferences>
				<Type TypeRef='1'>System.Collections.Hashtable, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</Type>
				<Type TypeRef='2'>System.Single, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</Type>
				<Type TypeRef='3'>System.Byte[], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</Type>
				<Type TypeRef='4'>System.Byte, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</Type>
				<Type TypeRef='5'>System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</Type>
				<Type TypeRef='6'>System.Object, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</Type>
				<Type TypeRef='7'>System.Object[], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</Type>
				<Type TypeRef='8'>System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</Type>
				<Type TypeRef='9'>ThisIsTheUnknownTypeForDateTime, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</Type>
				<Type TypeRef='10'>System.Int64, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</Type>
				<Type TypeRef='11'>System.UInt64, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</Type>
			  </TypeReferences>
			</XmlFormatter>
		";

		private const string xmlHashtable2 = @"
			<XmlFormatter Version='2.0'>
			  <ObjectGraph TypeRef='1' ObjectId='1' ISerializable='true'>
				<LoadFactor TypeRef='2' StringValue='0.72'>
				  <Bytes TypeRef='3'>
					<Bytes_element TypeRef='4'>236</Bytes_element>
					<Bytes_element TypeRef='4'>81</Bytes_element>
					<Bytes_element TypeRef='4'>56</Bytes_element>
					<Bytes_element TypeRef='4'>63</Bytes_element>
				  </Bytes>
				</LoadFactor>
				<Version TypeRef='5'>1</Version>
				<Comparer TypeRef='6'>null</Comparer>
				<HashCodeProvider TypeRef='6'>null</HashCodeProvider>
				<HashSize TypeRef='5'>11</HashSize>
				<Keys TypeRef='7'>
				  <Keys_element TypeRef='8'>TheKey</Keys_element>
				</Keys>
				<Values TypeRef='7'>
				  <Values_element TypeRef='9' ObjectId='2' ISerializable='true'>
					<ticks TypeRef='10'>632897592327913064</ticks>
					<dateData TypeRef='11'>9856269629182688872</dateData>
				  </Values_element>
				</Values>
			  </ObjectGraph>
			  <TypeReferences>
				<Type TypeRef='1'>ThisIsTheUnknownTypeForHashtable, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</Type>
				<Type TypeRef='2'>System.Single, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</Type>
				<Type TypeRef='3'>System.Byte[], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</Type>
				<Type TypeRef='4'>System.Byte, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</Type>
				<Type TypeRef='5'>System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</Type>
				<Type TypeRef='6'>System.Object, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</Type>
				<Type TypeRef='7'>System.Object[], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</Type>
				<Type TypeRef='8'>System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</Type>
				<Type TypeRef='9'>System.DateTime, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</Type>
				<Type TypeRef='10'>System.Int64, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</Type>
				<Type TypeRef='11'>System.UInt64, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</Type>
			  </TypeReferences>
			</XmlFormatter>
		";

	}
}

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
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions