Issue

When 2 powerJobs jobs are queued and they both use Connect-ERP with -OnConnect with the last server response example on the coolOrange wiki the following warning and error is logged in the logfile

2019-10-03 14:37:31,292 [Pipeline Execution Thread] WARN  ���.��� - ���.Connect() | Failed, exception = {NullReferenceException}.
System.NullReferenceException: Object reference not set to an instance of an object.
   at ���.���.Execute[��](Func`1 �?�)
   at ���.���.���(??� ���)
   at ���.���.GetMetadata(??� ���)
   at ���.���.���()


2019-10-03 14:37:31,308 [Pipeline Execution Thread] ERROR powerGate.Erp.Cmdlets.Cmdlets.ErpCmdletContext - DisconnectErpContext failed with exception: Sequence contains no matching element
System.InvalidOperationException: Sequence contains no matching element
   at System.Linq.Enumerable.First[TSource](IEnumerable`1 source, Func`2 predicate)
   at ���.���.�?�.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at ���.?��.���(Uri ?�)
   at ���.?��.Dispose()
   at powerGate.Erp.Cmdlets.Cmdlets.System.DisconnectErpContext.<Execute>b__0(IErpService ?�)
   at System.Collections.Generic.List`1.ForEach(Action`1 action)
   at powerGate.Erp.Cmdlets.Cmdlets.System.DisconnectErpContext.Execute()
   at powerGate.Erp.Cmdlets.Cmdlets.NoResultCmdletContext.Run()
   at powerGate.Erp.Cmdlets.Cmdlets.ErpCmdletContext.Run(ExecutionResult result)


Solution

There are different solutions to this issue to prevent this issue from happening.


Solution 1

Use a Disconnect-ERP at the end of your job.


Solution 2

Put the $connect scriptblock into a module and make the variable global to be able to use that in both jobs:

$global:connect = {
    param($settings)
    $settings.AfterResponse = [System.Delegate]::Combine([Action[System.Net.Http.HttpResponseMessage]] {
        param($response)
    }, $settings.AfterResponse)
}

Using the variable in Connect-ERP

Connect-ERP -Service "http://hostname:8080/PGS/CATALOGSERVICE" -OnConnect $global:connect


See Also

Connect-ERP (coolOrange wiki)