Databases Reference
In-Depth Information
“NM”, “NY”, “NC”, “ND”, “OH”, “OK”, “OR”, “PA”, “RI”, “SC”,
“SD”, “TN”, “TX”, “UT”, “VT”, “VA”, “WA”, “WV”, “WI”, “WY”
}
}
};
[OperationContract]
public string[] GetCompletionList(
string prefixText, int count, string contextKey)
{
var country = contextKey ?? string.Empty;
string[] regions;
if (regionsByCountry.TryGetValue(country, out regions))
{
var ignoreCase = StringComparison.InvariantCultureIgnoreCase;
return regions
.Where(r => r.StartsWith(prefixText, ignoreCase))
.Take(count)
.ToArray();
}
return new string[0];
}
}
}
The RegionCompletionService uses a
Dictionary
object to store a list of regions for each
known country. The
GetCompletionList
method performs lookup in the dictionary to
find an array of regions for the given country and returns only those regions that start
with the prefix entered by the user. Both dictionary lookup and the prefix filtering are
implemented to ignore case.
With WCF, implementing a service is only half the battle; endpoint configuration is
crucial to allow the client code (which in this case is JavaScript) to call the service success-
fully. In the sample project, this is done by specifying the
WebScriptServiceHostFactory
in the
@ServiceHost
directive of the .SVC file, which takes care of configuring the HTTP
endpoint based on the location of the SVC file and accepting JSON-formatted method
calls generated by the
AutoCompleteExtender
.
LISTING 10.14
RegionCompletionService.svc
<%@ ServiceHost Language=”C#” Debug=”true”
Service=”WebApplication.RegionCompletionService”
CodeBehind=”RegionCompletionService.svc.cs”
Factory=”System.ServiceModel.Activation.WebScriptServiceHostFactory”
%>