在发布到IIS无法启动NPM

问题描述 投票:1回答:1

我出版我的asp.net 2.2核心角7 Web应用程序IIS,我得到以下错误:

  1. 确保“新公共管理”已安装并可以在路径中的一个目录中找到。
  2. 出现InvalidOperationException:无法启动“新公共管理”。
  3. AggregateException:一个或多个错误发生。 (发生一个或多个错误。(无法启动“NPM”。为了解决这样的:[1]确保“NPM”安装,并且可以在PATH目录中的一个中找到)。

这是在Windows 2012 R2服务器上运行一个Asp.Net核心的Web应用程序IIS 8.我的本地机器与IIS 8具有相同的错误。


我launchsettings.json简介:

   "Test.IIS": {
        "commandName": "IIS",
        "launchBrowser": true,
        "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
   },
   "ancmHostingModel": "InProcess",
   "applicationUrl": "ttps://localhost:5002"
   }

下面是我的startup.cs文件:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

        // In production, the Angular files will be served from this directory
        services.AddSpaStaticFiles(configuration =>
        {
            configuration.RootPath = "ClientApp/dist";
        });
    }


    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        //To check which process is runing the application
        //app.Run(async (contenxt) => {
        //    await contenxt.Response.WriteAsync(System.Diagnostics.Process.GetCurrentProcess().ProcessName);
        //});

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseSpaStaticFiles();

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

        app.UseSpa(spa =>
        {
            // To learn more about options for serving an Angular SPA from ASP.NET Core,
            // see https://go.microsoft.com/fwlink/?linkid=864501

            spa.Options.SourcePath = "ClientApp";

            if (env.IsDevelopment())
            {
                spa.UseAngularCliServer(npmScript: "start");
            }
        });
    }

代码编译成功,但在运行时抛出的错误。

angular7 asp.net-core-2.2
1个回答
0
投票

这听起来像在asp.net 2.2核心一个已知的问题,如果你已经通过了进程托管模式的IIS。

这个问题已经被记录在这里:https://github.com/aspnet/AspNetCore/issues/5263可链接到这里的另一个问题GitHub的:https://github.com/aspnet/AspNetCore/issues/4206

虽然补丁开发,解决办法是添加以下帮手(更改命名空间为必填项):

using System;

namespace SampleApp
{
    internal class CurrentDirectoryHelpers
    {
        internal const string AspNetCoreModuleDll = "aspnetcorev2_inprocess.dll";

        [System.Runtime.InteropServices.DllImport("kernel32.dll")]
        private static extern IntPtr GetModuleHandle(string lpModuleName);

        [System.Runtime.InteropServices.DllImport(AspNetCoreModuleDll)]
        private static extern int http_get_application_properties(ref IISConfigurationData iiConfigData);

        [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
        private struct IISConfigurationData
        {
            public IntPtr pNativeApplication;
            [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.BStr)]
            public string pwzFullApplicationPath;
            [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.BStr)]
            public string pwzVirtualApplicationPath;
            public bool fWindowsAuthEnabled;
            public bool fBasicAuthEnabled;
            public bool fAnonymousAuthEnable;
        }

        public static void SetCurrentDirectory()
        {
            try
            {
                // Check if physical path was provided by ANCM
                var sitePhysicalPath = Environment.GetEnvironmentVariable("ASPNETCORE_IIS_PHYSICAL_PATH");
                if (string.IsNullOrEmpty(sitePhysicalPath))
                {
                    // Skip if not running ANCM InProcess
                    if (GetModuleHandle(AspNetCoreModuleDll) == IntPtr.Zero)
                    {
                        return;
                    }

                    IISConfigurationData configurationData = default(IISConfigurationData);
                    if (http_get_application_properties(ref configurationData) != 0)
                    {
                        return;
                    }

                    sitePhysicalPath = configurationData.pwzFullApplicationPath;
                }

                Environment.CurrentDirectory = sitePhysicalPath;
            }
            catch
            {
                // ignore
            }
        }
    }
}

然后将下面的代码添加到您的Program.cs文件的第一行:

CurrentDirectoryHelpers.SetCurrentDirectory();

我遇到了同样的问题,这项工作围绕解决了这个问题对我来说。

© www.soinside.com 2019 - 2024. All rights reserved.