【问题标题】:How can I deploy a Blazor server-hosted application from Visual Studio 2019如何从 Visual Studio 2019 部署 Blazor 服务器托管应用程序
【发布时间】:2019-06-11 12:08:42
【问题描述】:

我正在使用 VS2019 预览版。 我使用最新的 Blazor 扩展 (16.0.19227) 创建了一个“服务器托管”的 Blazor 应用程序。这是包含 3 个独立项目的变体...

  • MyApp.Client
  • MyApp.Server
  • MyApp.Shared

我可以通过将 MyApp.Server 设为活动项目来调试它,一切正常,但我很难将它发布/部署到 Azure。我已经尝试了以下...

  • 在 Solution-Explorer 中右键单击 MyApp.Server
  • 选择“发布”
  • 通过向导创建新的发布配置文件
  • 将部署模式更改为“自包含”
  • 点击发布

此时我在部署过程中遇到错误...

CSC(0,0):错误 CS0006:元数据文件 'D:\work\Applications\Web\MyApp.Client\bin\Release\netstandard2.0\win-x86\MyApp.Client.dll' 找不到

这似乎是因为 web-deploy 配置文件中的 “目标运行时” 设置为 win-x86。客户端应用程序实际上正在构建为

"D:\work\Applications\Web\MyApp.Client\bin\Release\netstandard2.0\MyApp.Client.dll"

(没有额外的 win-x86 子文件夹)因此部署过程似乎对构建过程使用的路径做出了错误的假设。发布对话框中无法指定空白/无关目标运行时。

是否有解决方法,或者我使用了错误的部署方法?

有一些official documentation,但不是很有帮助。

更新似乎部署正在使用客户端项目的输出路径,然后只是将 netstandard2.0{Target Runtime} 附加到它以便更改输出路径在客户端项目中不足以解决此问题。

更新 2 通过编辑 xml 删除发布配置文件中的 RuntimeIdentifier 标记只会导致部署时错误,指出空的 RuntimeIdentifier 与自包含的不兼容部署。不幸的是,自包含部署是必要的,因为 Azure 尚未直接托管 .net core 3。

【问题讨论】:

  • 当前是否有任何需要发布为“自包含”的原因?我目前也有错误使用它。但是,以“框架相关”模式(目标运行时:可移植)发布似乎可以正常发布到 azure appservice。 (我收到有关不受支持的警告,但它仍然有效)。编辑:请记住,在撰写本文时,blazor 仍处于预览阶段
  • 假设我需要使用自包含,因为服务器项目的目标是“netcoreapp3.0”,而这在服务器上不存在。在发布对话框中,“netcoreapp3.0”是唯一受支持的目标框架,“可移植”不是目标运行时下拉列表中的选项。对于服务器托管的 Blazor 应用,您肯定会看到这些选项吗?

标签: visual-studio blazor blazor-server-side


【解决方案1】:

因为 Azure 尚未直接托管 .net core 3。

但确实如此。

在 Azure 门户中,部署后转到您的 WebApp(或预先创建一个)。

转到扩展并单击添加 [+] 并选择 ASP.NET Core 3(x86 用于免费托管)。

还可以转到设置、常规并启用 WebSockets,默认情况下它们是关闭的。


临时:

请注意,Preview-6 不能作为扩展提供,因此请使用 Preview-5 或部署为独立的。

【讨论】:

  • 感谢 Henk - 这些确实是缺少的步骤!只是为了完整性...扩展是“ASP.NET Core 3.0 (x86) Runtime”(如果您在“Core”下看不到它),WebSockets 设置在 Settings/Configuration/General Settings/Platform Settings 下。
  • 是的,我得靠记忆去。
  • 感谢@HenkHolterman,但尝试添加此扩展时出现错误:“Conflict (HTTP Status Code: 409)
  • 这里没有线索。发表一个单独的问题,好好记录一下。
【解决方案2】:

无法在评论中放一张图片,所以我想我会在这里展示它。这是我当前的发布向导。

刚刚通过新项目 -> Asp.net 核心 Web 应用程序 -> blazor(托管的 Asp.net 核心)构建并发布到 azure 应用程序服务的全新项目。

【讨论】:

  • 感谢美联社 - 实际上将 Henks 的回复标记为“答案”,但这也很有用。
  • 我同意,Henk 的回答更好。实际上,我现在也这样做了,以确保它正常运行。
  • 在我的应用程序上获得 404,尽管它在预览版 5 中工作 - 看起来 dotnet publish 要么已损坏,要么 Blazor 版本已损坏。内容未设置。也无法从 VS2019 运行发布(也已损坏)
  • 您发布 BlazorTest.Server 还是 BlazorTest.Client?
【解决方案3】:

我的回答是:

  • 将发布配置文件配置为“自包含”部署模式。
  • 编辑所有 .csproj 文件以将 <TargetFramework>...</TargetFramework> 节点名称更改为 <TargetFrameworks>...</TargetFrameworks>。 (另见:https://stackoverflow.com/a/42855070
  • 在运行时修复Startup 类中的Web 根文件夹路径字符串,如下所示。
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.ResponseCompression;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json.Serialization;
using System.IO;
using System.Linq;

namespace BlazorHostedOnAzure.Server
{
    public class Startup
    {
        // 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.AddMvc().AddNewtonsoftJson();
            services.AddResponseCompression(opts =>
            {
                opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
                    new[] { "application/octet-stream" });
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseResponseCompression();

            // ---- APPEND PART.1 BEGIN ----
            var clientBlazorWebRootPath = default(string);
            // ---- APPEND PART.1 END ----

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseBlazorDebugging();
            }

            // ---- APPEND PART.2 BEGIN ----
            else
            {
                if (env.WebRootPath != null)
                {
                    var pathOfIndex = Path.Combine(env.WebRootPath, "index.html");
                    var pathOfContent = Path.Combine(env.WebRootPath, "_content");
                    if (!File.Exists(pathOfIndex) && Directory.Exists(pathOfContent))
                    {
                        clientBlazorWebRootPath = Directory.GetDirectories(pathOfContent).FirstOrDefault();
                        if (clientBlazorWebRootPath != null)
                        {
                            env.WebRootPath = clientBlazorWebRootPath;
                        }
                    }
                }
            }
            // ---- APPEND PART.2 END ----

            app.UseClientSideBlazorFiles<Client.Startup>();

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapDefaultControllerRoute();
                endpoints.MapFallbackToClientSideBlazor<Client.Startup>("index.html");
            });

            // ---- APPEND PART.3 BEGIN ----
            if (clientBlazorWebRootPath != null)
            {
                app.UseStaticFiles(new StaticFileOptions
                {
                    FileProvider = new PhysicalFileProvider(clientBlazorWebRootPath)
                });
            }
            // ---- APPEND PART.3 BEGIN ----
        }
    }
}

我在我的存储库 GitHub 上发布了我的示例代码和 README。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-20
    • 2023-01-19
    • 1970-01-01
    • 2020-06-09
    • 2020-01-05
    • 1970-01-01
    • 2011-05-10
    • 2019-01-16
    相关资源
    最近更新 更多