diff --git a/core/core.services.yml b/core/core.services.yml
index 2b27f69..a8fa3fe 100644
--- a/core/core.services.yml
+++ b/core/core.services.yml
@@ -388,6 +388,8 @@ services:
     class: SplString
     factory_service: 'app.root.factory'
     factory_method: 'get'
+    tags:
+      - { name: non_class }
   app.root.factory:
     class: Drupal\Core\AppRootFactory
     arguments: ['@kernel']
diff --git a/core/lib/Drupal/Core/CoreServiceProvider.php b/core/lib/Drupal/Core/CoreServiceProvider.php
index 249944f..155004c 100644
--- a/core/lib/Drupal/Core/CoreServiceProvider.php
+++ b/core/lib/Drupal/Core/CoreServiceProvider.php
@@ -10,6 +10,7 @@
 use Drupal\Core\Cache\CacheContextsPass;
 use Drupal\Core\Cache\ListCacheBinsPass;
 use Drupal\Core\DependencyInjection\Compiler\BackendCompilerPass;
+use Drupal\Core\DependencyInjection\Compiler\DependencySerializationTraitPass;
 use Drupal\Core\DependencyInjection\Compiler\StackedKernelPass;
 use Drupal\Core\DependencyInjection\Compiler\RegisterStreamWrappersPass;
 use Drupal\Core\DependencyInjection\ServiceProviderInterface;
@@ -73,6 +74,8 @@ public function register(ContainerBuilder $container) {
 
     // Register plugin managers.
     $container->addCompilerPass(new PluginManagerPass());
+
+    $container->addCompilerPass(new DependencySerializationTraitPass());
   }
 
   /**
diff --git a/core/lib/Drupal/Core/DependencyInjection/Compiler/DependencySerializationTraitPass.php b/core/lib/Drupal/Core/DependencyInjection/Compiler/DependencySerializationTraitPass.php
new file mode 100644
index 0000000..56d5f01
--- /dev/null
+++ b/core/lib/Drupal/Core/DependencyInjection/Compiler/DependencySerializationTraitPass.php
@@ -0,0 +1,34 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\Core\DependencyInjection\Compiler\DependencySerializationTraitPass.
+ */
+
+namespace Drupal\Core\DependencyInjection\Compiler;
+
+use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+
+/**
+ * Sets the _serviceId property on all services.
+ *
+ * @see \Drupal\Core\DependencyInjection\DependencySerializationTrait
+ */
+class DependencySerializationTraitPass implements CompilerPassInterface {
+
+  /**
+   * {@inheritdoc}
+   */
+  public function process(ContainerBuilder $container) {
+    foreach ($container->getDefinitions() as $service_id => $definition) {
+      // Some services might have strings internally.
+      // Given that you can just reload a service which is accessible via
+      // Container::get, you need to filter out public services here.
+      if (!$definition->hasTag('non_class') && $definition->isPublic()) {
+        $definition->setProperty('_serviceId', $service_id);
+      }
+    }
+  }
+
+}
diff --git a/core/lib/Drupal/Core/DependencyInjection/Container.php b/core/lib/Drupal/Core/DependencyInjection/Container.php
index 0d4c972..2ad015c 100644
--- a/core/lib/Drupal/Core/DependencyInjection/Container.php
+++ b/core/lib/Drupal/Core/DependencyInjection/Container.php
@@ -17,14 +17,13 @@ class Container extends SymfonyContainer {
   /**
    * {@inheritdoc}
    */
-  public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE) {
-    $service = parent::get($id, $invalidBehavior);
-    // Some services are called but do not exist, so the parent returns nothing.
-    if (is_object($service)) {
-      $service->_serviceId = $id;
-    }
+  public function set($id, $service, $scope = SymfonyContainer::SCOPE_CONTAINER) {
+     parent::set($id, $service, $scope);
 
-    return $service;
+    // Ensure that the _serviceId property is set on synthetic services as well.
+    if (!isset($this->services[$id]->_serviceId)) {
+      $this->services[$id]->_serviceId = $id;
+    }
   }
 
   /**
diff --git a/core/tests/Drupal/Tests/Core/DependencyInjection/ContainerTest.php b/core/tests/Drupal/Tests/Core/DependencyInjection/ContainerTest.php
index e892f22..7c8fbf8 100644
--- a/core/tests/Drupal/Tests/Core/DependencyInjection/ContainerTest.php
+++ b/core/tests/Drupal/Tests/Core/DependencyInjection/ContainerTest.php
@@ -31,18 +31,12 @@ protected function setUp() {
   }
 
   /**
-   * Tests the get method.
+   * Tests serialization.
    *
-   * @see \Drupal\Core\DependencyInjection\Container::get()
+   * @expectedException \PHPUnit_Framework_Error
    */
-  public function testGet() {
-    $service = new \stdClass();
-    $service->key = 'value';
-
-    $this->container->set('test_service', $service);
-    $result = $this->container->get('test_service');
-    $this->assertSame($service, $result);
-    $this->assertEquals('test_service', $result->_serviceId);
+  public function testSerialize() {
+    serialize($this->container);
   }
 
 }
