提问者:小点点

无法使用Angular 5客户端启动与Signalr核心集线器的连接


我有一个。运行SignalR Core集线器的Net Core 2.0 C#Web Api。我甚至无法从角度(5.0.1)启动()我的hubConnection收到此错误:

选项https://localhost:44301/hubs/sample405(方法不允许)

错误:启动连接失败。错误:方法不允许

建立连接时出错:(

XHR加载失败:选项"https://localhost:44301/hubs/sample"。

HttpError:方法不允许在XMLHttpRequest. xhr.onload[as__zone_symbol__ON_PROPERTYload](webpack-内部://…/…/…/…/@aspnet/signalr-client/dist/src/HttpClient.js:30:28)在XMLHttpRequest.wrapFn(webpack-内部://…./…/…/zone.js/dist/zone.js:1166:39)在ZoneReentate.调用Task(webpack-内部://…/…/./zone.js/dist/zone.js:425:31)在Object.onInvokeTask(webpack-内部://…/…/…/core/esm5/core.js:4816:33)在ZoneReentate.调用Task(webpack-内部://…/./…/./zone.js/dist/zone.js:425:31)/…/zone. js/dist/zone.js:499:34)在调用Task(webpack-内部://…/…/…/…/zone.js/dist/zone.js:1540:14)在XMLHttpRequest.globalZoneAware回调(webpack-内部://…./…/…/zone.js/dist/zone.js:1566:17)

我甚至在努力调试这个问题,即使在fiddler的帮助下,我也无法解决它。这似乎不是我的Api的问题,因为Kestrel控制台会打印对集线器的请求,但不会打印任何错误。我能够无问题地获取、放置和发布到我的控制器。

这是我的StartUp. cs配置(为了简洁起见,我省略了不相关的代码)

public void ConfigureServices(IServiceCollection services)
{
    services.AddCors(options =>
    {
        options.AddPolicy(
            "CorsPolicy",
            builder =>
                builder
                .AllowAnyOrigin()
                .AllowAnyHeader()
                .AllowAnyMethod()
                .AllowCredentials()
        );
    });

    services.AddAuthentication();
    services.AddMvcCore()
        .AddAuthorization(options => {...});
    services.AddAuthentication("Bearer")
        .AddIdentityServerAuthentication(options => {...});

    services.AddSignalR();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseCors("CorsPolicy");
    app.UseAuthentication();

    app.UseSignalR(routes =>
    {
        routes.MapHub<SampleHub>("/hubs/sample");
    });

    app.UseMvc();
}

这是我的样品集线器

public interface ISampleHubClient 
{
    Task receiveMessage(SampleRequest msg);
}

public class SampleHub : Hub<ISampleHubClient>
{
    private static string _connectionId;

    public async Task Subscribe(string groupName)
    {
        await Groups.AddAsync(Context.ConnectionId, groupName);

        await Clients.Client(Context.ConnectionId).receiveMessage("Subscribed to " + groupName);
    }

    /// <inheritdoc />
    public Task Send(SampleRequest msg)
    {
        ConsoleColor.Green.WriteLine("Send : " + _connectionId);

        return Clients.Group(msg.Group).receiveMessage(msg);
    }
}

这是我的Angular组件代码:

import { HubConnection } from '@aspnet/signalr-client';

@Component({})
export class SignalRTestComponent implements OnInit {
    private hubConnection: HubConnection;

    ngOnInit() {
        this.initHub();
    }

    initHub()
    {
        let self = this;
        var signalrBaseUri = "https://localhost:44301/hubs/sample";

       this.hubConnection = new HubConnection(signalrBaseUri);

        this.hubConnection
        .start() // <- Failure here
        .then(() =>{
             self.hubConnection.invoke("Subscribe", this.groupName);
        })
        .catch(err => { console.log("INVOKE FAILED");console.error(err);});             
    })
    .catch(err => 
    {
        console.log('Error while establishing connection :(');
        console.log(err);
    });

    this.hubConnection.on('receiveMessage', (receivedMessage: SampleMessage) => {
        console.log("Message received : ");
        console.log(sampleMessage);
    });
}
}

任何帮助都将不胜感激


共2个答案

匿名用户

问题是,npm包已从@aspnet/signalr-client重命名为@aspnet/signalr。更改为这个新包并修复导入对我有用(至少摆脱了OPTIONS调用错误。

从'@aspnet/signalr'导入{HubConnection}

匿名用户

Pawel的评论让我朝着正确的方向解决了这个问题。

以下是一些讨论OPTIONS请求和替换它的决定的帖子。

https://github.com/aspnet/SignalR/issues/1107

https://github.com/aspnet/SignalR/issues/1111

然而,我的问题似乎已经通过用nuget包版本纠正一个错误得到了解决。

我意识到我的SignalR Hub项目引用了Microsoft. AspNetCore.SignalR(2.1.0-preview1-27884)的预览版。我将其更改为使用1.0.0-alpha2-Final。我还清除了我的node_modules目录,验证了我的npm缓存,并重新安装了所有内容

npm cache verify
npm install

现在一切正常。