Issue
I followed code SignalR guide for .NET CORE AngularApp
I get below error:
Failed to start the connection: Error: Unable to initialize any of the available transports
The code is there hosted on Microsoft's Github here
Below is code snippet from Startup.cs
code:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(o => o.AddPolicy("CorsPolicy", builder => {
builder
.AllowAnyMethod().AllowAnyHeader()
.WithOrigins("http://localhost:49446")
.AllowCredentials();
//.AllowAnyOrigin()
}));
services.AddSignalR();
...
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseCors("CorsPolicy");
app.UseSignalR(routes =>
{
routes.MapHub<NotifyHub>("/notifyhub");
});
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller}/{action=Index}/{id?}");
});
...
}
}
My Hub class:
public class NotifyHub:Hub<ITypedHubClient>
{
public NotifyHub()
{
}
}
Angular app.component.ts
:
import {Component} from '@angular/core';
import {HubConnection, HubConnectionBuilder, IHubProtocol} from '@aspnet/signalr';
@Component({selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css']})
export class AppComponent {
public _hubConnection : HubConnection;
msgs : Message[] = [];
constructor() {}
ngOnInit() : void {
let builder = new HubConnectionBuilder();
this._hubConnection = builder
.withUrl('/notifyhub')
.build();
this
._hubConnection
.start()
.then(() => console.log('Connection started!'))
.catch(err => console.log('Error :', err));;
this
._hubConnection
.on('BroadcastMessage', (type : string, payload : string) => {
this
.msgs
.push({severity: type, summary: payload});
});
}
}
Not sure what am I missing here? Please advise. Thank you.
Solution
Ok, so issue was mismatch of .NET Core
version and @aspnet/signalr
version.
I am using .NET core version 1.0.0-preview1-final
but @aspnet/signalr
I was using was 1.0.0
.
So, I fixed the issue by changing the @aspnet/signalr
version from 1.0.0
to 1.0.0-preview1-final
which fixed the issue. Github is updated.
Answered By - User3250
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.