Publish to azure, asp.net core React app

2019-07-31 18:27发布

I've googled alot, and i haven't figured out how to do it. I have a web app that i would like to deploy on azure. I generates the link but the Routes that use my React components, return a 500 status code.

For my components i'm using JSX and i have them in the wwwroot folder under/js (i'm not using ES16).

I need something to bundle it and process that jsx into js right? I don't understand why it works on localhost.

On Startup.cs, configure method, i added the scripts like this:

app.UseReact(config =>
        {

            config
              .AddScript("~/js/test.jsx")
              .AddScript("~/js/users.jsx")
              .AddScript("~/js/userCards.jsx")
              .AddScript("~/js/userInfo.jsx")
              .AddScript("~/js/cards.jsx")
              .AddScript("~/js/user.jsx")
              .AddScript("~/js/addCard.jsx")
              .AddScript("~/js/cardImages.jsx")

              .SetJsonSerializerSettings(new JsonSerializerSettings
              {
                  StringEscapeHandling = StringEscapeHandling.EscapeHtml,
                  ContractResolver = new CamelCasePropertyNamesContractResolver()
              });

        });

I'm sorry if it is a silly question, but i can't seem to find a good explanation for asp.net core.

[EDIT]I'm using VS2017, asp.net core 1.0.4 and React.AspNet 3.0.1

The error logs:

ReactEngineNotFoundException: No usable JavaScript engine was found. Please 
install a JavaScript engine such as React.JavaScriptEngine.ClearScriptV8 (on 
 Windows) or React.JavaScriptEngine.VroomJs (on Linux and Mac OS X). Refer 
  to the ReactJS.NET documentation for more details.
   React.AspNet.HtmlHelperExtensions.get_Environment()

   TinyIoCResolutionException: Unable to resolve type: 
  React.JavaScriptEngineFactory
  React.AspNet.HtmlHelperExtensions.get_Environment()

  TinyIoCResolutionException: Unable to resolve type: React.ReactEnvironment
  React.AspNet.HtmlHelperExtensions.get_Environment()

   ReactNotInitialisedException: ReactJS.NET has not been initialised 
   correctly. Please ensure you have called app.AddReact() and 
   app.UseReact() in your Startup.cs file.
    React.AspNet.HtmlHelperExtensions.get_Environment()

It says i don't have app.AddReact nor app.UseReact, but i have then both...

 public void ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
        services.AddReact();

        services.AddServerSentEvents();
        services.AddServerSentEvents<INotificationsServerSentEventsService, NotificationsServerSentEventsService>();

        services.AddMvc();            

        services.AddSingleton<HttpClient>();
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IServiceProvider serviceProvider)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseBrowserLink();
        }
        else
        {
            app.UseDeveloperExceptionPage();
            app.UseBrowserLink();
        }


        app.MapServerSentEvents("/see-heartbeat");
        app.MapServerSentEvents("/sse-notifications", serviceProvider.GetService<NotificationsServerSentEventsService>());

        app.UseStatusCodePagesWithReExecute("/Error/{0}");

        app.UseReact(config =>
        {

            config
              .AddScript("~/js/test.jsx")
              .AddScript("~/js/users.jsx")
              .AddScript("~/js/userCards.jsx")
              .AddScript("~/js/userInfo.jsx")
              .AddScript("~/js/cards.jsx")
              .AddScript("~/js/user.jsx")
              .AddScript("~/js/addCard.jsx")
              .AddScript("~/js/cardImages.jsx")

              .SetJsonSerializerSettings(new JsonSerializerSettings
              {
                  StringEscapeHandling = StringEscapeHandling.EscapeHtml,
                  ContractResolver = new CamelCasePropertyNamesContractResolver()
              });

        });

        app.UseDefaultFiles();
        app.UseStaticFiles();


        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Index}/{action=Index}/{id?}");
        });


    }

2条回答
啃猪蹄的小仙女
2楼-- · 2019-07-31 18:56

Most likely, there is not installed the Internet Explorer 9+ (or Microsoft Edge) on your server and you did not install native assemblies for the JavaScriptEngineSwitcher.V8 and JavaScriptEngineSwitcher.ChakraCore modules.

Also I recommend you to read the “Misconceptions about the JavaScript Engine Switcher version 2.X” discussion.

查看更多
疯言疯语
3楼-- · 2019-07-31 19:01

That's because JavaScript Engine you have on your PC and not in Azure. Taken from: https://github.com/reactjs/React.NET/issues/400 (actually a complete instruction on how to get React working)

I was able to get the application working when published to Azure using a combination of these guides:

http://www.samulihaverinen.com/web-development/dotnet/2016/01/19/how-to-run-clearscript-v8-javascript-engine-in-azure/

https://github.com/Taritsyn/JavaScriptEngineSwitcher/wiki/Registration-of-JS-engines

From the first guide, I installed dependencies separately:

Uninstall-Package React.AspNet
Install-Package JavaScriptEngineSwitcher.Core
Install-Package JavaScriptEngineSwitcher.Msie (May not be needed)
Install-Package JSPool
Install-Package JavaScriptEngineSwitcher.ChakraCore
Install-Package JavaScriptEngineSwitcher.ChakraCore.Native.win-x64 (Selected native implementation)
Install-Package Microsoft.ChakraCore (I think this is key to enabling ChakraCore on the server)
Install-Package React.AspNet -IgnoreDependencies

From the second guide, I installed a package to enable JavaScriptEngineSwitcher configuration in .NET Core 1.X:

Install-Package JavaScriptEngineSwitcher.Extensions.MsDependencyInjection

In Startup.cs

ConfigureServices():
services.AddJsEngineSwitcher(options => options.DefaultEngineName = ChakraCoreJsEngine.EngineName).AddChakraCore();
查看更多
登录 后发表回答