Skip to content

TinyIoCContainer Can't replace classes registered as AsPerRequestSingleton() in ReactConfig #39

@tejacques

Description

@tejacques

There are really two issues:

1.) The following code in ReactConfig.cs will throw an exception on GetObject():

var container = AssemblyRegistration.Container;
container.Register<IReactEnvironment, ReactEnvironment>()
    .AsPerRequestSingleton();

This can be run only in places where HttpContext.Current is defined, because re-registering a class with AsPerRequestSingleton checks the items in the current HttpContext (I believe to remove it). This could be fixed with an if (null != HttpContext.Current) { /* do things */ }

2.) Even when registering a new ReactEnvironment successfully getting JSX files appears to break. A bare minimum test case I have is this: Follow ReactJS.Net tutorial and have a solution with that set up:

Create a new class:

using React;

namespace ReactDotNetExample
{
    public class CustomReactEnvironment : ReactEnvironment
    {
        public CustomReactEnvironment(
            IJavaScriptEngineFactory engineFactory,
            IReactSiteConfiguration config,
            ICache cache,
            IFileSystem fileSystem,
            IFileCacheHash fileCacheHash
        ) : base(engineFactory, config, cache, fileSystem, fileCacheHash)
        { }
    }
}

Add the following to the HomeController static constructor (HttpContext.Current will be defined here):

var container = AssemblyRegistration.Container;
container.Register<IReactEnvironment, CustomReactEnvironment>()
    .AsPerRequestSingleton();

Grabbing .jsx files will now fail even though CustomReactEnvironment and ReactEnvironment are functionally identical (500 error in browser). Adding the following line:

var container = AssemblyRegistration.Container;
container.Register<IReactEnvironment, CustomReactEnvironment>()
    .AsPerRequestSingleton();
container.Register<IReactEnvironment, ReactEnvironment>()
    .AsPerRequestSingleton();

Works, so the problem is somehow the CustomReactEnvironment class.

For what it's worth, I was trying to see if not disposing/deleting JS Engines per request would speed up server side rendering time, but I couldn't get it to work with a custom environment at all.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions