{"action":"closed","issue":{"url":"https://api.github.com/repos/aspnet/Hosting/issues/653","repository_url":"https://api.github.com/repos/aspnet/Hosting","labels_url":"https://api.github.com/repos/aspnet/Hosting/issues/653/labels{/name}","comments_url":"https://api.github.com/repos/aspnet/Hosting/issues/653/comments","events_url":"https://api.github.com/repos/aspnet/Hosting/issues/653/events","html_url":"https://github.com/aspnet/Hosting/issues/653","id":141119624,"number":653,"title":"Make RequestServicesContainerMiddleware the first middleware in the pipeline","user":{"login":"weidazhao","id":3486339,"avatar_url":"https://avatars.githubusercontent.com/u/3486339?v=3","gravatar_id":"","url":"https://api.github.com/users/weidazhao","html_url":"https://github.com/weidazhao","followers_url":"https://api.github.com/users/weidazhao/followers","following_url":"https://api.github.com/users/weidazhao/following{/other_user}","gists_url":"https://api.github.com/users/weidazhao/gists{/gist_id}","starred_url":"https://api.github.com/users/weidazhao/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/weidazhao/subscriptions","organizations_url":"https://api.github.com/users/weidazhao/orgs","repos_url":"https://api.github.com/users/weidazhao/repos","events_url":"https://api.github.com/users/weidazhao/events{/privacy}","received_events_url":"https://api.github.com/users/weidazhao/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/aspnet/Hosting/labels/3%20-%20Done","name":"3 - Done","color":"CCCCCC"},{"url":"https://api.github.com/repos/aspnet/Hosting/labels/bug","name":"bug","color":"fc2929"}],"state":"closed","locked":false,"assignee":{"login":"JunTaoLuo","id":2030323,"avatar_url":"https://avatars.githubusercontent.com/u/2030323?v=3","gravatar_id":"","url":"https://api.github.com/users/JunTaoLuo","html_url":"https://github.com/JunTaoLuo","followers_url":"https://api.github.com/users/JunTaoLuo/followers","following_url":"https://api.github.com/users/JunTaoLuo/following{/other_user}","gists_url":"https://api.github.com/users/JunTaoLuo/gists{/gist_id}","starred_url":"https://api.github.com/users/JunTaoLuo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JunTaoLuo/subscriptions","organizations_url":"https://api.github.com/users/JunTaoLuo/orgs","repos_url":"https://api.github.com/users/JunTaoLuo/repos","events_url":"https://api.github.com/users/JunTaoLuo/events{/privacy}","received_events_url":"https://api.github.com/users/JunTaoLuo/received_events","type":"User","site_admin":false},"milestone":{"url":"https://api.github.com/repos/aspnet/Hosting/milestones/18","html_url":"https://github.com/aspnet/Hosting/milestones/1.0.0-rc2","labels_url":"https://api.github.com/repos/aspnet/Hosting/milestones/18/labels","id":1371266,"number":18,"title":"1.0.0-rc2","description":null,"creator":{"login":"muratg","id":1808428,"avatar_url":"https://avatars.githubusercontent.com/u/1808428?v=3","gravatar_id":"","url":"https://api.github.com/users/muratg","html_url":"https://github.com/muratg","followers_url":"https://api.github.com/users/muratg/followers","following_url":"https://api.github.com/users/muratg/following{/other_user}","gists_url":"https://api.github.com/users/muratg/gists{/gist_id}","starred_url":"https://api.github.com/users/muratg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/muratg/subscriptions","organizations_url":"https://api.github.com/users/muratg/orgs","repos_url":"https://api.github.com/users/muratg/repos","events_url":"https://api.github.com/users/muratg/events{/privacy}","received_events_url":"https://api.github.com/users/muratg/received_events","type":"User","site_admin":false},"open_issues":7,"closed_issues":63,"state":"open","created_at":"2015-10-22T21:04:09Z","updated_at":"2016-03-17T22:57:15Z","due_on":null,"closed_at":null},"comments":6,"created_at":"2016-03-15T22:42:19Z","updated_at":"2016-03-17T22:57:15Z","closed_at":"2016-03-17T22:57:15Z","body":"When we add a middleware through IStartupFilter, it will be added before RequestServicesContainerMiddleware in the pipeline (see the impl at https://github.com/aspnet/Hosting/blob/dev/src/Microsoft.AspNetCore.Hosting/Internal/WebHost.cs#L163) Therefore, the middleware won't be able to get any scoped service instance from HttpContext.RequestServices since RequestServices is not constructed by RequestServicesContainerMiddleware yet at the moment.\r\n\r\nThis could be fixed by ensuring RequestServicesContainerMiddleware always to be the first middleware in the pipeline. A possible fix is to move https://github.com/aspnet/Hosting/blob/dev/src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs#L202 to https://github.com/aspnet/Hosting/blob/dev/src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs#L218.\r\n\r\nFor more context and details, check out https://github.com/weidazhao/Hosting/pull/8."}}