Handle Open Operation Set issue using Disposable Interface

Problem Statement:

In C# code, if an exception or error occurs while adding operations to an Operation Set, it remains in an Open state. Since each user has a limit of 10 open Operation Sets, exceeding this limit causes other Operation Sets to fail. To address this issue, the Disposable Interface can be used to automatically release or abandon an Operation Set in case of any exception or error. In this blog post, we will explore how to handle this problem.

What is IDisposable Interface:

In C#, the IDisposable interface defines a single method, Dispose(), that releases unmanaged resources held by an object. When an object is no longer needed, the Dispose() method is called to release resources, helping to improve application efficiency. The using statement is commonly used with IDisposable to ensure that Dispose() is called automatically when the object is no longer needed, even if an exception is thrown.

Steps:

There are 2 ways, first is using Try Catch Finally blocks and other using IDisposable interface. From which we will see IDisposable Interface approach:

Abandon Operation Set method:

public OperationSetResponse CallAbandonOperationSetAction(Guid operationSetId)
      {
         OrganizationRequest operationSetRequest = new OrganizationRequest("msdyn_AbandonOperationSetV1");
         operationSetRequest["OperationSetId"] = operationSetId.ToString();
         return GetOperationSetResponseFromOrgResponse(service.Execute(operationSetRequest));
      }

This code is used to abandon an operation set by calling the relevant Dynamics Scheduling API action.

Class for IDisposable Interface:

using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using static FunctionApp.Logic.Helpers.ScheduleApiHelper;

namespace FunctionApp.Logic.Helpers
{
   public class DisposeOperationSet : IDisposable
   {
      public string operationSet;
      private IOrganizationService service;

      public DisposeOperationSet(string operationSetParameter, IOrganizationService serviceParameter)
      {
         operationSet = operationSetParameter;
         service = serviceParameter;
      }
      public void Dispose()
      {
         var scheduleApiHelper = new ScheduleApiHelper(service);
         var record = service.Retrieve(OperationSet.LogicalName, Guid.Parse(operationSet), new ColumnSet(OperationSet.Status));
         var status = record.GetAttributeValue<OptionSetValue>(OperationSet.Status).Value;
         if (status == OperationSet.OS_Status.Open)
         {
            scheduleApiHelper.CallAbandonOperationSetAction(new Guid(operationSet));
         }
      }
   }
}

This C# code defines a class called “DisposeOperationSet” that implements the IDisposable interface. It takes in a parameter for the operation set and an IOrganizationService object. The Dispose() method checks the status of the given operation set, and if it is in the “Open” state, it calls the “CallAbandonOperationSetAction” method of a ScheduleApiHelper object to abandon the operation set. This class can be used to automatically release or abandon an operation set in case of any errors or exceptions.

Main logic:

var scheduleApiHelper = new ScheduleApiHelper(service);
var operationDescription = $"{project.Id}:{DateTime.UtcNow:yyyy-MM-ddTHH:mm:ss}";
var operationSetId = scheduleApiHelper.CallCreateOperationSetAction(project.Id, operationDescription);

using (var disposableOperationSet = new DisposeOperationSet(operationSetId, service))
 {
  //Add Operations Logic here......

  //Execute Operation Set
  scheduleApiHelper.CallExecuteOperationSetAction(operationSetId);
 }

The code creates a new instance of the ScheduleApiHelper class and then, it creates new Operation Set. Inside the using block, the code adds operations to the operation set and then executes it by calling the CallExecuteOperationSetAction method of the ScheduleApiHelper object and passing in the operationSetId. Once the code exits the using block, the Dispose method of the DisposeOperationSet class is called automatically, which releases any unmanaged resources held by the operation set.

Conclusion:

In summary, in C# code, if an error or exception occurs while adding operations to an Operation Set, it remains open, and exceeding the limit of 10 open Operation Sets per user can cause other sets to fail. To solve this issue, we can use the Disposable Interface, which automatically releases or abandons the Operation Set in case of any errors or exceptions. Through this blog post, we have discussed how to implement this solution to handle this problem.

One thought on “Handle Open Operation Set issue using Disposable Interface

Add yours

Leave a Reply

Up ↑

Discover more from Customizers

Subscribe now to keep reading and get access to the full archive.

Continue reading