Skip to content
This repository has been archived by the owner on Dec 19, 2018. It is now read-only.

HostEngine Start not setting HttpContext.ApplicationServices - uses default DI provider #337

Closed
ghost opened this issue Aug 22, 2015 · 8 comments
Assignees
Milestone

Comments

@ghost
Copy link

ghost commented Aug 22, 2015

I am returning a UnityServiceProvider from ConfigureServices and I can see that it is being set to the ApplicationServices property (above the HostingEngine.Start method in top right of image below), however the httpContext.ApplicationServices is null which causes it to use _services resulting in the default DI provider to be used. I see the DefaultControllerActivator retrieves the HttpContext.RequestServices (default provider) resulting in my HomeController not resolving IFoo as expected (line 104 left pane) since it is not using my UnityServiceProvider.

I am making assumptions based on the limited documentation available during internet research, but will be glad to blog the topic if I am missing something and you can elaborate...

UPDATED: My work around was to add the following line in HostEngine.Start() that sets RequestServices as follows:

                using (logger.BeginScope("Request Id: {RequestId}", requestIdentifier))
                {
                    // BillKrat.2015.08.23 - work around so DI container is used
                    httpContext.RequestServices = ApplicationServices;

                    contextAccessor.HttpContext = httpContext;
                    await application(httpContext);
                }

serviceprovider

@davidfowl
Copy link
Member

Looks like a bug.

@glennc glennc added this to the 1.0.0-beta8 milestone Aug 24, 2015
@glennc
Copy link
Member

glennc commented Aug 24, 2015

@HaoK Can you investigate?

@HaoK
Copy link
Member

HaoK commented Aug 24, 2015

Ah, this is probably a sideeffect of 'cleaning up' some of the weird logic last time... Yeah I'll take a look and add a test for this

@HaoK
Copy link
Member

HaoK commented Aug 24, 2015

So I added a test which is just verifying that ApplicationServices is set to the IServiceProvider returned via ConfigureServices (shown below), which works fine. Are you not seeing context.ApplicationServices being set to the one returned from your ConfigureServices method?

        public class CustomContainerStartup
        {
            public IServiceProvider Services;
            public IServiceProvider ConfigureServices(IServiceCollection services)
            {
                Services = services.BuildServiceProvider();
                return Services;
            }

            public void Configure(IApplicationBuilder app)
            {
                app.Run(async context =>
                {
                    await context.Response.WriteAsync("ApplicationServicesEqual:" + (context.ApplicationServices == Services));
                });
            }
        }

@ghost
Copy link
Author

ghost commented Aug 25, 2015

Actually this was observed during HostingEngine.Start. In the following excerpt from the image above you'll see that at my breakpoint the ApplicationServices property is properly set to my UnityServiceProvider. However, execution of line 79 has the middleware setting httpContext.RequestServices to (default) ServiceProvider (line 43) because httpContext.ApplicationServices was null so it used the _services value which is highlighted in yellow box of the bottom pane below.

Perhaps this was a beta 6 issue? I'll upgrade to latest and see if the problem persist, may take a wee bit because of my wife's honey-do list (which entails lawn mowers, edging, and weed eating) and I'm still new with attaching to source code (I'm old school Microsoft crony and TFS is my comfort zone) but I should know in a couple of days.

middleware

@HaoK
Copy link
Member

HaoK commented Aug 25, 2015

Ah okay yeah we tweaked this code a bit recently, so hopefully this was fixed already as part of that. let me know if you still see issues once you've upgraded

@HaoK
Copy link
Member

HaoK commented Aug 25, 2015

Ah okay I think I see the issue, we aren't explicitly setting the ApplicationServices on the context it looks like, so maybe there's a gap somewhere

@HaoK
Copy link
Member

HaoK commented Aug 25, 2015

Fixed in 55b28ab

@HaoK HaoK closed this as completed Aug 25, 2015
@HaoK HaoK added the 3 - Done label Aug 25, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants