Source Code

for file /AJAXEngine/S02_AJAXCoreSamples/OrteLookup.asmx

<%@ WebService Language="C#" Class="OrteLookup" %>
// OrteLookup.asmx
// Data provider to a list of city names for the Lookup AJAX Control
// Copyright (c) by Matthias Hertel, http://www.mathertel.de
// This work is licensed under a BSD style license. See http://www.mathertel.de/License.aspx
// ----- 
// 01.06.2005 created by Matthias Hertel
// 01.09.2005 ILookUpService Interface integrated.
// 27.09.2005 ILookUpService Interface extended.
// 29.09.2005 namespaces rearranged.

using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.Caching;
using System.Web.Services;
using System.Web.Services.Protocols;

using AJAXControls;

[WebService(Namespace = "http://www.mathertel.de/OrteLookup/",
  Description = "A WebService retreiving the names of German cities starting with a specific string.")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class OrteLookup : WebService, ILookUpService {

  private const string CACHEENTRYNAME = "AjaxAspectsOrte";
  private const string DataFileName = "Orte.txt";

  private const int MAXSUGGEST = 10;
  
  
  [WebMethod]
  [Obsolete("use GetPrefixedEntries")]
  public string OrteStartWith(string prefix) {
    return (GetPrefixedEntries(prefix));
  } // OrteStartWith


  #region ILookUpService Members

  /// <summary>Return Lookup entries that start with the given prefix.</summary>
  /// <param name="prefix">A prefix string</param>
  /// <returns>Semikolon separated list of entries.</returns>
  [WebMethod(Description="Return Lookup entries that start with the given prefix.")]
  public string GetPrefixedEntries(string prefix) {
    DateTime t1 = DateTime.Now;
    List<string> store = Load();

    int i = store.BinarySearch(prefix, StringComparer.OrdinalIgnoreCase);
    if (i < 0) i = ~i;

    int c = 0;
    while ((c < MAXSUGGEST) && (c + i < store.Count)
      && (store[c + i].StartsWith(prefix, StringComparison.OrdinalIgnoreCase)))
      c++;

    string ret = String.Join(";", store.GetRange(i, c).ToArray());
    return (ret);
  } // GetPrefixedEntries
  
  
  public string GetEntry(string Entry) {
    throw new Exception("The method or operation is not implemented.");
  }

  #endregion


  #region ----- private members -----

  /// <summary>Get the current List of Orte and load if necessary.</summary>
  private static List<string> Load() {
    Cache c = HttpRuntime.Cache;
    List<string> store = (List<string>)c[CACHEENTRYNAME];

    if (store == null) {
      HttpContext ctx = HttpContext.Current;
      string fileName = ctx.Server.MapPath(DataFileName);
      System.Diagnostics.Trace.WriteLine("Load from File", DataFileName);
      store = new List<string>(8000);

      StreamReader st = File.OpenText(fileName);
      while (!st.EndOfStream)
        store.Add(st.ReadLine());
      st.Close();

      store.Sort(StringComparer.OrdinalIgnoreCase);

      c.Insert(CACHEENTRYNAME, store, new CacheDependency(fileName),
        Cache.NoAbsoluteExpiration,
        TimeSpan.FromMinutes(2));
    } // if
    return (store);
  } // Load
  
  #endregion
  

} // class



This page is part of the http://www.mathertel.de/ web site.

For updates and discussions see http://ajaxaspects.blogspot.com/.