Skip to content

Unable to resolve type: React.IReactEnvironment (no JsEngineSwitcher misconceptions!) #640

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

Closed
adamjford opened this issue Dec 18, 2018 · 10 comments

Comments

@adamjford
Copy link

adamjford commented Dec 18, 2018

Hi there,

Despite it working for a week or more beforehand, suddenly now getting these errors whenever I call a ReactJS.NET Html helper:

[TinyIoCResolutionException: Unable to resolve type: React.IReactEnvironment]
   React.TinyIoC.TinyIoCContainer.ResolveInternal(TypeRegistration registration, NamedParameterOverloads parameters, ResolveOptions options) +821
   React.TinyIoC.TinyIoCContainer.Resolve() +46
   React.ReactEnvironment.get_GetCurrentOrThrow() +66

[ReactNotInitialisedException: ReactJS.NET has not been initialised correctly. Please ensure you have called services.AddReact() and app.UseReact() in your Startup.cs file.]
   React.ReactEnvironment.get_GetCurrentOrThrow() +158
   React.Web.Mvc.<>c__DisplayClass2_0`1.<React>b__0(TextWriter writer) +359
   React.Web.Mvc.ActionHtmlString.ToHtmlString() +201
   System.Web.WebPages.WebPageBase.Write(Object value) +103
   ASP._Page_Areas_Administration_Views_LoginPageStatusMessages_Index_cshtml.Execute() in C:\git\CompTracker.Web\src\CompTracker.Web\Areas\Administration\Views\LoginPageStatusMessages\Index.cshtml:31
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +252
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +148
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +107
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +375
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +88
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +775
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +775
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +81
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +973
   System.Web.Mvc.<>c__DisplayClass22.<BeginExecuteCore>b__1e() +39
   System.Web.Mvc.Async.AsyncResultWrapper.<.cctor>b__0(IAsyncResult asyncResult, Action action) +24
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +52
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +39
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +38
   System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +43
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +73
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +38
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +602
   System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +195
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +128

My application is an ASP.NET MVC 5 web app running in IIS 10.0.17763.1, and I have the following initialization code running at application start:

            IReactSiteConfiguration config = ReactSiteConfiguration.Configuration;

            config
                .SetLoadReact(false)
                .SetLoadBabel(false)
                .SetReuseJavaScriptEngines(true);

#if DEBUG
            const string reactLibraryType = "development";
            const string otherType = "";
#else
            const string reactLibraryType = "production.min";
            const string otherType = ".min";
#endif

            const string reactDirectory = "~/Scripts/ReactJS/";
            string reactBundle = reactDirectory + $"ReactJS.bundled.{reactLibraryType}.js";
            string reactServer = reactDirectory + $"Libraries/react-dom-server.browser.{reactLibraryType}.js";
            string components = reactDirectory + $"ReactJS.server-side-components.bundled{otherType}.js";

            void AddScripts(params string[] scripts)
            {
                foreach (string script in scripts)
                {
                    config.AddScriptWithoutTransform(script);
                }
            }

            AddScripts(
                reactBundle,
                reactServer,
                components
            );

            JsEngineSwitcher.Instance.DefaultEngineName = V8JsEngine.EngineName;
            JsEngineSwitcher.Instance.EngineFactories.AddV8();

(The React bundle contains the React and ReactDOM libraries.)

I went around ReactJS.NET and attempted to render the components via ReactDOM.render, and they worked as expected, so it doesn't appear to be a problem with my bundles or scripts.

As per the JSEngineSwitcher misconceptions (#409) article, I also have the JSEngineSwitcher V8.Native x86 package installed, I confirmed that x64\ClearScriptV8-64.dll and x64\v8-x64.dll are being copied to my bin\ folder, and attempting to install the appropriate C++ redistributable gives me a message that I already have it installed. I've tried installing the x86 native package as well just in case, and no difference.

Here are the versions of relevant packages I have installed:

  <package id="JavaScriptEngineSwitcher.ChakraCore" version="2.4.28" targetFramework="net471" />
  <package id="JavaScriptEngineSwitcher.Core" version="2.4.10" targetFramework="net471" />
  <package id="JavaScriptEngineSwitcher.Msie" version="2.4.19" targetFramework="net471" />
  <package id="JavaScriptEngineSwitcher.V8" version="2.4.13" targetFramework="net471" />
  <package id="JavaScriptEngineSwitcher.V8.Native.win-x64" version="2.4.16" targetFramework="net471" />
  <package id="JavaScriptEngineSwitcher.Vroom" version="2.4.15" targetFramework="net471" />
  <package id="React.Core" version="3.4.1" targetFramework="net471" />
  <package id="React.Web" version="3.4.1" targetFramework="net471" />
  <package id="React.Web.Mvc4" version="3.4.1" targetFramework="net471" />

Any ideas?

@Taritsyn
Copy link
Contributor

Hello, Adam!

Today I also faced this error, when I was working on answer to this issue. I recommend you to read the “ReactJS.NET - Bundles - TinyIoCResolutionException: Unable to resolve type: React.IReactEnvironment” discussion on Stack Overflow.

@adamjford
Copy link
Author

adamjford commented Dec 18, 2018

Hmm, I don't use the bundler code mentioned in those issues (my .jsx files are ran through Babel via the WebCompiler extension, then the bundles are generated by the BundlerMinifier extension and bundleconfig.json, all at design-time). However, to verify this bundling is also not a problem, I just tried switching from adding my ReactJS library bundle to including the react, react-dom, and react-dom-server files directly, and I still get the same error.

I also still get the same error if I don't include my local copies of the React libraries, and call .SetLoadReact(true) instead of .SetLoadReact(false).

@dustinsoftware
Copy link
Member

@adamjford a few things to try..

  1. If you turn on all "Common language runtime exceptions", are any other exceptions thrown before the TinyIoCResolutionException is thrown? There could be another hidden exception that is causing the environment to fail.

  2. Does loading a different Javascript engine, such as ChakraCore work?

  3. If you clone this repo, there are a few sample projects. Do the React.Sample.Mvc4 or the tutorial projects build and run?

@dustinsoftware
Copy link
Member

@adamjford there isn't enough information to debug this further right now. We just added some better debugging support in the latest 4.0.0-rc2 package to re-throw specific engine exceptions instead, so that may be helpful here.

@adamjford
Copy link
Author

adamjford commented Jan 10, 2019

Thank you for the list of things to try!

  1. I enabled all CLR exceptions, and no exceptions occurred that seemed to have anything to do with loading the React environment, or JS engines, etc.
  2. I then tried switching to ChakraCore. No change.
  3. I then cloned this repo and built & ran React.Sample.Mvc4. Worked fine, of course. 😓

I'll try using the 4.0.0-rc2 version of the package and see if it helps.

@adamjford
Copy link
Author

Unfortunately, running 4.0.0-rc2 didn't cause anything different to occur. Exact same exceptions occur, with no new InnerExceptions or anything.

Guess I'm back to figuring out why React.Sample.Mvc4 works and my project doesn't.

@Taritsyn
Copy link
Contributor

In this case, you must verify that the [assembly: WebActivatorEx.PreApplicationStartMethod(...)] attribute is used correctly (see example).

@adamjford
Copy link
Author

Alright, I replaced my startup code with exactly this file in App_Start\, to see if I could at least get to a missing component exception or something instead when I try to use a ReactJS.NET Html helper:

/*
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

using JavaScriptEngineSwitcher.Core;
using JavaScriptEngineSwitcher.V8;

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(React.Sample.Mvc4.ReactConfig), "Configure")]

namespace React.Sample.Mvc4
{
    public static class ReactConfig
    {
        public static void Configure()
        {
            ReactSiteConfiguration.Configuration
                                  .SetReuseJavaScriptEngines(true);

            JsEngineSwitcher.Current.DefaultEngineName = V8JsEngine.EngineName;
            JsEngineSwitcher.Current.EngineFactories.AddV8();
        }
    }
}

Unfortunately, I still get the exact same exception as above.

I'm starting to think something about how my project is set up is preventing PreApplicationStartMethod code from running in general (which would indeed cause WebInitializer.Initialize() to not get called, and thus the React environment would not be initialized), but I haven't had any luck searching for what the cause of that could be. Going to keep digging.

@Taritsyn
Copy link
Contributor

Unfortunately, running 4.0.0-rc2 didn't cause anything different to occur. Exact same exceptions occur, with no new InnerExceptions or anything.

Yesterday version 4.0.0 was released, which exactly included the “Save original exceptions as inner exceptions” pull request.

@adamjford
Copy link
Author

Tried running 4.0.0, no change.

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

3 participants