Redis backplane with SignalR and AspNetBoilerPlate - asp.net-core

I have implemented SignalR with AspNetBoilerPlate with .Net core 3.1. We have an azure server with multiple instances. So I am trying to implement Redis.
I followed this link and installed Microsoft.AspNetCore.SignalR.StackExchangeRedis package.
Added following code in startups ConfigureServices()
services.AddSignalR().AddStackExchangeRedis("localhost:6379", options => {
options.Configuration.ChannelPrefix = "dhub";
});
I go to cmd and write redis-cli and added following command:
> PSUBSCRIBE d*
> PSUBSCRIBE dhub*
No message gets displayed when I send a message through SendNotificationsAsync() of IRealTimeNotifier of ASPNetBoilerPlate.
But it is not working.

Related

How to host .NET 6 Web API as Windows Service?

I have .NET 6 Web API and want to host as a windows service on my own virtual server. I read many questions and answers and applied them, but I still get an error when starting the application (error 1053: the service did not respond to the start or control request in a timely fashion) Also I couldn't find a detailed error description when I looked in the event log.
I'm using Microsoft.Extensions.Hosting.WindowsServices package and Program.cs is below
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
Args = args,
ContentRootPath = WindowsServiceHelpers.IsWindowsService() ? AppContext.BaseDirectory : default
});
builder.Host.UseWindowsService();
var connectionString = builder.Configuration.GetConnectionString("MongoDB");
...
I'm developing on MAC so publishing my codes with the following command
dotnet publish --runtime win7-x64 --self-contained --configuration Release
I'm creating my windows service like this
sc create "MySvc" binPath= "C:\Users\Administrator\Desktop\test\MySvc.exe"
Where am I doing wrong or how can I find the error?

Can I use Serilog Enrichers to get Machine / Hostname Name?

I have been searching on how I can get client hostname/computer name using ASP.Net Core MVC using .net 3.1 .
There are threads here on SO but most of them are not working on intranet apps (clients used VPN to connect to network).
I've seen some suggestion on some thread to use Serilog Enrichment.
My question is how can I use this one? Can I really get the Machine Name (value passed on the application to db) using this plugin?
You should be able to. Try this configuration on:
"Using": [ "Serilog.Enrichers.ClientInfo" ],
"Enrich": [ "WithMachineName", "WithClientIp" ]
You'd need the nuget package Serilog.Enrichers.ClientInfo
UPDATE:
You can also write the logs directly to the database using one of the database sinks. For eg, to write to a MSSQL database, you could use the sink here: Serilog.Sinks.MSSqlServer
This gives you control over what properties and you want to log including Client Info

Redis Connection with Cache is failing

Goal:
Enable to use Redis Cache in a asp.net core sample project from this webpage.
(https://dotnetcoretutorials.com/2017/01/06/using-redis-cache-net-core/)
Problem:
When I follow the instruction and use the source code I get a error message saying
"StackExchange.Redis.RedisConnectionException: 'It was not possible to connect to the redis server(s); to create a disconnected multiplexer, disable AbortOnConnectFail. SocketFailure on PING"
Q1.
What part of the website or the source code am I missing?
Q2.
Do you need an account at Redis in order use an unique IP number that should be applied at "option.Configuration = "127.0.0.1";"?
Info:
Using VS 2017 asp.net core mvc 2.2
Thank you!

Self hosted ASP.NET Core app does not work with SSL

I have created an sample ASP.NET Core application using command line:
dotnet new
and tried to run it utilizing command:
dotnet run
But, when opening the URL in the browser, it complains that the SSL is invalid.
I can run the program using Visual Studio full version without any problem, but can not run it using command line:
dotnet run
Seems that Kestrel which is run by command dotnet run needs extra configurations.
Now, the question is how can I run my ASP.NET Core application over https using Kestrel?
I'm on Dot Net Core 2.2 and Visual Studio 2019
It seems that IIS Express does some works behinds the scense which include configuration of a SSL certification. So, I concentrated on ASP.NET Core's internal hosting component which is Kestrel.
Firstly, I created and SSL certificate using PowerShell by help of this command:
New-SelfSignedCertificate -DnsName localhost -CertStoreLocation "cert:\LocalMachine\My"
This command creats a certification which I can see it through management console / certificates program. For more information see this. After creating a new certificate, you are required to export it as a pfx file. To do so in the management console use follwing steps:
Add Certificates snap-in to the management console.
Navigate through Personal\Certifcates
Right click on your newly created certificate and, from "all tasks", select "export"
Select the option which says: "Yes, export the private key"
You'll see that the pfx is automatically selected. Click "Next"
Set a password and saving location.
You are done with creating a pfx file
Next step is to configure ASP.NET Core to use the pfx file which is described here:
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup < Startup > ()
.UseKestrel((hostingContext, options) =>
{
if (hostingContext.HostingEnvironment.IsDevelopment) {
options.Listen(IPAddress.Loopback, 9001);
options.Listen(IPAddress.Loopback, 9002, listenOptions => {
listenOptions.UseHttps("certificate.pfx", "password");
});
}
})
.Build();
Now, the problem should be resolved. If not, try the certificate which is created by the IIS Express. This is also available in management console.
When you run ASP.NET Core in development mode using dotnet run, or from within Visual Studio, then there is already built-in support for a development certificate that allows you to develop right away with HTTPS support.
The development certificate is built-in with the .NET Core SDK and usually it should set it self up when you run the .NET Core SDK for the first time. If that did not work or if you lost the development certificate for some reason, then you can always install it later using the following command:
dotnet dev-certs https --trust
This part is also described in the “Enforcing SSL” chapter of the official documentation.
Note that the development certicate only applies during development and of course you will need to set up a proper certificate for production later. How that works of course depends on how you are going to host the application later. The different options and how to configure SSL is covered in the hosting chapter.

.net core ubuntu change port when using service

I'm trying to test running .net core on a different port when using the service.
However, I keep on getting an error with a status code 140.
This occurs in my two attempts :
1) adding --server.urls=http://0.0.0.0:555 in the service file
ExecStart=/usr/bin/dotnet /dotnetcore/published/TestingWebApi.dll
2) adding UseUrls("http://0.0.0.0:555") in the Program.cs
When I run it manually using the code
dotnet run --server.urls=http:0.0.0.0:555 it works.
Anybody has managed to get it using the service?
use the following
ExecStart=/usr/bin/dotnet /dotnetcore/published/TestingWebApi.dll --urls http://localhost:5002