I fixed the problem by creating the context for each request, rather than sharing it among all the requests. My context was a singleton earlier.
InvalidOperationException while WCF Data Service on load test
-
10-07-2023 - |
Вопрос
I have a method to insert a record in a Table, which works fine during my unit test. However, when I do a load test using Visual studio ultimate's load test, with just constant load of 10 users, I get below InvalidOperationException after around 1000 records are successfully inserted. I am not sure which collection is modified and how to get rid of this exception. I cannot catch this during debugging, as it throws exception only with moderate load.
Topic is a single table, having FKs of Topic Category and User tables.
I have to make this to be used by at-least 10,000 users. Is, WCF DataService a bad choice for this?
Exception:
Test method AgentCollaborationTest.TestDataLayer.TestCreateTopic threw exception:
System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
Stack Trace:
System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
System.Collections.Generic.Dictionary`2.ValueCollection.Enumerator.MoveNext()
System.Linq.Enumerable.<UnionIterator>d__88`1.MoveNext()
System.Linq.Enumerable.<UnionIterator>d__88`1.MoveNext()
System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
System.Linq.Buffer`1..ctor(IEnumerable`1 source)
System.Linq.OrderedEnumerable`1.<GetEnumerator>d__0.MoveNext()
System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
System.Data.Services.Client.BaseSaveResult..ctor(DataServiceContext context, String method, DataServiceRequest[] queries, SaveChangesOptions options, AsyncCallback callback, Object state)
System.Data.Services.Client.BaseSaveResult.CreateSaveResult(DataServiceContext context, String method, DataServiceRequest[] queries, SaveChangesOptions options, AsyncCallback callback, Object state)
System.Data.Services.Client.DataServiceContext.SaveChanges(SaveChangesOptions options)
System.Data.Services.Client.DataServiceContext.SaveChanges()
DishTalk.Data.DataStream.CreateTopic(String Name, String UserName, String content, Guid catagoryId, Boolean isLocked) in c:\Users\<username>\Documents\Visual Studio 2012\Projects\DataLayer\DataAccessLayer\DataStream.cs: line 239
AgentCollaborationTest.TestDataLayer.TestCreateTopic() in c:\Users\<userName>Documents\Visual Studio 2012\Projects\DataLayer\AgentCollaborationTest\UnitTest1.cs: line 67
Method:
public Guid? CreateTopic(string name, string userName, string content, Guid catagoryId)
{
context.IgnoreResourceNotFoundException = true;
//Get the category and user objects, which has a Foreign Key association with Topic
Category selectedCat = GetCatagory(catagoryId);
User user = GetUser(userName);
var topic = DataService.Topic.CreateTopic(
Guid.NewGuid(),
name,
DateTime.Now,
GetUser(userName).Id,
false,
catagoryId,
false
);
try
{
topic.Content = content;
topic.Solved = false;
//The context is created in the constructor
context.AddToTopics(topic);
context.AddLink(selectedCat, "Topics", topic);
context.AddLink(user,"Topics",topic);
// Send the insert to the data service.
DataServiceResponse response = context.SaveChanges();
}
catch (DataServiceRequestException ex)
{
logger.ErrorException(ex.GetType().ToString(), ex);
}
return topic.Id;
}
Решение
Не связан с StackOverflow