I have created a web page where users can upload a file which contains data that are inserted into the Lead table in Microsoft Dynamics CRM 2011.
The weird thing now is that when I deploy to our test environment the application runs fine seemingly but absolutely no rows are imported. In my dev environment it works just fine.
After a while when trying to find the error I created a setting that runs the application without using threading (Thread.Run basically) and then it inserts all the Leads. If I switch back to using threads it inserts no leads at all although I get no application errors.
When using SQL Server Profiler I can see in dev (where it works with threading) that all of the insert statements run. When profiling in test environment no insert statements at all are run.
I sort of get the feeling that there is some server issue or setting that is causing this behaviour but when googling I don't really get any results that have helped me.
I was sort of hoping that someone recognizes this problem. I haven't got much experience in using threading so maybe this is some road bump that I need to go over.
I cannot show my code completely but this is basically how I start the threads:
for (int i = 0; i < _numberOfThreads; i++)
{
MultipleRequestObject mpObject = new MultipleRequestObject() { insertType = insertType, listOfEntities = leadsForInsertionOrUpdate[i].ToList<Entity>() };
Thread thread = new Thread(delegate()
{
insertErrors.AddRange(leadBusinessLogic.SaveToCRMMultipleRequest(mpObject));
});
thread.Start();
activeThreads.Add(thread);
}
// Wait for threads to complete
foreach (Thread t in activeThreads)
t.Join();
I initialize my crm connection like this (it reads the connectionstring from web.config -> connectionstrings)
public CrmConnection connection { get; set; }
private IOrganizationService service { get; set; }
public CrmContext crmContext { get; set; }
public CrmGateway()
{
connection = new CrmConnection("Crm");
service = (IOrganizationService)new OrganizationService(connection);
crmContext = new CrmContext(service);
}