diff --git a/core/core.services.yml b/core/core.services.yml
index b238304..5597a4f 100644
--- a/core/core.services.yml
+++ b/core/core.services.yml
@@ -292,8 +292,6 @@ services:
     arguments: ['@database', '@lock']
   router.request_context:
     class: Symfony\Component\Routing\RequestContext
-    calls:
-      - [fromRequest, ['@request']]
   router.admin_context:
     class: Drupal\Core\Routing\AdminContext
     calls:
@@ -317,10 +315,9 @@ services:
       - [setFinalMatcher, ['@router.matcher.final_matcher']]
   url_generator:
     class: Drupal\Core\Routing\UrlGenerator
-    arguments: ['@router.route_provider', '@path_processor_manager', '@route_processor_manager', '@config.factory', '@settings']
+    arguments: ['@router.route_provider', '@router.request_context', '@path_processor_manager', '@route_processor_manager', '@config.factory', '@settings']
     calls:
       - [setRequest, ['@?request']]
-      - [setContext, ['@?router.request_context']]
   link_generator:
     class: Drupal\Core\Utility\LinkGenerator
     arguments: ['@url_generator', '@module_handler', '@path.alias_manager.cached']
@@ -330,8 +327,8 @@ services:
   router:
     class: Symfony\Cmf\Component\Routing\ChainRouter
     calls:
-      - [setContext, ['@router.request_context']]
       - [add, ['@router.dynamic']]
+      - [setContext, ['@router.request_context']]
   router.path_roots_subscriber:
     class: Drupal\Core\EventSubscriber\PathRootsSubscriber
     arguments: ['@state']
@@ -476,7 +473,7 @@ services:
     class: Symfony\Component\HttpKernel\EventListener\RouterListener
     tags:
       - { name: event_subscriber }
-    arguments: ['@router']
+    arguments: ['@router', '@router.request_context', NULL, '@request_stack']
   content_negotiation:
     class: Drupal\Core\ContentNegotiation
   view_subscriber:
diff --git a/core/lib/Drupal/Core/DrupalKernel.php b/core/lib/Drupal/Core/DrupalKernel.php
index 8172b48..ea086f4 100644
--- a/core/lib/Drupal/Core/DrupalKernel.php
+++ b/core/lib/Drupal/Core/DrupalKernel.php
@@ -368,8 +368,11 @@ protected function getKernelParameters() {
   protected function initializeContainer() {
     $this->containerNeedsDumping = FALSE;
     $persist = $this->getServicesToPersist();
-    // The request service requires custom persisting logic, since it is also
-    // potentially scoped.
+    // The request and request_context services require custom persisting logic.
+    // This is because during WebTestBase::setUp the container is initialized
+    // multiple times and it is necessary to persist those two services.
+    // Additionally it is necessary to keep track of the request context and
+    // restore after it if exists here.
     $request_scope = FALSE;
     if (isset($this->container)) {
       if ($this->container->isScopeActive('request')) {
@@ -378,6 +381,9 @@ protected function initializeContainer() {
       if ($this->container->initialized('request')) {
         $request = $this->container->get('request');
       }
+      if ($this->container->initialized('router.request_context')) {
+        $request_context = $this->container->get('router.request_context');
+      }
     }
     $this->container = NULL;
     $class = $this->getClassName();
@@ -456,6 +462,9 @@ protected function initializeContainer() {
     if (isset($request)) {
       $this->container->set('request', $request);
     }
+    if (isset($request_context)) {
+      $this->container->set('router.request_context', $request_context);
+    }
     \Drupal::setContainer($this->container);
   }
 
diff --git a/core/lib/Drupal/Core/Routing/UrlGenerator.php b/core/lib/Drupal/Core/Routing/UrlGenerator.php
index 2402708..94e1789 100644
--- a/core/lib/Drupal/Core/Routing/UrlGenerator.php
+++ b/core/lib/Drupal/Core/Routing/UrlGenerator.php
@@ -10,6 +10,7 @@
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpKernel\Log\LoggerInterface;
 
+use Symfony\Component\Routing\RequestContext;
 use Symfony\Component\Routing\Route as SymfonyRoute;
 use Symfony\Component\Routing\Exception\RouteNotFoundException;
 
@@ -80,6 +81,8 @@ class UrlGenerator extends ProviderBasedGenerator implements UrlGeneratorInterfa
    *
    * @param \Drupal\Core\Routing\RouteProviderInterface $provider
    *   The route provider to be searched for routes.
+   * @param \Symfony\Component\Routing\RequestContext $request_context
+   *   The request context shared with the router listener and url matcher.
    * @param \Drupal\Core\PathProcessor\OutboundPathProcessorInterface $path_processor
    *   The path processor to convert the system path to one suitable for urls.
    * @param \Drupal\Core\RouteProcessor\OutboundRouteProcessorInterface $route_processor
@@ -91,8 +94,8 @@ class UrlGenerator extends ProviderBasedGenerator implements UrlGeneratorInterfa
    * @param \Symfony\Component\HttpKernel\Log\LoggerInterface $logger
    *   An optional logger for recording errors.
    */
-  public function __construct(RouteProviderInterface $provider, OutboundPathProcessorInterface $path_processor, OutboundRouteProcessorInterface $route_processor, ConfigFactoryInterface $config, Settings $settings, LoggerInterface $logger = NULL) {
-    parent::__construct($provider, $logger);
+  public function __construct(RouteProviderInterface $provider, RequestContext $request_context, OutboundPathProcessorInterface $path_processor, OutboundRouteProcessorInterface $route_processor, ConfigFactoryInterface $config, Settings $settings, LoggerInterface $logger = NULL) {
+    parent::__construct($provider, $request_context, $logger);
 
     $this->pathProcessor = $path_processor;
     $this->routeProcessor = $route_processor;
diff --git a/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php
index 9d876a4..0c8fddd 100644
--- a/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php
+++ b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php
@@ -1147,8 +1147,17 @@ protected function rebuildContainer($environment = 'testing') {
     // DrupalKernel replaces the container in \Drupal::getContainer() with a
     // different object, so we need to replace the instance on this test class.
     $this->container = \Drupal::getContainer();
-    // The current user is set in TestBase::prepareEnvironment().
+
+    // Restore the request.
     $this->container->set('request', $request);
+
+    // The request context is normally set by the router_listener from within
+    // its KernelEvents::REQUEST listener. In the simpletest parent site this
+    // event is not fired, therefore it is necessary to updated the request
+    // context manually here.
+    $this->container->get('router.request_context')->fromRequest($request);
+
+    // The current user is set in TestBase::prepareEnvironment().
     $this->container->set('current_user', \Drupal::currentUser());
   }
 
diff --git a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
index 8efa40a..3df8e34 100644
--- a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
+++ b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
@@ -3687,6 +3687,8 @@ protected function prepareRequestForGenerator($clean_urls = TRUE, $override_serv
 
     $request = Request::create($request_path, 'GET', array(), array(), array(), $server);
     $generator->setRequest($request);
+    $this->container->get('router.request_context')->fromRequest($request);
     return $request;
   }
+
 }
diff --git a/core/tests/Drupal/Tests/Core/Routing/UrlGeneratorTest.php b/core/tests/Drupal/Tests/Core/Routing/UrlGeneratorTest.php
index d1c59c9..e69dd3f 100644
--- a/core/tests/Drupal/Tests/Core/Routing/UrlGeneratorTest.php
+++ b/core/tests/Drupal/Tests/Core/Routing/UrlGeneratorTest.php
@@ -133,13 +133,11 @@ function setUp() {
 
     $config_factory_stub = $this->getConfigFactoryStub(array('system.filter' => array('protocols' => array('http', 'https'))));
 
-    $generator = new UrlGenerator($provider, $processor_manager, $this->routeProcessorManager, $config_factory_stub, new Settings(array()));
-    $generator->setContext($context);
+    $generator = new UrlGenerator($provider, $context, $processor_manager, $this->routeProcessorManager, $config_factory_stub, new Settings(array()));
     $this->generator = $generator;
 
     // Second generator for mixed-mode sessions.
-    $generator = new UrlGenerator($provider, $processor_manager, $this->routeProcessorManager, $config_factory_stub, new Settings(array('mixed_mode_sessions' => TRUE)));
-    $generator->setContext($context);
+    $generator = new UrlGenerator($provider, $context, $processor_manager, $this->routeProcessorManager, $config_factory_stub, new Settings(array('mixed_mode_sessions' => TRUE)));
     $this->generatorMixedMode = $generator;
   }
 
diff --git a/core/vendor/symfony-cmf/routing/Symfony/Cmf/Component/Routing/ProviderBasedGenerator.php b/core/vendor/symfony-cmf/routing/Symfony/Cmf/Component/Routing/ProviderBasedGenerator.php
index e3aef91..37edab2 100644
--- a/core/vendor/symfony-cmf/routing/Symfony/Cmf/Component/Routing/ProviderBasedGenerator.php
+++ b/core/vendor/symfony-cmf/routing/Symfony/Cmf/Component/Routing/ProviderBasedGenerator.php
@@ -35,11 +35,11 @@ class ProviderBasedGenerator extends UrlGenerator implements VersatileGeneratorI
      */
     protected $provider;
 
-    public function __construct(RouteProviderInterface $provider, LoggerInterface $logger = null)
+    public function __construct(RouteProviderInterface $provider, RequestContext $context, LoggerInterface $logger = null)
     {
         $this->provider = $provider;
+        $this->context = $context;
         $this->logger = $logger;
-        $this->context = new RequestContext();
     }
 
     /**
