private void ThreadWork(object state) { try { try { using (new SessionScope(MetaManagerServices.GetSessionFactory(), MetaManagerServices.GetDomainInterceptor(), true, FlushMode.Never, true)) { ((DataAccess.DomainInterceptor)MetaManagerServices.GetDomainInterceptor()).GetDataFromConfigurationManagement = true; generateStartTime = DateTime.Now; var selectedServices = (from id in (List <string>) state select modelService.GetDomainObject <Service>(Guid.Parse(id))).ToList(); // Get which business entities we need to generate depending on the selected services. Dictionary <Guid, BusinessEntity> entityDictionary = new Dictionary <Guid, BusinessEntity>(); //Dictionary<Guid, IDomainObject> loadedObjects = new Dictionary<Guid, IDomainObject>(); List <Service> loadedServices = new List <Service>(); foreach (Service service in selectedServices) { //Service loadedService = (Service)loadedObjects[service.Id]; Service loadedService = modelService.GetDomainObject <Service>(service.Id); loadedServices.Add(loadedService); foreach (ServiceMethod method in loadedService.ServiceMethods) { if (method != null) { if (!entityDictionary.ContainsKey(method.MappedToAction.BusinessEntity.Id)) { entityDictionary.Add(method.MappedToAction.BusinessEntity.Id, method.MappedToAction.BusinessEntity); } } } } ApplicationTemplate template = new ApplicationTemplate(); template.entities = entityDictionary.Values.ToList <BusinessEntity>(); template.services = loadedServices; template.solutionFileName = Path.Combine(solutionPathTbx.Text, solutionNameTbx.Text + ".sln"); template.referenceDirectory = referencePathTbx.Text; template.templateCallback = CodeSmithTemplateCallback; template.Render(TextWriter.Null); UpdateStatusAndTime("Done!", 0, 0, 0); } string info = string.Format("Code generation complete.\r\n{0} files generated, {1} files written to disk", FileCacheManager.GetWrites(), FileCacheManager.GetWritesToDisk()); if (this.InvokeRequired) { this.Invoke(new System.Action(() => { ShowGenerationResult.Show(this, "Generation Result", info, "Files written to disk", FileCacheManager.GetWrittenFilesToDisk()); }) ); } else { ShowGenerationResult.Show(this, "Generation Result", info, "Files written to disk", FileCacheManager.GetWrittenFilesToDisk()); } } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error in Codegeneration", MessageBoxButtons.OK, MessageBoxIcon.Error); } } finally { ((DataAccess.DomainInterceptor)MetaManagerServices.GetDomainInterceptor()).GetDataFromConfigurationManagement = false; if (this.InvokeRequired) { this.Invoke(new System.Action(() => { Cursor.Current = Cursors.Default; pProgress.Visible = false; EnableDisableButtons(); }) ); } else { Cursor.Current = Cursors.Default; pProgress.Visible = false; EnableDisableButtons(); } } }