laravel-soap icon indicating copy to clipboard operation
laravel-soap copied to clipboard

Service already exists error when in loop

Open craigward opened this issue 6 years ago • 1 comments

I'm trying to loop through some data and make a Soap call on each iteration.

The first one always processes without issue. However, after the first I get the following error

Service 'addNewJob' already exists. which is thrown by /vendor/artisaninweb/laravel-soap/src/Artisaninweb/SoapWrapper/SoapWrapper.php

The file contains the following code

public function add($name, Closure $closure)
  {
    if (!$this->has($name)) {
      $service = new Service();

      $closure($service);

      $this->services[$name] = $service;

      return $this;
    }

Does it cache the name or something? how do I stop this error?

craigward avatar Apr 01 '19 14:04 craigward

You need to unregister the service of the SoapWrapper after the execution because the worker instance is unique and in the second iteration tries to register again the service that is already registered. SoapWrapper::remove('addNewJob');

/** * Remove a service from the wrapper * * @param $name * @return $this */ public function remove($name) { if(!empty($this->services[$name])) { unset($this->services[$name]); } return $this; }

dneira avatar Jan 29 '21 19:01 dneira