Exception: Serialization of 'Closure' is not allowed in serialize() in Drupal batch

I had this error:

Exception: Serialization of 'Closure' is not allowed in serialize()

during a batch process. Problem seemed to be dependency injection of services in my ModuleBatchService.php

Solution:

The solution was to use the services directly in my processNode method instead of using the services container.

 

Saved you some valuable time?

Buy me a drink 🍺 to keep me motivated to create free content like this!