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

TinyIoC doesn't allow to override IReactEnvironment with AsPerRequestSingleton lifetime #336

Open
asmagin opened this Issue Oct 16, 2016 · 3 comments

Comments

Projects
None yet
3 participants
@asmagin

asmagin commented Oct 16, 2016

I'm trying to override react components via providing a new implementation of IReactEnvironment.

in order to do that, I've created library "*.React" and added AssemblyRegistration class there.

    public class AssemblyRegistration : IAssemblyRegistration
    {
        /// <summary>
        /// Gets the IoC container. Try to avoid using this and always use constructor injection.
        /// This should only be used at the root level of an object heirarchy.
        /// </summary>
        public static TinyIoCContainer Container
        {
            get { return TinyIoCContainer.Current; }
        }

        /// <summary>
        /// Registers standard components in the React IoC container
        /// </summary>
        /// <param name="container">Container to register components in</param>
        public void Register(TinyIoCContainer container)
        {
            // One instance shared for the whole app
            container.Register<IReactEnvironment, ReduxEnvironment>().AsPerRequestSingleton();
        }
    }

The problem is when I'm running this code I'm getting following error.
image

If I edit this code directly in React.Core or comment out original registration for IReactEnvironment site is working fine.

@Daniel15

This comment has been minimized.

Member

Daniel15 commented Oct 16, 2016

Hmm, interesting... Could you please post the full stack trace?

@asmagin

This comment has been minimized.

asmagin commented Oct 18, 2016

[ReactAspNetException: Trying to store item in HttpContext.Current while not in an ASP.NET request!]
   React.Web.TinyIoC.HttpContextLifetimeProvider.SetObject(Object value) +201
   React.TinyIoC.SafeDictionary`2.set_Item(TKey key, TValue value) +162
   React.TinyIoC.TinyIoCContainer.Register() +155
   Smagin.Alex.React.AssemblyRegistration.Register(TinyIoCContainer container) in D:\.projects\Smagin\src\server\Smagin.Alex.React\AssemblyRegistration.cs:37
   React.Initializer.InitializeIoC(Func`2 requestLifetimeRegistration) +512
   React.Web.WebInitializer.Initialize() +71

[TargetInvocationException: Exception has been thrown by the target of an invocation.]
   System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) +0
   System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) +260
   System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +146
   WebActivatorEx.BaseActivationMethodAttribute.InvokeMethod() +102
   WebActivatorEx.ActivationManager.RunActivationMethods(Boolean designerMode) +718
   WebActivatorEx.ActivationManager.Run() +125

[InvalidOperationException: The pre-application start initialization method Run on type WebActivatorEx.ActivationManager threw an exception with the following error message: Exception has been thrown by the target of an invocation..]
   System.Web.Compilation.BuildManager.InvokePreStartInitMethodsCore(ICollection`1 methods, Func`1 setHostingEnvironmentCultures) +877
   System.Web.Compilation.BuildManager.InvokePreStartInitMethods(ICollection`1 methods) +169
   System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded) +133
   System.Web.Compilation.BuildManager.ExecutePreAppStart() +176
   System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +734

[HttpException (0x80004005): The pre-application start initialization method Run on type WebActivatorEx.ActivationManager threw an exception with the following error message: Exception has been thrown by the target of an invocation..]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +539
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +125
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +731

here is what I've got

@chrisd37

This comment has been minimized.

chrisd37 commented Apr 12, 2017

Ran across this error today in my mixed MVC + ASP.Net app but with React.Initializer.Initialize(ReactConfig.AsPerRequestSingleton);

Solved it by relocating my item storing code from ReactConfig.cs' configure() to application_start() in global.asax.cs so I had an HttpContext.Current

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