Reputation: 538
The setup
We have a web app hosted on IIS 7.5 that uses a .net web api rest service to short-poll a SQL Server 2008 database. The service controller calls a stored procedure to read the data.
The problem
When concurrent requests are made to the same web service, we intermittently see the duplicate results, and sometime other processes results, returned to the browser.
C# service code
namespace ImageApp_REST_Services.Repositories {
public class ImageLinkRepository : IImageLinkRepository
{
private List<ImageLink> ImageLinks
{
get;
set;
}
public IEnumerable<ImageLink> Get(String userId)
{
ImageLinks = new List<ImageLink>();
using (var cnnSQL = new SqlConnection(...))
{
// opend connection to DB
cnnSQL.Open();
try
{
SqlCommand cmmSQL = new SqlCommand("nVision_select_lcimagelinks_sp", cnnSQL);
cmmSQL.CommandType = System.Data.CommandType.StoredProcedure;
SqlParameter prmSQL = cmmSQL.Parameters.Add(new SqlParameter
{
ParameterName = "@LCIMGLINKUSERID",
Value = userId
});
SqlDataReader rdrSQL = cmmSQL.ExecuteReader();
if (rdrSQL.HasRows)
{
while (rdrSQL.Read())
{
ImageLinks.Add(new ImageLink
{
// set new ImageLink object's properties
imageTopicId = DBReader.SQLString(rdrSQL, "LCIMGLINKIMGTOPICID"),
id = DBReader.SQLInt(rdrSQL, "LCIMGLINKPK"),
recordId = DBReader.SQLString(rdrSQL, "LCIMGLINKRECORDID"),
text = DBReader.SQLString(rdrSQL, "LCIMGLINKTEXT"),
topicId = DBReader.SQLString(rdrSQL, "LCIMGLINKTOPICID"),
topicItem = DBReader.SQLString(rdrSQL, "LCIMGLINKTOPICITEM"),
url = DBReader.SQLString(rdrSQL, "LCIMGLINKURL"),
user = DBReader.SQLString(rdrSQL, "LCIMGLINKUSERID")
});
}
}
}
catch (Exception)
{
}
}
return ImageLinks;
}
}
}
Again, when multiple requests are hitting the service simultaneously, we occasionally see either duplicate records returned, or records belonging to another service call.
We want the service to only return the data for the given request. Does anyone know what is wrong???
Upvotes: 0
Views: 1993
Reputation: 22568
Is ImageLinks
defined as a static member? If it is, the contents will be shared across multiple threads and web requests. One thread will overwrite the other's results. Long shot, but fits your issue.
Upvotes: 2