Communicate web app and api project using SignalR & StackExchangeRedis - c#

I'm working with a project built with ASP.NET Core 2.2. The main solution contains multiple projects, which includes API, web and other class libraries.
We've used SignalR to displaying shared messages/notifications between the API project and the web project. For example, adding a new employee record from an API should call SignalR Hub and all the web client should received the notification.
To make it communicate, we have used Radis with SignalR
// SignalR Configuration
services.AddSignalR(option =>
{
option.EnableDetailedErrors = true;
option.KeepAliveInterval = TimeSpan.FromSeconds(3);
}).AddStackExchangeRedis(options =>
{
options.Configuration.ChannelPrefix = "TestChannel";
options.Configuration.EndPoints.Add("127.0.0.1", 6379);
options.Configuration.ClientName = "ClientNameSignalR";
options.Configuration.AllowAdmin = true;
});
When I try to get client list in Radis,
Now, how to sending and receiving messages from web app and API?

The code you share is used to set up a Redis backplane for scaling out your ASP.NET Core SignalR app.
how to sending and receiving messages from web app and API?
To achieve above requirement, you can do:
For your API project and Hub server, you can inject an instance of IHubContext in your API controller, then you can access to an instance of IHubContext in controller action and push message/notification to client(s).
Access a SignalR IHubContext to send notifications to clients from outside a hub
For your web app, you can built and implement SignalR JavaScript client functionality to receive notifications.
SignalR JavaScript client

Related

How to get Server IP or HostName in .NET Core 5.0 API

I have developed an Angular 8 application that is hosted on Server (i.e 123.234.456.XYZ). This UI application is consuming the.DotNet Core API that is hosted on a different server(i.e 123.234.456.PQR).
There is a middleware in my Core API. Which logs the client IP by using the below code.
IPAddress remoteIP = context.Connection.RemoteIpAddress;
Is there any way to get the application server IP (i.e 123.234.456.XYZ) in my middleware?
Tried the below code: But this code is returnning my API hostname
IServerVariablesFeature serverVariablesFeature = context.Features.Get<IServerVariablesFeature>();
var hosteName = serverVariablesFeature["REMOTE_HOST"];
Log.Debug($"Request from REMOTE_HOST: {hosteName}");
Below code returned No such host is known
string HostName= Dns.GetHostEntry(remoteIP).HostName;
Log.Debug($"Request from HostName: {HostName}");
Technically, your Angular application runs in the browser of the user, so the computer of the user accesses the API directly after it has downloaded the Angular application from the server XYZ. So by default there is no contact from the application server XYZ to the API server PQR.
You can solve this on the infrastructure level with a reverse proxy that runs on the application server, for instance under /api. The application then does not access the API directy, but uses the /api URI to connect to the application server that then routes the requests to the API server. If the middleware does need additional data, you can add headers in the reverse proxy that you use in the middleware on the API server to identify the application server.
As a simpler custom approach you can also add headers to the requests to the API in the Angular application that contain the origin of the Angular application (the URL under which the Angular application is running). The middleware on the API server can then use these headers to identity the instance of the Angular application that sent the request.

Proxy ASP NET Core Web API requests to another API

I am implementing a REST API which has to do the following:
By default, all requests should be proxied to another API.
When i implement a new controller/endpoint in the new .NET Core Web API,
i want to be able to specify that requests to those endpoints should not be proxied to the other API.
Any tips/suggestions are appreciated as I'm very new to C# and .NET
You could look into a web server like Nginx to proxy your requests to another endpoint

How to map signalr user to connection (Web Api)?

Previously I used Context.User.Identity.Name to map connection,
but when I moved Hubs to Web Api it stopped working
(SignalR client and all Identity logic is in asp.net mvc app).
Any ideas?

Using RabbitMQ at ASP.NET MVC

Clients use iOS app + RabbitMQ to send messages on the server.
Sellers use ASP.NET MVC web site.
How can I use RabbitMQ for ASP.NET MVC to get messages from server?
If I subscribe to RabbitMQ channel as consumer inside controller, this doesn't work because controller returns View and EventHanlder doesn't fire.
You can create subscribing and recieving message from rabbitmq in global.asax or startup or program (if this selfhost) or another place which can be initialized on start. ASP.NET mvc in this case doesn't matter.

windows authentication in web api 2

I create angular js application for client side and for server side I use asp.net web api2.
Now I want to integrate windows authentication for this application.
How to integrate Windows authentication for this scenario.
I not want simple iis based windows authentication in web api 2.
If you are opting for OWIN hosted Web API,
using (WebApp.Start(url, (app) =>
{
HttpListener listener = (HttpListener)appBuilder.Properties["System.Net.HttpListener"];
listener.AuthenticationSchemes = AuthenticationSchemes.IntegratedWindowsAuthentication;
...
}

Categories