SignalR/NetCore3.1/React.Js ~/negotiate?negotiateVersion=1 404 (не найдено) (верите, что это проблема CORS)

Продолжайте получать эту ошибку ниже:

localhost:3000/negotiate?negotiateVersion=1 404 (не найдено) Ошибка: не удалось завершить согласование с сервером: ошибка: не найдено Ошибка: не удалось установить соединение: ошибка: не найдено

Это версии программного обеспечения

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.SignalR.Common" Version="5.0.0-``
    <PackageReference Include="Microsoft.AspNetCore.SignalR.Core" Version="1.1.0" />
  </ItemGroup>

---------КЛИЕНТ --------

  "dependencies": {
    "@microsoft/signalr": "^3.1.4",

Вот мой Cors.cs. Я пробовал не включать .WithOrigins или менять порт, но это не помогает.

    public static void ConfigureServices(IServiceCollection services)
    {
        services.AddCors(options =>
        {
            options.AddPolicy("AllowAllCors", builder =>
            {
                builder

                .WithOrigins("https://localhost:3000")
                .AllowAnyHeader()
                .AllowAnyMethod()
                .AllowCredentials()
                .SetIsOriginAllowedToAllowWildcardSubdomains()
                .SetIsOriginAllowed(delegate (string requestingOrigin)
                {
                    return true;
                }).Build();
            });
        });
    public static void Configure(IApplicationBuilder app)
    {
        app.UseCors("AllowAllCors");
        app.UseSignalR(routes =>
        {
            routes.MapHub<ChatHub>("/client/messages");
        });
    }

--

import React, { Component } from 'react';
import * as signalR from '@microsoft/signalr';

class EmailEditor extends Component {
  constructor(props) {
    super(props);

    this.state = {
      nick: '',
      message: '',
      messages: [],
      hubConnection: null,
    };
  }
  componentDidMount = () => {
    const nick = window.prompt('Your name:', 'John');

    const hubConnection = new signalR.HubConnectionBuilder()
      .withUrl("/client/messages")
      .configureLogging(signalR.LogLevel.Information)
      .build();

    this.setState({ hubConnection, nick }, () => {
      this.state.hubConnection.start()
        .then(() => console.log('Connection started!'))
        .catch(err => console.log('Error while establishing connection :('+ err));

      this.state.hubConnection.on('ReceiveMessage', (nick, receivedMessage) => {
        const text = `${nick}: ${receivedMessage}`;
        const messages = this.state.messages.concat([text]);
        this.setState({ messages });
      });
    });
  };

person Cesar Rios    schedule 14.05.2020    source источник
comment
В .NET Core вам не нужны пакеты SignalR, так как SignalR встроен в .NET Core 3.1.   -  person Kiril1512    schedule 14.05.2020


Ответы (1)


Порт для сервера был на 5001, поэтому он должен был быть

const hubConnection = new signalR.HubConnectionBuilder()
      .withUrl("https://localhost:5001/client/messages")
      .configureLogging(signalR.LogLevel.Information)
      .build();
person Cesar Rios    schedule 14.05.2020