diff --git a/lib/private/appframework/dependencyinjection/dicontainer.php b/lib/private/appframework/dependencyinjection/dicontainer.php
index 81910df6990c5ae7d5bd9b59d85c17e21e78b0e0..0054b153f9105419cd44d8c2a22d0f9d3c9eae4f 100644
--- a/lib/private/appframework/dependencyinjection/dicontainer.php
+++ b/lib/private/appframework/dependencyinjection/dicontainer.php
@@ -88,8 +88,9 @@ class DIContainer extends SimpleContainer implements IAppContainer{
 		/**
 		 * Middleware
 		 */
-		$this['SecurityMiddleware'] = $this->share(function($c){
-			return new SecurityMiddleware($this, $c['Request']);
+		$app = $this;
+		$this['SecurityMiddleware'] = $this->share(function($c) use ($app){
+			return new SecurityMiddleware($app, $c['Request']);
 		});
 
         $middleWares = $this->middleWares;