Connect wpf to SignalR hub - c#

I try to connect my WPF application to My Hub On a ASP.NET website. But when i try to connect with this code:
HubConnection connection = new HubConnectionBuilder()
.WithUrl("https://localhost:5001/matchrchat")
.Build();
And this is the endpoint of the HUB:
app.UseEndpoints(endpoints =>
{
endpoints.MapHub<MatchrHub>("/matchrchat");
});
It gives me this Exception:
SignalR Version: 5.0.0
Can someone help me with to understand this exception or knows the solution? i would appreciate that

Related

Google cloud run unable to connect MQTT

Currently I am developing a .Net6 application with a some controllers and a minimal MQTT Server. So far I have everything working correctly on my local machine, however, when deploying to Google Cloud Run (GCR) I am unable to connect to the MQTT Server.
I noticed that the GCR container wants you to map incoming traffic to a single port (8080 in my case), however I am running MQTT on port 1883 (default) and unable to connect to it. The controllers running on port 8080 are accessible and work fine.
I need some direction toward a solution, preferably in a single container.
Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseKestrel(o =>
{
o.ListenAnyIP(1883, l => l.UseMqtt());
o.ListenAnyIP(8080);
});
...
var app = builder.Build();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints
.MapConnectionHandler<MqttConnectionHandler>("/mqtt",
httpConnectionDispatcherOptions =>
httpConnectionDispatcherOptions.WebSockets.SubProtocolSelector = protocolList => protocolList.FirstOrDefault() ?? string.Empty);
});
app.UseMqttServer(server => server.StartAsync());
app.MapControllers();
app.Run();
A possible option is to not use 2 ports.
If you use MQTT over WebSockets then your broker can share a port with the HTTP server.

How to host c# grpc service on linux and connect to in using client on windows machine

I have prepared dotnet service using grpc "template" (dotnet new grpc) that does simple CRUD operations on PostgreSQL DB. Service and database are on linux. I want to host it on linux and connect with my client from windows. Both computers are inside the same network - I can ping PC1 from PC2.
I've found that I should add hosting settings to Startup.cs file, but in this template there are no Startup.cs file. I have only Program.cs:
//Program.cs
using client_pgsql;
using client_pgsql.Services;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddGrpc();
builder.Services.AddDbContext<MovieContextClass>(
o=>o.UseNpgsql(builder.Configuration.GetConnectionString("MoviesDB"))
);
var app = builder.Build();
// Configure the HTTP request pipeline.
app.MapGrpcService<GreeterService>();
app.MapGet("/", () => "Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909");
// Configure the HTTP request pipeline.
app.MapGrpcService<MovieService>();
app.MapGet("/movie/", () => "Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909");
app.Run();
Could someone tell me what should I do to make this service accessible from another PC? And how to prepare client for this connection.

Xamarin forms and gRPC: The server returned an invalid or unrecognized response

I am trying to get a response from the gRPC service but I get the error that the server returned an invalid or unrecognized response when I make the call from a Xamarin application. However, if I use a WPF client, that use the same gRPC client library than the Xamarin application, it works as exepected, I get the response from the service.
The service code is this:
webBuilder.ConfigureKestrel(options =>
{
options.Listen(IPAddress.Any, 5001, listenOptions =>
{
listenOptions.Protocols = HttpProtocols.Http2;
listenOptions.UseHttps("server.pfx", "1111");
});
});
webBuilder.UseStartup<Startup>();
Client library:
HttpClientHandler miHandler = new HttpClientHandler();
miHandler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;
GrpcChannelOptions misOpciones = new GrpcChannelOptions() { HttpHandler = miHandler };
var miChannel = GrpcChannel.ForAddress("https://192.168.1.134:5001", misOpciones);
What I am doing wrong? At first I thought it was because from android I couldn't use http, that I need https, but now I am using https. It is true that I am ignoring any certificate from the server, could it be this the reason for that? But the error message it would be related with SSL or something like that, instead of telling that the response is not recognized.
Thanks.
EDIT: in the console aplication that hosts the service, I get this fail: HTTP/2 over TLS was not negotiated on an HTTP/2-only endpoint.
You could try to use Grpc.Core or gRPC-Web as it is mentioned in the documentation.
Calling gRPC over HTTP/2 with Grpc.Net.Client is currently not
supported on Xamarin. We are working to improve HTTP/2 support in a
future Xamarin release. Grpc.Core and gRPC-Web are viable alternatives
that work today.
And you may refer to https://stackoverflow.com/a/60362990/10768653.

