[Mono-list] Re: How to implement a Singleton Web Service in Mono

Robert Jordan robertj at gmx.net
Mon Oct 31 06:41:05 EST 2005


Gayan,

> can any body giv me a hint where to start to implement a Singleton Web
> Service in Mono.

Why are you starting another thread?

http://lists.ximian.com/pipermail/mono-list/2005-October/029190.html

Save the (untested) sample as SingletonService.asmx into your
ASP.NET app's folder.

<%@ WebService Language="c#" Class="WebServiceTests.SingletonService" %>

using System;
using System.Web.Services;
using System.Web.Services.Protocols;

namespace WebServiceTests
{
	interface ISingleton
	{
		string Test ();
	}

	// stock singleton implementation
	class Singleton : ISingleton
	{
		static Singleton _instance;
		static object _instanceLock = new object ();

		public static Singleton Instance
		{
			get {
				lock (_instanceLock) {
					if (_instance == null)
						_instance = new Singleton ();
					return _instance;
				}
			}
		}

		public string Test ()
		{
			return "whatever";
		}
	}


	public class SingletonService : System.Web.Services.WebService,
		ISingleton
	{
		[WebMethod]
		public string Test ()
		{
			return Singleton.Instance.Test ();
		}
	}
}



More information about the Mono-list mailing list