New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
BillKrat opened this Issue Aug 22, 2015 · 8 comments

Comments

Projects
None yet
5 participants
@BillKrat

BillKrat 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

This comment has been minimized.

Show comment
Hide comment
@davidfowl

davidfowl Aug 23, 2015

Member

Looks like a bug.

Member

davidfowl commented Aug 23, 2015

Looks like a bug.

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

@glennc

This comment has been minimized.

Show comment
Hide comment
@glennc

glennc Aug 24, 2015

Member

@HaoK Can you investigate?

Member

glennc commented Aug 24, 2015

@HaoK Can you investigate?

@HaoK

This comment has been minimized.

Show comment
Hide comment
@HaoK

HaoK Aug 24, 2015

Member

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

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

This comment has been minimized.

Show comment
Hide comment
@HaoK

HaoK Aug 24, 2015

Member

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));
                });
            }
        }
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));
                });
            }
        }
@BillKrat

This comment has been minimized.

Show comment
Hide comment
@BillKrat

BillKrat 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

BillKrat 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

This comment has been minimized.

Show comment
Hide comment
@HaoK

HaoK Aug 25, 2015

Member

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

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

This comment has been minimized.

Show comment
Hide comment
@HaoK

HaoK Aug 25, 2015

Member

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

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

This comment has been minimized.

Show comment
Hide comment
@HaoK

HaoK Aug 25, 2015

Member

Fixed in 55b28ab

Member

HaoK commented Aug 25, 2015

Fixed in 55b28ab

@HaoK HaoK closed this Aug 25, 2015

@HaoK HaoK added the 3 - Done label Aug 25, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment