Drupal 8 Menu Tree issue in my contrib theme possibly because of "Convert menu tree building to a service." Previously, in my Drupal 8 contrib theme, I was rendering the full menu tree as:

function gratis_preprocess_page(&amp;$vars, $hook) {

// Build the main menu links tree.
  $vars['primary_nav'] = FALSE;
  if (!empty($vars['main_menu'])) {
  // Build links.
    $config = Drupal::config('menu.settings');
    $menu_enabled = Drupal::moduleHandler()-&gt;moduleExists('menu');
    // When menu module is not enabled, we need a hardcoded default values.
    $main_links_source = $menu_enabled ? $config-&gt;get('main_links') : 'main';
    $vars['main_menu'] = menu_tree($main_links_source);
    $vars['main_menu']['#theme_wrappers'] = array('menu_tree__primary');
    $vars['main_menu']['#attributes']['id'] = 'main-menu-links';
    $vars['main_menu']['#attributes']['class'] = array('links', 'clearfix');
  }
}


However, in testing the theme with the latest Drupal 8 dev, I am getting a WSOD with this specific error:

PHP Fatal error:  Call to undefined function menu_tree() in /themes/gratis/gratis.theme on line 93


Line 93 from the above is:

$vars['main_menu'] = menu_tree($main_links_source);


I did a little searching and discovered this core change: Convert menu tree building to a service. and its corresponding change notice.

I tried adjusting my code to: 

$vars['main_menu']= \Drupal::service($main_links_source);


... but now I am getting this error:

Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: You have requested a non-existent service "main". Did you mean one of these: "maintenance_mode_subscriber", "user_maintenance_mode_subscriber"? in Symfony\Component\DependencyInjection\Container-&gt;get() (line 303 of core/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/Container.php).


So I am at a loss of how to fix this. You can see the repo for my .theme file here.

Cross referencing Render full menu tree in Drupal 8 version.

Update: [working code]

Here is the code in its fixed and working version thanks once again to @Clive

// Build the main menu links tree.
  if (!empty($vars['main_menu'])) {
  // Build links.
    $config = Drupal::config('menu.settings');
    $menu_enabled = Drupal::moduleHandler()-&gt;moduleExists('menu');
    // When menu module is not enabled, we need a hardcoded default values.
    $main_links_source = $menu_enabled ? $config-&gt;get('main_links') : 'main';
    $vars['main_menu'] = \Drupal::service('menu_link.tree')-&gt;renderMenu($main_links_source);
    $vars['main_menu']['#attributes']['id'] = 'main-menu-links';
    $vars['main_menu']['#attributes']['class'] = array('links', 'clearfix');
    $vars['main_menu']['#theme_wrappers'] = array('menu_tree__primary');
  }


and then we render the wrapper set above with menu_tree__primary

/**
 * Theme wrapper function for the primary menu links.
 */
function gratis_menu_tree__primary(&amp;$vars) {
  return '&lt;ul class="toggle-menu"&gt;' . $vars['tree'] . '&lt;/ul&gt;';
}
Does https://drupal.org/node/2226481 help you?

This should be something like

\Drupal::service('menu_link.tree')-&gt;renderMenu($main_link_source);