首页 > 解决方案 > 带有信号器的 blazor 服务器项目一旦在本地 IIS 上发布就会停止正常工作并在连续加载中返回空白页面

问题描述


按照本教程(链接),我使用 blazor 服务器和 SignalR 开发了一个应用程序。该应用程序由视觉工作室完美执行。然后我在本地 IIS 上发布了该应用程序,以便使用不同的设备进行更真实的测试,而不仅仅是一台设备。该应用程序显然发布没有问题。我把它放在端口 80 上,以便在默认情况下通过连接到 localhost 来连接。第一次运行时,应用程序正常运行,我可以毫无问题地使用这些功能。之后,当我尝试连接到应用程序时,页面不再加载并保持白色。

我读到这个问题可能是与默认禁用的 IIS Web 套接字有关的问题。我已经尝试在一些 stackoverflow 响应之后激活它们,但我没有任何更改。

首轮结果: 首轮

第一次运行结果: 下一次运行

我附上应用程序代码:

中心:

using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BlazorApp2.Data
{
    public class ChatHub : Hub
    {
        public async Task SendMessage(string user,string message)
        {
            await Clients.All.SendAsync("ReciveMessage", user, message);
        }
    }
}

看法:

   @page "/"
@using Microsoft.AspNetCore.SignalR.Client
@inject NavigationManager _navManager

<div class="row">
    <div class="col-md-4">
        <h2>
            Chat app
        </h2>

        <div class="form-group">
            <label>
                User : <input type="text" class="form-control" @bind="_userInput" />
            </label>
        </div>
        <div class="form-group">
            <label>
                Message : <input type="text" class="form-control" @bind="_message" />
            </label>
        </div>
        <button @onclick="Send" disabled="@(!IsConnected)">Send</button>
    </div>
</div>
<p>@path.ToString()</p>
<p>@path.AbsoluteUri</p>
<hr />
<ul id="messageList">
    @foreach(var message in _messages)
    {
        <li>
            @message
        </li>
    }
</ul>

@error

@code{
    private HubConnection _hubConnection;
    private string _userInput = "";
    private string _message = "";
    private Uri path;
    private List<string> _messages = new List<string>();
    private string error = "";

    protected override async Task OnInitializedAsync()
    {
        try
        {
            path = _navManager.ToAbsoluteUri("/chatHub");
            _hubConnection = new HubConnectionBuilder().
                WithUrl(path)
                .Build();
            _hubConnection.On<string, string>("ReciveMessage", (user, message) =>
            {
                var encodeMS = $"{user}:{message}";
                _messages.Add(encodeMS);
                StateHasChanged();
            });

            await _hubConnection.StartAsync();
        }
        catch (Exception e)
        {
            error = e.Message;
        }
    }

    Task Send() =>
            _hubConnection.SendAsync("SendMessage", _userInput, _message);

    public bool IsConnected =>
        _hubConnection.State == HubConnectionState.Connected;

}

启动:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using BlazorApp2.Data;

namespace BlazorApp2
{
    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.AddRazorPages();
            services.AddServerSideBlazor();
            services.AddSingleton<WeatherForecastService>();
            services.AddSignalR();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment 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();
            }

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

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapBlazorHub();
                endpoints.MapFallbackToPage("/_Host");
                endpoints.MapHub<ChatHub>("/chatHub");
            });
        }
    }
}

标签: c#asp.net-coresignalrblazoriis-8

解决方案


推荐阅读