Receiving two messages from .net core SignalR and react - c#

I am trying to set up a WebSocket connection using the .net SignalR and React app as a client to be able to send private messages.
Here is my code on the client side:
const setUpSignalRConnection = async () => {
const connection = new HubConnectionBuilder()
.withUrl("http://localhost:5000/messaginghub")
.build();
setConnection(connection);
connection.on("ReceiveMessage", (message: string) => {
console.log("Recieved Message", message);
setChatMessages((oldArray) => [...oldArray, message]);
});
try {
await connection.start();
} catch (err) {
console.log("Errors", err);
}
return connection;
};
const SendMessage = async () => {
if (connection) {
try {
console.log("sending message");
await connection.send("SendPrivateMessage", user.user.email, message);
} catch (e) {
console.log("Errors sending message", e);
}
} else {
alert("No connection to server yet.");
}
};
and my server side code
public async Task SendPrivateMessage(string userEmail, string message)
{
var RecivingMessageUser = _unitOfWork.UserRepository.GetByEmail(userEmail);
var currUserEmail = Context.User.FindFirstValue(ClaimTypes.NameIdentifier);
var sender = _unitOfWork.UserRepository.GetByEmail(currUserEmail);
var newMessage = new MessagesDto
{
FromId = sender.UserId,
ToId = RecivingMessageUser.UserId,
MessageBody = message,
SentAt = DateTime.UtcNow,
};
await Clients.Group(userEmail).SendAsync("ReceiveMessage", message);
_unitOfWork.MessagingRepository.Insert(_mapper.Map<MessagesDto, Messages>(newMessage));
_unitOfWork.SaveChanges();
}
public override Task OnConnectedAsync()
{
var groupName = Context.User.FindFirstValue(ClaimTypes.NameIdentifier);
Groups.AddToGroupAsync(Context.ConnectionId, groupName);
return base.OnConnectedAsync();
}
public override Task OnDisconnectedAsync(Exception ex)
{
Groups.RemoveFromGroupAsync(Context.ConnectionId, Context.User.FindFirstValue(ClaimTypes.NameIdentifier));
return base.OnDisconnectedAsync(ex);
}
With console.logs I see that I am sending a message once and the message is stored in DB once but somehow on the other end, I am getting two received messages.
I am testing it on my local machine in two separate browsers.
What am I doing wrong?

Which method on your back-end is calling twice?
You are telling your message saved in to the DB once so it shouldn't be the SendPrivateMessage method which is calling towice.

Related

Why I can't get the HubCallerContext user with web sockets connections?

