Clear ResourceAssignment and add new assignment

Hi,


I have to clear task’s ResourceAssignment and add new assignments, but I’m facing that problem (see attached image).

How can I do this?

Source:

while(task.Assignments.Count > 0)
task.Assignments.First().Delete();
foreach (PrjSyncInsumoRelac relac in tarefa.InsumoRelacs)
{
Resource resource = PrjAsposeUtils.getResourceByGuid(asposeProject, relac.Insumo.PrimaryKey);

ResourceAssignment assignment = asposeProject.ResourceAssignments.Add(task, resource, RMSConvert.ToDouble(relac.Quantidade));
}

StackTrace:

em System.DateTime.AddTicks(Int64 value)
em Aspose.Tasks.Calendar. (DateTime )
em . (DateTime , Calendar )
em . (DateTime , TimephasedDataType , TimeSpan , Boolean )
em . (DateTime , TimeSpan , TimephasedDataType , Boolean )
em Aspose.Tasks.ResourceAssignment. (DateTime , DateTime , TimephasedDataType )
em Aspose.Tasks.ResourceAssignment.GetTimephasedData(DateTime start, DateTime end, TimephasedDataType timephasedType)
em Aspose.Tasks.ResourceAssignment.get_TimephasedData()
em Aspose.Tasks.ResourceAssignment. (DateTime , DateTime , TimephasedDataType )
em Aspose.Tasks.ResourceAssignment. (DateTime )
em Aspose.Tasks.ResourceAssignment. ()
em . (ResourceAssignment )
em . (ResourceAssignment , Double )
em . (ResourceAssignment , Double )
em . (ResourceAssignment , Double )
em . (ResourceAssignment , Double )
em . (ResourceAssignment , Double )
em Aspose.Tasks.ResourceAssignmentCollection.Add(Task task, Resource resource, Double units)
em RM.Prj.Sincronismo.Aspose.PrjPersistirInsumoRelacSbc1.DoSaveContext(TContext context) na c:\RM\Requisitos\Prj-SincTOPProject\Prj-Projetos\RM.Prj.Sincronismo.Service\Sincronismo\Aspose\Subscriber\PrjPersistirInsumoRelacSbc.cs:linha 51</div><div> em RM.Lib.Subscriber.RMSSubscriber1.SaveContext(TContext context)
em RM.Lib.Subscriber.RMSSubscriberController2.DoSaveSubscriber(List1 list)
Hi Lucas,

Thank you for contacting Aspose support team.

I have tried to re-produce the scenario by using a sample project file containing 3 tasks and 2 resources. All the resource assignments are removed from each task and new resources and resource assignments are added to project. All the steps are performed successfully and no exception is raised. Please give it a try and let us know your feedback. If still problem exists, please send us a console application and a sample template project file. This will help me to re-create the problem and provide solution accordingly.

Following is the sample code used for testing:
String dir = @"D:\";
Project proj = new Project(dir + "Project1.mpp");
ChildTasksCollector collector = new ChildTasksCollector(); // Create a ChildTasksCollector instance
TaskUtils.Apply(proj.RootTask, collector, 1); // Collect all the tasks from RootTask using TaskUtils
int i = 0;
foreach (Task tsk in collector.Tasks)
{
if (i == 0)
{
i++;
continue;
}
while (tsk.Assignments.Count > 0)
tsk.Assignments.First().Delete();
i++;
Resource rsc = proj.Resources.Add("Resource " + (2 + i));
ResourceAssignment assn = proj.ResourceAssignments.Add(tsk, rsc);
}
Hi Muhammad,

The source code attached can simulate the error.
See that I set CalculateMode to Manual, because I don't want Aspose.Tasks calculate any date or cost, just write MS Project file with the data.

Thanks.

Hi Lucas,

Thank you for sharing the sample code.

When Calculation mode is set to Manual, the project recalculations need to be called manually. I have added the Project.Recalculate() before saving to disc and the exception is no more raised. Please try it with the highlighted changes as mentioned in the code sample below and let us know your feedback.

Code:

private static void CreateFile(string fileName, string saveFileName)
{
    Project project = new Project(fileName);
    project.CalculationMode = CalculationMode.Manual;
    Task Task1 = project.RootTask.Children.Add();
    SetTaskData(Task1, 1);
    Task Task2 = Task1.Children.Add();
    SetTaskData(Task2, 2);
    Task Task3 = Task2.Children.Add();
    SetTaskData(Task3, 3);
    Task Task4 = Task2.Children.Add();
    SetTaskData(Task4, 4);
    Resource rsc1 = project.Resources.Add("Resource 1");
    rsc1.Set(Rsc.Type, ResourceType.Material);
    Resource rsc2 = project.Resources.Add("Resource 2");
    rsc2.Set(Rsc.Type, ResourceType.Material);
    project.ResourceAssignments.Add(Task3, rsc1, 3);
    project.ResourceAssignments.Add(Task3, rsc2, 5);
    project.ResourceAssignments.Add(Task4, rsc2, 5);
    project.Recalculate();
    project.Save(saveFileName, Aspose.Tasks.Saving.SaveFileFormat.MPP);
}
private static void UpdateFile(string fileName)
{
    Project project = new Project(fileName);
    ChildTasksCollector collector1 = new ChildTasksCollector(); // Create a ChildTasksCollector instance
    TaskUtils.Apply(project.RootTask, collector1, 1); // Collect all the tasks from RootTask using TaskUtils
    foreach (Task task in collector1.Tasks)
    {
        if (task.Children.Count == 0)
        {
            task.Set(Tsk.Start, task.Get(Tsk.Start).AddDays(1));
            task.Set(Tsk.Finish, task.Get(Tsk.Finish).AddDays(5));
            while (task.Assignments.Count > 0)
                task.Assignments.First().Delete();
            project.ResourceAssignments.Add(task, project.Resources.First(), 5);
        }
    }
    project.Recalculate();
    project.Save(fileName, Aspose.Tasks.Saving.SaveFileFormat.MPP);
}
Hi Muhammad,

I don't know why, but I ran a same source code and raised the same error (attached).
Do you did any other change in source code or just add call to "Recalculate"?

Thanks.

Hi Lucas,

We have investigated the issue further on another system and was able to face the issue. Despite the detailed investigation, we could not get to the reason for the issue not being reproducible on some machines. The issue has been logged as TASKS-33945 in our issue tracking system for further investigation by our product team. We’ll update you here once there is some information available in this regard.

Hi Kashif,

I read in the release notes of the 8.2.0 version that this error had been corrected, but this bug is still happening.

Can you verify that?

Thank you for attention.

Hi Lucas,


I have checked the status of this issue and it is planned to be fixed in Aspose.Tasks for .NET 8.3.0. You will be automatically notified once this issue is fixed and new DLL is available online for download.

We are sorry for any inconvenience caused to you in this regard.

Hi José,


The fix version of Aspose.Tasks for .NET 8.3.0 will be available in the last week of this month. We shall notify you here once the fix version is available for download.

Hi José,

We are in processing of releasing the fix version Aspose.Tasks for .NET 8.3.0. It will be available at maximum by today where you will be notified automatically via this thread.

The issues you have found earlier (filed as ) have been fixed in this Aspose.Words for JasperReports 18.3 update.