【问题标题】:Failed to start NPM when published to IIS发布到 IIS 时启动 NPM 失败
【发布时间】:2019-01-23 20:38:59
【问题描述】:

我正在将我的 asp.net core 2.2 Angular 7 Web 应用程序发布到 IIS,但出现以下错误:

  1. 确保已安装“npm”并且可以在 PATH 目录之一中找到。
  2. InvalidOperationException:无法启动“npm”。
  3. AggregateException:出现一个或多个错误。 (发生了一个或多个错误。(无法启动“npm”。要解决此问题:。[1] 确保已安装“npm”并且可以在 PATH 目录之一中找到)。

这适用于运行 IIS 8 的 Windows 2012 R2 服务器上的 Asp.Net 核心 Web 应用程序。 我的 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】:

    如果您采用了 IIS 进程内托管模型,这听起来像是 asp.net core 2.2 中的一个已知问题。

    该问题已在此处记录: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();
    

    我遇到了同样的问题,这项工作为我解决了。

    【讨论】:

    • 谢谢肯斯派克!将尝试此解决方案并更新您。
    猜你喜欢
    • 2023-03-04
    • 2019-08-11
    • 1970-01-01
    • 1970-01-01
    • 2017-03-29
    • 2017-05-28
    • 2014-09-29
    • 1970-01-01
    • 2022-10-20
    相关资源
    最近更新 更多