when I use the WebSockets I Can't get the userId on the server
Client code:
HubConnection = new HubConnectionBuilder().WithUrl(Config.BaseUrl + ApplicationConstants.SignalR.HubUrl, options =>
{
options.Transports = Microsoft.AspNetCore.Http.Connections.HttpTransportType.WebSockets;
options.Headers.Add("Authorization", $"Bearer {NPCompletApp.Token}");
options.AccessTokenProvider = async () => await Task.FromResult(NPCompletApp.Token);
}).WithAutomaticReconnect().Build();
Server code:
public override async Task<Task> OnConnectedAsync()
{
ConnectedUserModel connectedUserModel = new ConnectedUserModel()
{
ConnectionId = Context.ConnectionId, UserId = _userManager.GetUserId(Context.User)
};
UserHandler.connectedUsers.Add(connectedUserModel);
var temp = new List<string>();
foreach(ConnectedUserModel connectedUser in UserHandler.connectedUsers)
{
if (!String.IsNullOrEmpty(connectedUserModel.UserId) && temp.Find(x=> x == connectedUserModel.UserId) == null)
{
temp.Add(connectedUserModel.UserId);
await OnConnectAsync(connectedUserModel.UserId);
}
}
return base.OnConnectedAsync();
}
The good thing is that I can catch if the user Disconnected, but still can't know who is the user.
server code (On disconnecting):
public override async Task<Task> OnDisconnectedAsync(Exception? exception)
{
var connection = UserHandler.connectedUsers.Find(x => x.ConnectionId == Context.ConnectionId);
await OnDisconnectAsync(connection.UserId);
UserHandler.connectedUsers.Remove(connection);
return base.OnDisconnectedAsync(exception);
}
On the other hand When I use LongPolling I can get the userId but I can't catch him when disconnecting
client code:
HubConnection = new HubConnectionBuilder().WithUrl(Config.BaseUrl + ApplicationConstants.SignalR.HubUrl, options =>
{
options.Transports = Microsoft.AspNetCore.Http.Connections.HttpTransportType.LongPolling;
options.Headers.Add("Authorization", $"Bearer {NPCompletApp.Token}");
options.AccessTokenProvider = async () => await Task.FromResult(NPCompletApp.Token);
}).WithAutomaticReconnect().Build();
What should I do ? I want to know who is the user in my context & to catch him when he diconnected.
On your server your have to configure the middleware.
This is taken from a working project...
Server :
services.TryAddEnumerable(
ServiceDescriptor.Singleton<IPostConfigureOptions<JwtBearerOptions>,
ConfigureJwtBearerOptions>());
ConfigureJwtBearerOptions.cs
public class ConfigureJwtBearerOptions : IPostConfigureOptions<JwtBearerOptions>
{
private readonly ChatConfigurations config;
public ConfigureJwtBearerOptions(ChatConfigurations config)
{
this.config = config;
}
public void PostConfigure(string name, JwtBearerOptions options)
{
var originalOnMessageReceived = options.Events.OnMessageReceived;
options.Events.OnMessageReceived = async context =>
{
await originalOnMessageReceived(context);
if (string.IsNullOrEmpty(context.Token)) {
var accessToken = context.Request.Query["access_token"];
var requestPath = context.HttpContext.Request.Path;
var endPoint = $"/{config.EndPoint}";
if (!string.IsNullOrEmpty(accessToken) &&
requestPath.StartsWithSegments(endPoint)) {
context.Token = accessToken;
}
}
};
}
}
In your client you also have to configure for tokens.
public async ValueTask InitialiseAsync()
{
IsInitialised = false;
hubConnection = CreateHubConnection();
hubConnection.On<string, string>("ReceiveMessage", ReceiveMessageAsync);
....
await hubConnection.StartAsync();
await hubConnection.SendAsync("JoinGroup", roomName);
IsInitialised = true;
}
private HubConnection CreateHubConnection()
{
var endPoint = $"/{config.EndPoint}";
var hubConnection = new HubConnectionBuilder()
.WithUrl(navigationManager.ToAbsoluteUri(endPoint), options =>
{
options.AccessTokenProvider = async () =>
{
var accessTokenResult = await accessTokenProvider.RequestAccessToken();
accessTokenResult.TryGetToken(out var accessToken);
var token = accessToken.Value;
return token;
};
})
.WithAutomaticReconnect()
.Build();
return hubConnection;
}
My Hubs OnConnectedAsync
public async override Task OnConnectedAsync()
{
logger.LogDebug("Hub Connection");
await chatService.RegisterConnectionAsync(Context.ConnectionId, Context.UserIdentifier);
await base.OnConnectedAsync();
}
Note: I am persisting connections to a database.
My Hubs OnDisconnectedAsync
public async override Task OnDisconnectedAsync(Exception exception)
{
logger.LogDebug("Hub Disconnect");
await chatService.RegisterDisconnectAsync(Context.ConnectionId);
await base.OnDisconnectedAsync(exception);
}
Some debug logs:
dbug: OrakTech.ChatServer.Brokers.Loggings.LoggingBroker[0]
Hub Connection
dbug: OrakTech.ChatServer.Brokers.Loggings.LoggingBroker[0]
Hub Connected (r7SJaAMEGs7eovH810H5Xg, c8f81673-d8b3-4e46-80f6-a83b671e6ff1)
dbug: OrakTech.ChatServer.Brokers.Loggings.LoggingBroker[0]
Join Group (TestRoom : r7SJaAMEGs7eovH810H5Xg)
dbug: OrakTech.ChatServer.Brokers.Loggings.LoggingBroker[0]
Hub Disconnect
dbug: OrakTech.ChatServer.Brokers.Loggings.LoggingBroker[0]
Hub Disconnect (r7SJaAMEGs7eovH810H5Xg)

