BPM Async breaks CallContext.Current.TemporarySessionCreator

Hello

I’m facing a problem trying to create a BPM that runs Asynchronous. Everything seems to be working until I try to change Plants using TemporarySessionCreator or Ice.Contracts.SessionModSvcContract

I have this call

new System.Threading.Tasks.Task(() => AsyncStart(ds, Session.CompanyID, connstring)).Start();

this is the Action for that call

Action<Erp.Tablesets.SalesOrderTableset, string, string> AsyncStart = (ds1, CompanyID, stringconn) =>
{
      // I'm creating a new ErpContext since Db is not available at this point
    Erp.ErpContext ctx1 = new Erp.ErpContext(connstring);
    ctx1.Connection.Open();   

// I can do searches or call BO methods
                    var partPlant = (from row in ctx1.PartPlant
                                      where row.Company == rel.Company &&
                                            row.Plant == rel.Plant &&
                                            row.PartNum == rel.PartNum
                                            select row).FirstOrDefault();

//the problem is here*****

 if(currentPlant != partPlant.Plant) 
 {

using (CallContext.Current.TemporarySessionCreator.SetCompanyID(CompanyID).SetPlantID(partPlant.Plant).Create())
                                checkCreateJob = createTransferOrder(CompanyID, rel.OrderNum, rel.OrderLine, rel.OrderRelNum, partPlant.Plant);
}

I also tried this:

using (var ssnSvc = Ice.Assemblies.ServiceRenderer.GetService<Ice.Contracts.SessionModSvcContract>(ctx1, true))
                                {

                                    var sessionId = ssnSvc.Login();

                                    ssnSvc.SetCompany(CompanyID, out companyName, out plantID, out plantName, out workstationID, out workstationDescription, out employeeID, out countryGroupCode, out countryCode, out tenantID, out isProductionInstance);

                                    ssnSvc.SetPlant(partPlant.Plant, out plantName);
                                    writeToLogFile(2, string.Format("session using service plantName " + plantName),  true);
                            //using (CallContext.Current.TemporarySessionCreator.SetCompanyID(CompanyID).SetPlantID(partPlant.Plant).Create())                        
                                createJob(CompanyID, rel.OrderNum, rel.OrderLine, rel.OrderRelNum, partPlant.Plant);

and it throws what I think is the same issue, something is missing

Value cannot be null. (Parameter 'operation')
 Stack 
   at Epicor.Hosting.ServiceInitializer.Initialize(OperationBoundClass service, Operation operation, Boolean root, Boolean isExternalCall) in C:\_releases\ICE\ICE4.2.100.0\Source\Server\Framework\Epicor.System\Hosting\ServiceInitializer.cs:line 14
   at Ice.Assemblies.ServiceRendererImpl.Create[TContract](Boolean ignoreFacade, Boolean root, Object[] args) in C:\_releases\ICE\ICE4.2.100.0\Source\Server\Framework\Epicor.System\Assemblies\ServiceRendererImpl.cs:line 49
   at Epicor.Customization.Bpm.BO.UpdatePostProcessingDirective_JobCreationTransactionTest_87E78177DAA040EFBF5FC68B30005DA9.<>c__DisplayClass6_0.<A001_CustomCodeAction>b__5(SalesOrderTableset ds1, String CompanyID, String stringconn)

or this error

Object reference not set to an instance of an object.
 Stack 
   at Epicor.Hosting.TemporarySessionFluentFactory..ctor(CallContext callContext) in C:\_releases\ICE\ICE4.2.100.4\Source\Server\Framework\Epicor.Ice\Hosting\TemporarySessionFluentFactory.cs:line 20
   at Epicor.Customization.Bpm.BO.UpdatePostProcessingDirective_JobCreationTransactionTest_87E78177DAA040EFBF5FC68B30005DA9.<>c__DisplayClass6_0.<A001_CustomCodeAction>b__5(SalesOrderTableset ds1, String CompanyID, String stringconn)

any ideas?

thanks for your help

You shouldn’t be creating your own Db context.
Also if you want to run a BPM async just flag it as such don’t make your own Task.Run that will cause other issues.

What is the business problem you are trying to solve?

Hi Jose, thanks for your reply, using the flag gave me the same error. at the exact same line

the user will be creating Sales Orders and it will trigger this BPM that will creates jobs and transfer orders in another plant, if the BPM is Sync the CallContext is working correctly so I can change plants. the problem is the user should continue doing orders, since those may to big and the waiting time is a problem for them