【问题标题】:Upgrade from .NET 6.0.0-preview.3.21201.13 to 6.0.0-preview.4.21253.5, app.UseBlazorFrameworkFiles(); causes error从 .NET 6.0.0-preview.3.21201.13 升级到 6.0.0-preview.4.21253.5,app.UseBlazorFrameworkFiles();导致错误
【发布时间】:2021-05-27 13:21:36
【问题描述】:

我正在使用 Microsoft Visual Studio Community 2019 Preview - Version 16.11.0 Preview 1.0。我的网络应用程序与 .NET 6.0.0-preview.3.21201.13 配合得很好。这是我用 git diff 演示的升级过程

donhuvy@DESKTOP-23PIH3M MINGW64 /d/github/acc (main)
$ git diff
diff --git a/Client/acc.Client.csproj b/Client/acc.Client.csproj
index dfa936b..b07ab26 100644
--- a/Client/acc.Client.csproj
+++ b/Client/acc.Client.csproj
@@ -1,4 +1,4 @@
-<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
+<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">

   <PropertyGroup>
     <TargetFramework>net6.0</TargetFramework>
@@ -9,11 +9,11 @@


   <ItemGroup>
-    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.0-preview.3.21201.13" />
-    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.0-preview.3.21201.13" PrivateAssets="all" />
-    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="6.0.0-preview.3.21201.13" />
-    <PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0-preview.3.21201.4" />
-    <PackageReference Include="System.Net.Http.Json" Version="6.0.0-preview.3.21201.4" />
+    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.0-preview.4.21253.5" />
+    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.0-preview.4.21253.5" PrivateAssets="all" />
+    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="6.0.0-preview.4.21253.5" />
+    <PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0-preview.4.21253.7" />
+    <PackageReference Include="System.Net.Http.Json" Version="6.0.0-preview.4.21253.7" />
   </ItemGroup>

   <ItemGroup>
diff --git a/Server/acc.Server.csproj b/Server/acc.Server.csproj
index 4361cae..d87d0a4 100644
--- a/Server/acc.Server.csproj
+++ b/Server/acc.Server.csproj
@@ -6,8 +6,8 @@
   </PropertyGroup>

   <ItemGroup>
-    <PackageReference Include="DevExpress.Document.Processor" Version="21.1.2-beta" />
-    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="6.0.0-preview.3.21201.13" />
+    <PackageReference Include="DevExpress.Document.Processor" Version="21.1.3" />
+    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="6.0.0-preview.4.21253.5" />
   </ItemGroup>

   <ItemGroup>
@@ -16,12 +16,12 @@
   </ItemGroup>

   <ItemGroup>
-    <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="6.0.0-preview.3.21201.13" />
-    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="6.0.0-preview.3.21201.13" />
-    <PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="6.0.0-preview.3.21201.13" />
-    <PackageReference Include="Microsoft.AspNetCore.ApiAuthorization.IdentityServer" Version="6.0.0-preview.3.21201.13" />
-    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0-preview.3.21201.2" />
-    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.0-preview.3.21201.2">
+    <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="6.0.0-preview.4.21253.5" />
+    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="6.0.0-preview.4.21253.5" />
+    <PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="6.0.0-preview.4.21253.5" />
+    <PackageReference Include="Microsoft.AspNetCore.ApiAuthorization.IdentityServer" Version="6.0.0-preview.4.21253.5" />
+    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0-preview.4.21253.1" />
+    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.0-preview.4.21253.1">
       <PrivateAssets>all</PrivateAssets>
       <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
     </PackageReference>
diff --git a/Shared/acc.Shared.csproj b/Shared/acc.Shared.csproj
index 70fad51..53ceae6 100644
--- a/Shared/acc.Shared.csproj
+++ b/Shared/acc.Shared.csproj
@@ -5,8 +5,8 @@
   </PropertyGroup>

   <ItemGroup>
-    <PackageReference Include="DevExpress.Blazor" Version="21.1.2-beta" />
-    <PackageReference Include="Microsoft.AspNetCore.Components" Version="6.0.0-preview.3.21201.13" />
+    <PackageReference Include="DevExpress.Blazor" Version="21.1.3" />
+    <PackageReference Include="Microsoft.AspNetCore.Components" Version="6.0.0-preview.4.21253.5" />
     <PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.2.0" />
   </ItemGroup>

(END)

错误