No customer received the message

I am making a notification microservice for users. It is impossible to use the SignalR built into MassTransit due to the built-in microservice architecture.
After accepting messages from the queue RabbitMq, it is called StartAsync:
public class MassTransitManager : IMassTransitManager
{
public MassTransitManager(Func<string, Task<string>> processDelegateAsync)
{
ProcessDelegateAsync = processDelegateAsync;
}
public Func<string, Task<string>> ProcessDelegateAsync { get; set; }
public async Task<string> StartAsync(string requestString)
{
string response;
try
{
response = await ProcessMessageAsync(requestString);
}
catch (Exception exception)
{
response = exception.ToString();
}
return response;
}
private async Task<string> ProcessMessageAsync(string requestString)
{
if (ProcessDelegateAsync == null)
throw new InvalidOperationException($"Not found delegate {nameof(ProcessDelegateAsync)}.");
var result = await ProcessDelegateAsync(requestString);
return result;
}
}
MassTransitManager is in Class Library. Delegate created in Startup:
services.AddSingleton<IMassTransitManager, MassTransitManager>(provider =>
{
return new MassTransitManager(async serializedMessage =>
{
var hubContext = provider.GetRequiredService<IHubContext<NoticeHub>>();
await hubContext.Clients.All.SendAsync("SendResponse", "Hi all.");
return "Hi man";
});
});
But no client receives this message. What can be the ways to solve this problem?

Read server streaming into client c#

I'm doing an application using server streaming.
The problem is the client doesn't read the data from the server streaming.
This is my proto service:
service UserService {
rpc GetData(Id) returns (stream DataResponse) {}
}
message Id {
int32 id = 1;
}
message DataResponse {
bytes data = 1;
}
c# server is like this:
public override async Task GetData(Id request, IServerStreamWriter<DataResponse> response, ServerCallContext context)
{
var user = {} // get user
foreach (var d in user.Data)
{
await response.WriteAsync(new DataResponse { Data = d });
}
}
And it works because I have a NodeJS client where I can call the server and works perfectly.
Client in Node is
let call = client.getData({id:1})
call.on('data', function (response) {
// do things
})
call.on('end', function () {
// do things
})
And c# client is:
AsyncServerStreamingCall<DataResponse> response = client.GetData(new Id{Id_ = 1});
while(await response.ResponseStream.MoveNext())
{
Console.WriteLine("Into while loop"); // <-- This is never executed
DataResponse current = response.ResponseStream.Current;
Console.WriteLine($"{current.Data}");
}
I've also added a try/catch and it doesn't output anything so it seems MoveNext() is always false.
What is the problem here? Why NodeJS client works and c# client can't read the stream? Have I missed something?
Here is full client.cs class:
class Program
{
const int Port = 50051;
static void Main(string[] args)
{
try
{
Channel channel = new Channel("127.0.0.1:50051", ChannelCredentials.Insecure);
var client = new UserService.UserServiceClient(channel);
GetDataStreaming(client);
}
catch (RpcException ex)
{
Console.WriteLine($"Error: {{Code: {ex.StatusCode}, Status: {ex.Status.Detail}}}");
}
}
private static async void GetDataStreaming(UserService.UserServiceClient client)
{
AsyncServerStreamingCall<DataResponse> response = client.GetData(new Id { Id_ = 1 });
while (await response.ResponseStream.MoveNext())
{
Console.WriteLine("Into while loop");
DataResponse current = response.ResponseStream.Current;
Console.WriteLine($"{current.Data.ToStringUtf8()}");
}
}
}
The issue is that your client has ended before the client receive the response. When you call GetDataStreaming(client) in Main it doesn't wait and finishes.
To fix the issue change async void GetDataStreaming to async Task GetDataStreaming.
private static async Task GetDataStreaming(UserService.UserServiceClient client)
{
AsyncServerStreamingCall<DataResponse> response = client.GetData(new Id { Id_ = 1 });
while (await response.ResponseStream.MoveNext())
{
Console.WriteLine("Into while loop");
DataResponse current = response.ResponseStream.Current;
Console.WriteLine($"{current.Data.ToStringUtf8()}");
}
}
Change static void Main to static async Task Main and you should also call channel.ShutdownAsync method at the end.
static async Task Main(string[] args)
{
try
{
Channel channel = new Channel("127.0.0.1:50051", ChannelCredentials.Insecure);
var client = new UserService.UserServiceClient(channel);
await GetDataStreaming(client);
await channel.ShutdownAsync();
}
catch (RpcException ex)
{
Console.WriteLine($"Error: {{Code: {ex.StatusCode}, Status: {ex.Status.Detail}}}");
}
}
Another option is to change async void GetDataStreaming to async Task GetDataStreaming and in Main method wait until Task complete.
static void Main(string[] args)
{
try
{
Channel channel = new Channel("127.0.0.1:50051", ChannelCredentials.Insecure);
var client = new UserService.UserServiceClient(channel);
var task = GetDataStreaming(client);
task.Wait();
}
catch (RpcException ex)
{
Console.WriteLine($"Error: {{Code: {ex.StatusCode}, Status: {ex.Status.Detail}}}");
}
}

