.net C# - Microsoft.AspNetCore.SpaProxy在调试时未调用

s5a0g9ez  于 2023-11-20  发布在  .NET
关注(0)|答案(1)|浏览(266)

我正在尝试将一个.net 3.1 Angular(SPA模板)应用程序升级到.net 7.0。我正在尝试从旧的UseSpa()中间件迁移到.net core与利用Microsoft.AspNetCore.SpaProxy的SPA框架交互的新方法。我正在尝试通过在新的.net 7 Angular Spa模板中对现成的应用程序进行建模来更新我们的项目。
我遇到的问题是代理中间件没有被调用。我希望看到类似这样的内容:
正在启动SPA代理...当SPA代理准备就绪时,此页将自动重定向到http://localhost:44414.
因为Microsoft.AspNetCore.SpaProxy包应该被添加到中间件中,以便在ng serve准备好后启动npm命令并重定向。但是我从来没有看到它或npm脚本被启动过。
以下是我到目前为止所做的工作:
1.已将软件包添加到.csproj文件:

<PackageReference Include="Microsoft.AspNetCore.SpaProxy" Version="7.0.1" />

字符串
1.已更新LaunchSettings.json,以包含环境变量:

"environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
        "ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.SpaProxy"
      }


1.更新了项目文件,以纳入三个水疗中心:

<SpaRoot>ClientApp\</SpaRoot>
    <SpaProxyServerUrl>http://localhost:4200</SpaProxyServerUrl>
    <SpaProxyLaunchCommand>npm start</SpaProxyLaunchCommand>


我已经尝试了很多方法来解决问题,包括使Program.cs与模板完全相同。(以排除任何其他中间件冲突)

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllersWithViews();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
}

app.UseStaticFiles();
app.UseRouting();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller}/{action=Index}/{id?}");

app.MapFallbackToFile("index.html");

app.Run();


我知道包安装正确,因为我确实在bin文件夹中看到了spa.proxy.json。我还测试了环境变量是否混乱,以验证它是否生效。将其更改为:

"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.SpaProxy_WRONG"


在运行dotnet时产生正确的错误:

crit: Microsoft.AspNetCore.Hosting.Diagnostics[11]
      Hosting startup assembly exception
      System.InvalidOperationException: Startup assembly Microsoft.AspNetCore.SpaProxy_WRONG failed to execute. See the inner exception for more details.
       ---> System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.AspNetCore.SpaProxy_WRONG, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.
      File name: 'Microsoft.AspNetCore.SpaProxy_WRONG, Culture=neutral, PublicKeyToken=null'
         at System.Reflection.RuntimeAssembly.InternalLoad(AssemblyName assemblyName, StackCrawlMark& stackMark, AssemblyLoadContext assemblyLoadContext, RuntimeAssembly requestingAssembly, Boolean throwOnFileNotFound)
         at System.Reflection.Assembly.Load(AssemblyName assemblyRef)
         at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.ExecuteHostingStartups()
         --- End of inner exception stack trace ---


我已经想不出其他的办法可以尝试了。有没有人有过将Microsoft.AspNetCore.SpaProxy添加到.net7应用程序的经验,并且能想到我可能错过的任何东西?
谢谢

u3r8eeie

u3r8eeie1#

当我面对同样的问题时,打开Visual Studio“以管理员身份运行”,一切都完美地工作了!

相关问题