Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3241142
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:09:54+00:00 2026-05-17T18:09:54+00:00

I found this article on Context Variables in an earlier version of Ninject. My

  • 0

I found this article on Context Variables in an earlier version of Ninject. My question is two-fold. First, how can I get this behavior with Ninject 2? Secondly, do context variables carry through down the request chain? For example, let’s say I wanted to replace these calls:

var a = new A(new B(new C())));
var specialA = new A(new B(new SpecialC()));

… with this:

var a = kernel.Get<A>();
var specialA = kernel.Get<A>(With.Parameters.ContextVariable("special", "true"));

Is it possible to set up a binding like this, where the context remembers that it is in a “special” context when it comes time to construct a C?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T18:09:55+00:00Added an answer on May 17, 2026 at 6:09 pm

    Here’s some stuff that I use against V2, with ~0 effort to clean it up for you – let me know if you can’t disentagle it.

    As you surmised, there doesn’t seem to be a really explicit API that surfaces the “context parameter, even for nested resolutions” stuff in v2 as-is (it’s presence is buried as the 3rd parameter on an overload of the Parameter ctor).

    public static class ContextParameter
    {
        public static Parameter Create<T>( T value )
        {
            return new Parameter( value.GetType().FullName, value, true );
        }
    }
    
    public static class ContextParameterFacts
    {
        public class ProductId
        {
            public ProductId( string productId2 )
            {
                Value = productId2;
    
            }
            public string Value { get; set; }
        }
    
        public class Repository
        {
            public Repository( ProductId productId )
            {
                ProductId = productId;
    
            }
            public ProductId ProductId { get; set; }
        }
    
        public class Outer
        {
            public Outer( Repository repository )
            {
                Repository = repository;
            }
            public Repository Repository { get; set; }
        }
    
        public class Module : NinjectModule
        {
            public override void Load()
            {
                Bind<ProductId>().ToContextParameter();
            }
        }
    
        //[ Fact ]
        public static void TwoDeepShouldResolve()
        {
            var k = new StandardKernel( new Module() );
            var o = k.Get<Outer>( ContextParameter.Create( new ProductId( "a" ) ) );
            Debug.Assert( "a" == o.Repository.ProductId.Value );
        }
    }
    

    And here’s some code [that’ll confuse the matter] which demonstrates how I apply it in my context:-

    public class ServicesNinjectModule : NinjectModule
    {
        public override void Load()
        {
            Bind<ProductId>().ToContextParameter();
    
            Bind<Func<ProductId, ResourceAllocator>>().ToConstant( ( productId ) => Kernel.Get<ResourceAllocator>(
                ContextParameter.Create( productId ) ) );
        }
    }
    
    public static class NinjectContextParameterExtensions
    {
        public static IBindingWhenInNamedWithOrOnSyntax<T> ToContextParameter<T>( this IBindingToSyntax<T> bindingToSyntax )
        {
            return bindingToSyntax.ToMethod( context => (T)context.Parameters.Single( parameter => parameter.Name == typeof( T ).FullName ).GetValue( context ) );
        }
    }
    

    As usual, you should go look a the source and the tests – they’ll provide you with a far more detailed and relevant answer than I can.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found this in an article on Multithreaded Apartments, but can’t find a definition
I've found this MSDN article that explains how to monitor processes and services with
I read This article and i found it interesting. To sum it up for
I've found an article on this subject by a Microsoft employee, but has anyone
Reading this question I found this as (note the quotation marks) code to solve
Just found this out, so i am answering my own question :) Use a
While searching for linq conditional where clause, I found this article , the way
I was reading this article by Brandon Aaron here , about how jquery context
I've managed to setup jboss as windows service found this article http://www.jboss.org/jbossweb/install/service.html When I
On a Google Search, I found this article: http://docs.python.org/release/1.4/lib/node201.html Which showed examples of using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.