Controller did not call the SignalR

I read a lot here on stackoverflow before post this, and the only that help, but not really, was
SignalR call from controller
https://learn.microsoft.com/en-us/aspnet/core/tutorials/signalr?view=aspnetcore-2.2&tabs=visual-studio
I thought that that error they say was my error, but didnt work. So When the client send a message, this should trigger the hub, but didnt... At least did show up
My controller is like...
//Constructor
private readonly IHubContext<ChatHub> chatHub;
public UserController(IHubContext<ChatHub> hubContext)
{
this.chatHub = hubContext;
}
//Method
[HttpPost]
public async Task<ActionResult> Message(Message message)
{
await chatHub.Clients.All.SendAsync("ReceiveMessage", message.emisor, message.Text);
}
Chat.js
"use strict";
var connection = new signalR.HubConnectionBuilder().withUrl("/chatHub").build();
//Disable send button until connection is established
document.getElementById("sendButton").disabled = true;
connection.on("ReceiveMessage", function (user, message) {
var msg = message.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
var encodedMsg = user + " says " + msg;
var li = document.createElement("li");
li.textContent = encodedMsg;
document.getElementById("messagesList").appendChild(li);
});
connection.start().then(function () {
document.getElementById("sendButton").disabled = false;
}).catch(function (err) {
return console.error(err.toString());
});
document.getElementById("sendButton").addEventListener("click", function (event) {
var user = document.getElementById("userInput").value;
var message = document.getElementById("messageInput").value;
connection.invoke("SendMessage", user, message).catch(function (err) {
return console.error(err.toString());
});
event.preventDefault();
});
ChatHub class
public class ChatHub : Hub
{
public async Task SendMessage(string name, string message)
{
await Clients.All.SendAsync("ReceiveMessage", name, message);
}
}
So, this method on chathub, did not have any reference, If I debug, didnt call this method
You only call hub methods from the client. And you call client methods from the server.
IHubContext is on the server and all it does is send to clients. If you want to "call a hub method" from the server, then you need to refactor your code to have a common class that both the hub method calls and your controller calls.

MVC, ASP.NET Identity, EmailService and Async calls w/Angularjs

