Cannot autowire service: Argument references class but no such service exists

Cannot autowire service: Argument references class but no such service exists

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Cannot autowire service: Argument references class but no such service exists

Upgrading from Symfony 3 to Symfony 4 introduces a significant shift in how dependencies are resolved. The error message Cannot autowire service "App\Entity\ActivationRepository": argument "$class" of method "Doctrine\ORM\EntityRepository::__construct()" references class "Doctrine\ORM\Mapping\ClassMetadata" but no such service exists is one of the most common stumbling blocks during this transition. It often feels like a bug in your configuration, but it is actually a deliberate change in Doctrine's architecture and Symfony's autowiring behavior.

In Symfony 3, the container was less strict about resolving every argument automatically, and legacy patterns allowed repositories to be instantiated without explicitly defining the ClassMetadata instance as a service. However, in Symfony 4, the container attempts to resolve every constructor argument for any class registered as a service. When you define App\Entity\ActivationRepository in your services configuration (either via resource: '../src/*' or an explicit definition), the container looks at its constructor signature.

The parent class, Doctrine\ORM\EntityRepository, requires two arguments: an EntityManagerInterface and a Mapping\ClassMetadata object. While Symfony 4 can easily inject the EntityManagerInterface because Doctrine registers it as a service, it does not automatically register every ClassMetadata instance associated with your entities. This is intentional; ClassMetadata instances are transient objects created during the boot process to describe entity mappings, not long-lived services that should be stored in the container.

The solution is not to create a service definition for Doctrine\ORM\Mapping\ClassMetadata in your services.yaml. Doing so would require you to manually instantiate every entity's metadata at application startup, which defeats the purpose of lazy loading and efficient memory management. Instead, you need to tell Doctrine how to resolve these arguments dynamically based on the specific repository being instantiated.

You can achieve this by configuring the repository class specifically in your services file. By adding a definition for App\Entity\ActivationRepository (or using a wildcard if you have many), you can override the default autowiring behavior for that specific class. The key is to provide a factory or a custom constructor argument resolver, but the most straightforward approach in Symfony 4 is often to ensure the repository is not treated as a standard service that needs full constructor resolution unless necessary.

However, a more elegant and common pattern found in upgraded projects involves defining the repository with a specific configuration that bypasses the strict autowiring for the ClassMetadata argument. You do this by adding an explicit definition for the repository class that does not attempt to autowire the second argument, or by ensuring your services.yaml is configured to handle Doctrine repositories correctly.

Consider this configuration in config/services.yaml:

App\Entity\ActivationRepository:
    class: App\Entity\ActivationRepository
    public: true
    arguments:
        $em: '@doctrine.orm.default_entity_manager'
        # We do NOT autowire $class here because it is a transient object

In this example, we explicitly inject the EntityManagerInterface but leave the ClassMetadata argument out of the arguments list. When the container tries to instantiate this service, it sees that one argument is missing from the manual definition and does not attempt to autowire it from the container. The responsibility then falls back to Doctrine's internal logic, which knows exactly which metadata instance corresponds to the repository being created.

This approach works because EntityRepository is designed to accept the metadata instance directly in its constructor, and if you don't provide it via the service definition, Doctrine's ORM layer will supply the correct instance at runtime when the repository is actually used within a unit of work. This maintains the separation of concerns: your service definitions handle persistent dependencies (like managers), while transient data structures (like metadata) are managed by the framework's internal lifecycle.

If you have many repositories extending EntityRepository, using a wildcard resource rule like resource: '../src/*' might be too broad if it inadvertently picks up classes that rely on this specific constructor signature without explicit configuration. It is often safer to explicitly define your repository classes or use a tag-based approach for entities and repositories, letting Doctrine handle the registration of repository proxies automatically while you only configure the service layer logic in your YAML files.

Remember that the goal of autowiring is to reduce boilerplate for your services, not to force the container to resolve every single argument of every parent class, especially when those arguments represent stateful context rather than simple dependencies. By explicitly defining the arguments you want to inject and omitting the ones handled by the framework, you restore the expected behavior from Symfony 3 while enjoying the benefits of the new dependency injection system.

Our network