System.MissingMethodException
  HResult=0x80131513
  Message=Method not found: 'Microsoft.AspNetCore.Builder.IApplicationBuilder Microsoft.AspNetCore.Builder.UseExtensions.Use(Microsoft.AspNetCore.Builder.IApplicationBuilder, System.Func`3<Microsoft.AspNetCore.Http.HttpContext,Microsoft.AspNetCore.Http.RequestDelegate,System.Threading.Tasks.Task>)'.
  Source=Microsoft.AspNetCore.Components.WebAssembly.Server
  StackTrace:
   at Microsoft.AspNetCore.Builder.ComponentsWebAssemblyApplicationBuilderExtensions.<>c__DisplayClass0_0.<UseBlazorFrameworkFiles>b__1(IApplicationBuilder subBuilder)
   at Microsoft.AspNetCore.Builder.MapWhenExtensions.MapWhen(IApplicationBuilder app, Func`2 predicate, Action`1 configuration)
   at Microsoft.AspNetCore.Builder.ComponentsWebAssemblyApplicationBuilderExtensions.UseBlazorFrameworkFiles(IApplicationBuilder builder, PathString pathPrefix)
   at Microsoft.AspNetCore.Builder.ComponentsWebAssemblyApplicationBuilderExtensions.UseBlazorFrameworkFiles(IApplicationBuilder applicationBuilder)
   at acc.Server.Startup.Configure(IApplicationBuilder app, IWebHostEnvironment env) in D:\github\acc\Server\Startup.cs:line 62
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Microsoft.AspNetCore.Hosting.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder)
   at Microsoft.AspNetCore.Hosting.ConfigureBuilder.<>c__DisplayClass4_0.<Build>b__0(IApplicationBuilder builder)
   at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass15_0.<UseStartup>b__1(IApplicationBuilder app)
   at Microsoft.AspNetCore.Mvc.Filters.MiddlewareFilterBuilderStartupFilter.<>c__DisplayClass0_0.<Configure>g__MiddlewareFilterBuilder|0(IApplicationBuilder builder)
   at Microsoft.AspNetCore.HostFilteringStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder app)
   at Microsoft.AspNetCore.Hosting.GenericWebHostService.<StartAsync>d__31.MoveNext()

如果我尝试评论这一行,

文件Startup.cs

using acc.Server.Data;
using acc.Server.Models;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using System.IO;

namespace acc.Server
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
            services.AddDatabaseDeveloperPageExceptionFilter();
            services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true).AddEntityFrameworkStores<ApplicationDbContext>();
            services.AddIdentityServer().AddApiAuthorization<ApplicationUser, ApplicationDbContext>();
            services.AddAuthentication().AddIdentityServerJwt();
            services.AddControllersWithViews();
            services.AddRazorPages();
            services.AddSingleton<IFileProvider>(new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "FilesHere")));
            // services.AddDevExpressBlazor();
            // https://docs.devexpress.com/Blazor/401608/common-concepts/troubleshooting#an-unhandled-exception-on-the-current-circuit
            services.AddServerSideBlazor().AddCircuitOptions(options => { options.DetailedErrors = true; });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            var supportedCultures = new[] { "vi-VN" };
            var localizationOptions = new RequestLocalizationOptions().SetDefaultCulture(supportedCultures[0])
                .AddSupportedCultures(supportedCultures)
                .AddSupportedUICultures(supportedCultures);
            app.UseRequestLocalization(localizationOptions);
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseMigrationsEndPoint();
                app.UseWebAssemblyDebugging();
            }
            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();
            }
            app.UseHttpsRedirection();
            app.UseBlazorFrameworkFiles();
            app.UseStaticFiles();
            app.UseRouting();
            app.UseIdentityServer();
            app.UseAuthentication();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
                endpoints.MapControllers();
                endpoints.MapFallbackToFile("index.html");
            });
        }
    }
}

如何解决?

【问题讨论】:

  • Microsoft.AspNetCore.Builder.UseExtensions.Use() 显然不存在于更新中。
  • 你指导解决方案还是用肮脏的方式快速修复它?
  • 调用另一个实际存在的方法?
  • 请指导我一个具体的解决方案/几行代码。我不是很了解这些东西。
  • 您似乎已经在github.com/dotnet/aspnetcore/issues/33074 提交了错误报告。我会等着看他们要说什么。

标签: c# asp.net-core blazor blazor-webassembly asp.net-core-6.0


【解决方案1】:

我们需要删除binobj,重新构建,最重要的是运行命令:

dotnet new globaljson --sdk-version 6.0.100-preview.4.21255.9

感谢https://github.com/dotnet/aspnetcore/issues/33074#issuecomment-849667061评论的作者

https://docs.microsoft.com/en-us/dotnet/core/tools/global-json?tabs=netcore3x#globaljson-and-the-net-cli 上查看更多信息(.NET 6 的文档现在不存在)

【讨论】:

  • global.json 仅在当前文件夹或父文件夹中已有 global.json 文件时才需要。每次升级 SDK 或发布时,都应清除二进制文件夹并重新构建。如果升级 NuGet 包时添加了不会自动删除的文件,您甚至可能必须这样做
  • 没有 global.json,dotnet 将使用最新的 SDK 和运行时。需要global.json 强制使用旧版本
  • 感谢您的信息(有一些有用的信息)。在我的实践中,我必须运行上述命令以避免发生错误。如果我没有运行上述命令,我将无法运行解决方案。
  • 这意味着您在某个父文件夹中忘记了旧的global.json。如果您使用同一台机器进行实验和生产开发,您的工作项目中必须有一个global.json,以避免错误地瞄准 .NET Core 6。也许您将该文件放在磁盘的根目录中,而不是与工作相关的项目的根目录中
  • 我只是从最新的预览堆栈创建新的 Blazor WebAssembly 项目,我真的没有看到 global.json 。我在所有解决方案中都通过 Windows Exporer 搜索了它。 i.imgur.com/zndLacK.png
【解决方案2】:

检查 .csproj 文件中是否有对 Microsoft.AspNetCore.Components.WebAssembly.Server 的包引用。

<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="6.0.1" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 2017-04-27
    • 1970-01-01
    • 2018-02-25
    • 1970-01-01
    相关资源
    最近更新 更多