I want to build a Webservice that on given intervals request data from a SQL-server and update an internal list in the Webservice.
The Webservice will be accessed from multiple clients and I don't to access to SQL-server every time the list is requested from the clients. Is this possible and or recommended!?

My code so far is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Timers;

namespace WebServiceTest
{
	/// <summary>
	/// Summary description for Service1
	/// </summary>
	[WebService(Namespace = "http://10.101.4.156/MyWebServices")]
	[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
	[System.ComponentModel.ToolboxItem(false)]
	// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
	// [System.Web.Script.Services.ScriptService]
	public class Service1 : System.Web.Services.WebService
	{
		static List<string> Test = new List<string>();
		Timer timer = new System.Timers.Timer(5000);
		
		public Service1()
		{
			InitializeComponent();

			timer.Elapsed += timer_Elapsed;
			timer.Start();
		}
		private void InitializeComponent()
		{
	
		}
		static void timer_Elapsed(object sender, ElapsedEventArgs e)
		{
			string time = DateTime.Now.ToString();
			Test.Add("Timer:" + Test.Count + " " + time);
			
			//Database check and update list
		}
		[WebMethod]
		public string HelloWorld()
		{
			return "Hello World";
		}
		[WebMethod]
		public DateTime GetServerTime()
		{
			return DateTime.Now;
		}
		[WebMethod]
		public List<string> GetList()
		{
			return Test;
		}
	}
}

Am not sure what you are outputting, possibly XML, so why not cache it for a specific amount of time (to a file). This way you can return the file's contents or rebuild it first if it has expired.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.