Skip to content
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

Ninject 3.2.2 InRequestScope not returning the same instance for the same request #30

Open
Hovo563 opened this issue Mar 28, 2016 · 7 comments

Comments

@Hovo563
Copy link

Hovo563 commented Mar 28, 2016

a new instance was created each time.
this is my binding
Bind().To().InRequestScope();

@foresightyj
Copy link

I also encountered a similar issue. Basically, Ninject.Web.Commons uses HttpContext.Current as the current scope. If you are using InRequestScope in selfhost web api environemtn where HttpContext.Current is null. InRequestScope will fail to work.

Check out the file /src/Ninject.Web.Common/WebCommonNinjectModule.cs

@Hovo563
Copy link
Author

Hovo563 commented May 10, 2016

I use owin host web api envirement and it still doesn't work

@cornillemichiel
Copy link

cornillemichiel commented Apr 17, 2018

We face this same issue when using async code with ConfigureAwait(false) with the service locator (anti)pattern inside that code.
Too bad ninject requestscope has a dependency on HttpContext.Current

@foresightyj
Copy link

@cornillemichiel you can check out this project. https://github.com/neuecc/OwinRequestScopeContext

@cornillemichiel
Copy link

cornillemichiel commented Apr 23, 2018

@foresightyj I checked it out, . but is this a Ninject compatible thing?

@foresightyj
Copy link

Yeah it works perfectly fine for more than a year already. This is my extension method:

      public static IBindingNamedWithOrOnSyntax<T> InCustomRequestScope<T>(this IBindingInSyntax<T> syntax)
       {
           return syntax.InScope(ctx =>
           {
               //var typeName = ctx.Request.Target.Type.Name;
               //var argumentName = ctx.Request.Target.Name;
               //var parentTypeName = ctx.Request.ParentContext.Request.Service.Name;
               if (HttpContext.Current != null)
               {
                   return HttpContext.Current;
               }
               if (OwinRequestScopeContext.Current != null)
               {
                   return OwinRequestScopeContext.Current;
               }
             
               throw new Exception("I haven't yet to expect this");
           });
       }

@wendelstam
Copy link

wendelstam commented Jul 20, 2018

I had the same problem and found that there were no component implementation of INinjectHttpApplicationPlugin included nor loaded, and since it worked when using the older Ninject.Web (whos nuget package does not support the latest release of Ninject) library I found that creating a custom module and loading it (or just adding the component directly) solved it (for the HttpContext.Current but will probably be portable to the OWIN pipeline as well).

internal class PerRequestWebModule : NinjectModule
    {
        public override string Name { get; } = "Xyz";

        public override void Load()
        {
            this.Kernel.Components.Add<INinjectHttpApplicationPlugin, RequestScopeNinjectWebHttpApplicationPlugin>();
        }

        internal class RequestScopeNinjectWebHttpApplicationPlugin : NinjectComponent, INinjectHttpApplicationPlugin
        {
            /// <summary>
            /// Gets the request scope.
            /// </summary>
            /// <param name="context">The context.</param>
            /// <returns>The request scope.</returns>
            public object GetRequestScope(IContext context)
            {
                return HttpContext.Current;
            }

            public void Start()
            {
            }

            public void Stop()
            {
            }
        }
    }

And the call is made from the InRequestScope extension method (see below)

https://github.com/ninject/Ninject.Web.Common/blob/master/src/Ninject.Web.Common/RequestScopeExtensionMethod.cs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants