Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 角度/信号器错误:无法完成与服务器的协商_Asp.net_Angular_Signalr - Fatal编程技术网

Asp.net 角度/信号器错误:无法完成与服务器的协商

Asp.net 角度/信号器错误:无法完成与服务器的协商,asp.net,angular,signalr,Asp.net,Angular,Signalr,我的服务器使用SignalR,客户端使用Angular。。。运行客户端时,我收到以下错误: zone.js:2969 OPTIONS https://localhost:27967/chat/negotiate 0 () Utils.js:148 Error: Failed to complete negotiation with the server: Error Utils.js:148 Error: Failed to start the connection: Error 我猜这是

我的服务器使用SignalR,客户端使用Angular。。。运行客户端时,我收到以下错误:

zone.js:2969 OPTIONS https://localhost:27967/chat/negotiate 0 ()

Utils.js:148 Error: Failed to complete negotiation with the server: Error

Utils.js:148 Error: Failed to start the connection: Error
我猜这是因为CORS。。。我正在尝试实现一个简单的聊天应用程序。我使用的是信号机的最新版本:

下面是github,它包含我下面的教程的代码。

这是我的创业公司

    using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;

namespace signalrChat
{
    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.AddCors(o => o.AddPolicy("CorsPolicy", builder =>
            {
                builder
                    .AllowAnyMethod()
                    .AllowAnyHeader()
                    .WithOrigins("http://localhost:4200");
            }));

            services.AddSignalR();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseCors("CorsPolicy");

            app.UseSignalR(routes =>
            {
                routes.MapHub<ChatHub>("/chat");
            });
        }
    }
}
我想可能是cors的问题。谢谢大家!


编辑:我刚刚在VisualStudio中重新创建了signalr实现,它成功了。我相信我在启动时选择了错误的设置。

当我尝试连接到Azure Signal service Azure Function时,我在Angular应用程序中遇到了相同的问题

connection = new signalR.HubConnectionBuilder()
    .configureLogging(signalR.LogLevel.Debug)
    .withUrl("http://localhost:5000/decisionHub", {
      skipNegotiation: true,
      transport: signalR.HttpTransportType.WebSockets
    })
    .build();
[FunctionName("Negotiate")]
public static IActionResult Run(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, [SignalRConnectionInfo(HubName = "broadcast")] SignalRConnectionInfo info,
    ILogger log) {
    log.LogInformation("Negotiate trigger function processed a request.");
    return info != null ? (ActionResult) new OkObjectResult(info) : new NotFoundObjectResult("SignalR could not load");
}
下面是Angular service中的init()函数代码

init() {
    this.getSignalRConnection().subscribe((con: any) => {
        const options = {
            accessTokenFactory: () => con.accessKey
        };

        this.hubConnection = new SignalR.HubConnectionBuilder()
            .withUrl(con.url, options)
            .configureLogging(SignalR.LogLevel.Information)
            .build();

        this.hubConnection.start().catch(error => console.error(error));

        this.hubConnection.on('newData', data => {
            this.mxChipData.next(data);
        });
    });
}
我的问题是
con.accessKey
。我刚刚检查了
signalconnectioninfo
类的属性,了解到我需要使用
accessToken
而不是
accessKey

public class SignalRConnectionInfo {
    public SignalRConnectionInfo();

    [JsonProperty("url")]
    public string Url {
        get;
        set;
    }
    [JsonProperty("accessToken")]
    public string AccessToken {
        get;
        set;
    }
}

因此,在将代码更改为
accessTokenFactory:()=>con.accessToken
之后,一切正常

我面临着更苗条的问题,我通过添加

skipNegotiation: true,
transport: signalR.HttpTransportType.WebSockets

在客户端,如@Caims所述。但我认为这不是正确的解决方案,更像是一个黑客我为此浪费了将近两天的时间,终于找到了答案

此错误何时发生?

  • 将现有SignalR服务器项目升级到.Net Core时 但不要升级客户端
  • 当您创建signar服务器时 使用.Net内核,但客户端使用传统的.Net框架
为什么会发生此错误?

  • 出现此错误是因为new Signal不允许您使用旧服务器和新客户端或新服务器和旧客户端

  • 这意味着如果使用.Net core创建SignalR服务器,则必须使用.Net core创建客户端


这就是我的问题。

我也有同样的问题,结果是launchSettings.json在它说SignalChartServer什么都不做的部分,与我一起工作的url是iisexpress的url,我这么说是因为有很多地方他们说url就是下面的那个


在我的情况下,不需要所有这些东西,我缺少的是https而不是http,它工作起来很有魅力

const connection = new signalR.HubConnectionBuilder()
  .configureLogging(signalR.LogLevel.Debug)
  .withUrl('https://localhost:44308/message')
  .build();

您是否尝试过在.WithOriginates()中不使用“”;是的,正在使用Originates();未更改任何内容。我创建了一个具有初学者版本的存储库:请检查代理服务器设置,设置internet选项连接选项卡Lan设置->选择自动检测设置并取消选中代理配置。@John您所说的“我刚刚重新创建了信号器实现”是什么意思?请提供详细信息。它可能会请注意,我也有同样的问题。您是从头开始创建解决方案还是只重写客户端/服务器端代码?或者您重新安装/更新了一些包/库(哪些包/库)等?请给我们解释一下这是如何解决OP问题的。这里给出的解释是:“一个布尔值指示是否应该跳过协商。只有当传输属性设置为“HttpTransportType.WebSockets”时,才能跳过协商。这就是@Claims将传输类型设置为WebSocket的原因。我认为这种方法的一个缺点是,在不支持WebSocket
AllowRedentials
(使用
app.UseCors(options=>options.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().allowRedentials());在
Startup.cs configure
方法上的旧式浏览器中,这种方法不起作用;
对我有效!嗯-如果我尝试使用skip和websocket,那么我将得到一个307错误(重定向),如果没有skip,我将得到CORS错误。发现问题,我有app.UseHttpsRedirection(),它将重定向到https://...5001. 一旦我禁用了它,它就工作了
const connection = new signalR.HubConnectionBuilder()
  .configureLogging(signalR.LogLevel.Debug)
  .withUrl('https://localhost:44308/message')
  .build();