Web method intermittently unresponsive to AJAX call
I am using jQuery UI autocomplete on a text box which is making a call back to an ASP.NET .ashx handler.
JavaScript:
$partsSearch.autocomplete({
dela开发者_如何学Pythony: 300,
source: '/Services/PartsCovered.ashx',
minLength: 3,
select: function(event, ui) {
$(event.target).val(ui.item.label);
findPartsByPhrase(ui.item.value);
return false;
},
focus: function(event, ui) {
$(event.target).val(ui.item.label);
return false;
}
});
C#:
[WebService(Namespace = "http://www.blah.co.uk/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class PartsCovered : IHttpHandler
{
/// <summary>
///
/// </summary>
/// <param name="context"></param>
public void ProcessRequest(HttpContext context)
{
context.Response.Clear();
context.Response.ContentType = "application/json";
List<AutoCompleteEntry> autoCompleteList = PartsUtils.PartSuggestionsGet(context.Request.QueryString["term"]);
JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
context.Response.Write(jsSerializer.Serialize(autoCompleteList));
context.Response.End();
}
/// <summary>
///
/// </summary>
public bool IsReusable
{
get
{
return false;
}
}
}
EDIT: Adding additional C# code:
public static List<AutoCompleteEntry> PartSuggestionsGet(string query)
{
List<AutoCompleteEntry> suggestions = new List<AutoCompleteEntry>();
using (SqlConnection sqlConnection = SqlDatabaseUtils.PortalSqlConnection)
{
try
{
SqlCommand sqlCommand = new SqlCommand("Parts_MatchBySearchPhraseGet", sqlConnection)
{
CommandType = CommandType.StoredProcedure
};
sqlCommand.Parameters.AddWithValue("@SearchPhrase", query);
sqlCommand.CommandTimeout = 1;
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
DataTable results = new DataTable();
sqlDataAdapter.Fill(results);
foreach (DataRow row in results.Rows)
{
suggestions.Add(new AutoCompleteEntry(row["PartName"].ToString(), row["PartName"].ToString(), Convert.ToBoolean(row["Variant"])));
}
}
catch (Exception ex)
{
LoggingUtils.AddEntry("Part suggestions failed.", ex, typeof(PartsUtils), LoggingUtils.LogLevel.Warn);
}
}
suggestions.Sort();
return suggestions;
}
And the stored procedure:
ALTER PROCEDURE [dbo].[Parts_MatchBySearchPhraseGet]
@SearchPhrase nvarchar(50)
AS BEGIN SET NOCOUNT ON;
(SELECT PartName,
Included,
0 AS Variant
FROM Parts_Covered
WHERE PartName LIKE '%' + @SearchPhrase + '%')
UNION ALL
(SELECT V.VariantName AS PartName,
P.Included,
1 AS Variant
FROM Parts_Covered AS P
INNER JOIN Parts_Covered_Variants AS V
ON P.PartID = V.PartID
WHERE V.VariantName LIKE '%' + @SearchPhrase + '%')
ORDER BY PartName ASC
END
It works most of the time but then becomes unresponsive. In FireBug you can see the AJAX request is hanging, trying to access any other page on the website also is unresponsive so its got to be the web application. I've also performed profiling on the database and this isn't hanging - the query isn't run until the last moment before the request kicks back into life.
Any suggestions welcome.
Dave
精彩评论