After reviewing and trying many of the suggestions surrounding the error message:
"An asynchronous module or handler completed while an asynchronous
operation was still pending."
I found myself in the situation where even though the call to the MVC accountController actually EXECUTED the desired code (an email was sent to the right place with the right content) and a Try/Catch in the controller method would not 'catch' the error, the AngularJS factory that was initiating the call would receive a server error "page".
Factory:(AngularJS)
InitiateResetRequest: function (email) {
var deferredObject = $q.defer();
$http.post(
'/Account/InitiateResetPassword', { email: email }
)
.success(function (data) {
deferredObject.resolve(data);
})
.error(function (data) {
//This is a stop-gap solution that needs to be fixed..!
if (data.indexOf("An asynchronous module or handler completed while an asynchronous operation was still pending.") > 0) {
deferredObject.resolve(true);
} else {
deferredObject.resolve(false);
}
});
return deferredObject.promise;
}
MVC Controller (C#):
[HttpPost]
[AllowAnonymous]
public async Task<int> InitiateResetPassword(string email)
{
try
{
_identityRepository = new IdentityRepository(UserManager);
string callbackUrl = Request.Url.AbsoluteUri.Replace(Request.Url.AbsolutePath, "/account/reset?id=");
await _identityRepository.InitiatePasswordReset(email, callbackUrl);
return 0;
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
return 1;
}
}
Identity Repository/InitiatePasswordReset:
public async Task InitiatePasswordReset(string email, string callbackUrl)
{
try
{
var u = await _applicationUserManager.FindByEmailAsync(email);
string passwordResetToken = await GetResetToken(u);
callbackUrl = callbackUrl + HttpUtility.UrlEncode(passwordResetToken);
await _applicationUserManager.SendEmailAsync(u.Id, RESET_SUBJECT, string.Format(RESET_BODY, u.FirstName, u.LastName, callbackUrl));
}
catch(Exception ex)
{ //another vain attempt to catch the exception...
Console.WriteLine(ex.ToString());
throw ex;
}
}
The EmailService injected into the ASP.NET Identity "ApplicationUserManager"
public class EmailService : IIdentityMessageService
{
XYZMailer xyzMailer;
public EmailService()
{
xyzMailer = XYZMailer.getCMRMailer();
}
public async Task SendAsync(IdentityMessage message)
{
//original code as posted:
//await Task.FromResult(xyzMailer.SendMailAsync(message));
//solution from #sirrocco-
await xyzMailer.SendMailAsync(message);
}
}
and finally...the XYZMailer class
class XYZMailer
{
#region"Constants"
private const string SMTP_SERVER = "XYZEXCHANGE.XYZ.local";
private const string NO_REPLY = "noReply#XYZCorp.com";
private const string USER_NAME = "noreply";
private const string PASSWORD = "theMagicP#55word"; //NO, that is not really the password :)
private const int SMTP_PORT = 587;
private const SmtpDeliveryMethod SMTP_DELIVERY_METHOD = SmtpDeliveryMethod.Network;
#endregion//Constants
internal XYZMailer()
{
//default c'tor
}
private static XYZMailer _XYZMailer = null;
public static XYZMailer getXYZMailer()
{
if (_XYZMailer == null)
{
_XYZMailer = new XYZMailer();
}
return _XYZMailer;
}
public async Task<int> SendMailAsync(IdentityMessage message)
{
#if DEBUG
message.Body += "<br/><br/>DEBUG Send To: " + message.Destination;
message.Destination = "me#XYZCorp.com";
#endif
// Create the message:
var mail =
new MailMessage(NO_REPLY, message.Destination)
{
Subject = message.Subject,
Body = message.Body,
IsBodyHtml = true
};
// Configure the client:
using (SmtpClient client = new SmtpClient(SMTP_SERVER, SMTP_PORT)
{
DeliveryMethod = SMTP_DELIVERY_METHOD,
UseDefaultCredentials = false,
Credentials = new System.Net.NetworkCredential(USER_NAME, PASSWORD),
EnableSsl = true
})
{
// Send:
await client.SendMailAsync(mail);
}
return 0;
}
}
(note: originally the controller method was simply "public async Task InitiateResetPassword, I added the return type as an attempt to trap the error on the server. At runtime, return 0; does hit (breakpoint) the catch does not get hit and at the client")
At the moment I am simply filtering for the expected error message and telling javascript to treat it as a success. This solution has the benefit of 'actually working'... but it is not 'ideal'.
How do I prevent the error on the server?
or alternately,
How do I catch the error on the server?
You need to remove await Task.FromResult from EmailService because that makes it so the code executes synchronously instead of async.
As to why the the exception was still raised and bubbled up outside the try/catch - I suspect the Task.FromResult was the culprit here too - if you now raise an exception in SendAsync (just to test it) you should catch in the controller.

Categories

Resources