title: InvalidOperationException while WCF Data Service on load test
body: <p>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.</p>
  
  <p>Topic is a single table, having FKs of Topic Category and User tables.</p>
  
  <p>I have to make this to be used by at-least 10,000 users. Is, WCF DataService a bad choice for this?</p>
  
  <p>Exception:</p>
  
  <pre><code>Test method AgentCollaborationTest.TestDataLayer.TestCreateTopic threw exception: 
  System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
  </code></pre>
  
  <p>Stack Trace:</p>
  
  <pre><code>System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
  System.Collections.Generic.Dictionary`2.ValueCollection.Enumerator.MoveNext()
  System.Linq.Enumerable.&lt;UnionIterator&gt;d__88`1.MoveNext()
  System.Linq.Enumerable.&lt;UnionIterator&gt;d__88`1.MoveNext()
  System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
  System.Linq.Buffer`1..ctor(IEnumerable`1 source)
  System.Linq.OrderedEnumerable`1.&lt;GetEnumerator&gt;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\&lt;username&gt;\Documents\Visual Studio 2012\Projects\DataLayer\DataAccessLayer\DataStream.cs: line 239
  AgentCollaborationTest.TestDataLayer.TestCreateTopic() in c:\Users\&lt;userName&gt;Documents\Visual Studio 2012\Projects\DataLayer\AgentCollaborationTest\UnitTest1.cs: line 67
  </code></pre>
  
  <p>Method:</p>
  
  <pre><code>    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;            
      }
  </code></pre>