私はデータベースへのWebサービスに接続するウェブサイトを持って実行されない場合があり一貫性のないエラー:コレクションが変更された列挙操作が
私は私がこれらのコードを持って
{"Message":"Collection was modified; enumeration operation may not execute.","StackTrace":" at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)\r\n at System.Collections.Generic.List
1.Enumerator.MoveNextRare()\r\n at System.Collections.Generic.List
1.Enumerator.MoveNext()\r\n at System.Web.Script.Serialization.JavaScriptSerializer.SerializeEnumerable(IEnumerable enumerable, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, Str
を言い、この一貫性のないエラーが発生していますc#Webサービスでは、この関数は0行以上を返します。
public class Liked
{
public bool liked;
}
static List<Liked> _get_liked = new List<Liked> { };
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[WebMethod]
public List<Liked> get_liked(string userID,string postID)
{
DataTable table = null;
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Like_Retrieve";
cmd.Parameters.AddWithValue("@UserId", userID);
cmd.Parameters.AddWithValue("@PostId", postID);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
table = this.dbcon.ExecuteDataTable(cmd);
_get_liked.Clear();
Liked _list = new Liked();
if (table.Rows.Count > 0)
{
_list.liked = true;
}
else
{
_list.liked = false;
}
_get_liked.Add(_list);
return _get_liked;
}
エラーの原因には一貫性がありません。私はJavaScriptの中にこれらを持っています
function checkLike(userId, postId) {
$.ajax({
type: "POST",
url: "../Main.asmx/get_liked",
data: "{'userID':'" + userId + "', 'postID':'" + postId + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var result = response.d;
$.each(result, function (index,data) {
var liked = data.liked;
if (liked == true) {
$("#" + postId).prev().parent("button").css("background", "#ccc");
}
else
{
$("#" + postId).prev().parent("button").css("background", "white");
}
});
},
error: function (xhr, ajaxOptions, thrownError) {
alert('error');
console.log(postId);
console.log(userId);
console.log(xhr.responseText);
console.log(thrownError);
}
});
}
誰かが私を助けてくれることを願っています。私は何時間も頭を叩いていました
静的変数_get_likedがシリアル化されている間に、同時呼び出しと関連しているようですが、別の呼び出しがそれを操作しようとしています。 – mehran