发布到Azure,asp.net核心React应用 [英] Publish to azure, asp.net core React app

查看:60
本文介绍了发布到Azure,asp.net核心React应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Google上搜索了很多,但还没有弄清楚该怎么做.我有一个要在azure上部署的Web应用程序.我生成了链接,但是使用我的React组件的Routes返回了500状态代码.

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.

对于我的组件,我使用的是JSX,我将它们放在/js下的wwwroot文件夹中(我没有使用ES16).

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

我需要一些东西将其捆绑并将jsx处理为js,对吗?我不明白为什么它可以在本地主机上工作.

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

在Startup.cs上,配置方法,我添加了如下脚本:

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()
              });

        });

很抱歉,这是一个愚蠢的问题,但是我似乎找不到关于asp.net核心的很好的解释.

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

我正在使用VS2017,asp.net core 1.0.4和React.AspNet 3.0.1

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

错误日志:

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()

它说我没有app.AddReact或app.UseReact,但是我俩都没有...

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?}");
        });


    }

推荐答案

这是因为您的PC上有JavaScript引擎,而Azure中没有. 摘自: https://github.com/reactjs/React.NET/issues/400 (实际上是有关如何使React工作的完整说明)

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)

结合使用以下指南,我能够使应用程序在发布到Azure时正常工作:

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/JS引擎注册

在第一本指南中,我分别安装了依赖项:

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

从第二本指南中,我安装了一个软件包以在.NET Core 1.X中启用JavaScriptEngineSwitcher配置:

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

Install-Package JavaScriptEngineSwitcher.Extensions.MsDependencyInjection

在Startup.cs

In Startup.cs

ConfigureServices():
services.AddJsEngineSwitcher(options => options.DefaultEngineName = ChakraCoreJsEngine.EngineName).AddChakraCore();

这篇关于发布到Azure,asp.net核心React应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