Server frequently loses Azure SignalR connection

My ASP.NET Web API (target framework: .NET Framework 4.6.2) project frequently loses connection with Azure SignalR service (free tier). I have followed the example as shown in aspnet ‘chatroom’ example.
My client application is based on Angular JS. Messages are being send to the clients but after a few hours, the server connection with Azure SignalR service is lost and is not established again.
As far as I understand from the MS Azure SignalR Internals documentation:
If a server connection is disconnected for network issue,
the server connection starts reconnecting automatically.
The following error is returned back in response:
Azure SignalR Service is not connected yet, please try again later
However, this does not seem to be happening i.e. server connection with Azure SignalR service is not established again.
nuget packages:
Microsoft.AspNet.SignalR v2.4.0
Microsoft.AspNet.SignalR.Core v2.4.0
Microsoft.AspNet.SignalR.SystemWeb v2.4.0
Microsoft.Azure.SignalR.AspNet v1.0.0-preview1-10317
Microsoft.Azure.SignalR.Protocols v1.0.6
There is currently an issue with Microsoft.Azure.SignalR.AspNet v1.0.0-preview1-10317. The fix is planned to be released this week.
Have you added error handling code in your client like below-
// Define handlers for any errors
//
this.hubConnection.error((error: any) => {
// Push the error on our subject
//
this.hubConnection.start()
.done(() => {
this.startingSubject.next();
//Invoke connect method on Hub
// this.hubProxy.invoke("Connect", userId, usertype);
})
.fail((error: any) => {
this.startingSubject.error(error);
});
});
Also in case of closed connection code would be like
this.hubConnection.onclose(() => {
setTimeout(function(){
this.hubConnection.start()
.done(() => {
this.startingSubject.next();
//Invoke connect method on Hub
// this.hubProxy.invoke("Connect", userId, usertype);
})
.fail((error: any) => {
this.startingSubject.error(error);
});
},3000);
});
Hope it helps.
MV

Xamarin forms: AspNetCore.SignalR.Client v1.0.3, websocket transport not working

Background:
I would like to connect to my SignalR hub using Xamarin forms SignalR websocket transport.
My setup:
-> SignalR hub is sitting on a Windows VM. I am connecting to the VM using the iphone simulator on Mac.
-> I have created a reverse proxy rule on iis for my self hosted SignalR hub.
-> I have added an entry to my hosts file to connect to the Hub.
What is going wrong?
I am able to succesfully connect to the Hub however the issue is that the transport is always set to Server Sent Events. This should be using WebSocket transport.
What have I tried?
I have created a web client and I was able to successfully run that, initially connecting on a http request, and after negotiation the message upgraded to WebSocket.
The difference between the two connection is one is using Javascript and Websocket support on a browser, where as Xamarin forms is using a nuget package AspNetCore.SingalR.Client.
I understand on Xamarin, the connection will first try connecting using WebSocket, then Server Sent Request and if the previous two fail long polling. But as far as I understand Mono is now able to support websocket.
Xamarin client connection code (A quick write up to test that WebSocket transport works on Xamarin forms):
public class PriceStream
{
readonly HubConnection connection;
public PriceStream()
{
connection = new HubConnectionBuilder()
.WithUrl("https://dev.prices:5001/priceHub")
.Build();
connection.On<TestObject>("ReceiveMessage", (data) =>
{
var priceUpdate = new PriceUpdateMessage
{
ProductId = data.ProductId,
PriceId = data.PriceId,
Price = data.Price
};
// update price on app
// TODO
});
}
public async Task SubscribeAsync(int productId)
{
// connecting here because I want to see the transprot before sending message
await connection.StartAsync();
await connection.SendCoreAsync("SendMessage", new[] { productId });
}
}
So what am I missing that it is not able to connect using WebSocket transport?

Categories

Resources