I'm new using SignalR and I'm trying to setup a server on a .net console that I will execute in a Win server 2012. I spend like 3 days on YouTube/Google etc but can't handle it when I execute the server in different pc's. In my pc I can connect client and server succesfully.
My goal is to get real-time updates with MS SQL, Entity-framework 7 and different clients in Winforms in net core 7, the server (does not care if is .net Framework or .net core).
I have this code in the server-side:
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Hosting;
using serverPTP.Hubs;
using Microsoft.Extensions.Logging;
class Program{
static void Main(string[] args)
{
try
{
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
EnvironmentName = Environments.Production
});
builder.Logging.ClearProviders();
builder.Logging.AddConsole();
builder.Services.AddSignalR();
var app = builder.Build();
app.Urls.Add("http://localhost:some port");
app.MapHub<HubPTP>("/Hubs/HubPTP");
app.UseHsts();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
Console.WriteLine("3 seg for RUN:");
Thread.Sleep(3000);
app.Run();
Console.WriteLine("Continue..");
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.WriteLine(ex.StackTrace);
if(ex.InnerException != null)
{
Console.WriteLine(ex.InnerException.ToString());
}
}
}
}
If I run this app works, if I install this app in my pc works but in my windows server 2012 does not works. I have no errors messages, just inmediatelly after app.Run() closes the console... What is wrong in the code?
I'm using the NuGet package Microsoft.AspNetCore.SignalR.Client 7.0.3
UPDATE
Now I also I'm trying to get it in Web enviorment.
Program.cs
builder.Services.AddCors(options => {
options.AddPolicy("AllowOrigin", builder =>
builder.WithOrigins(new[] { "http://localhost:80", "https://localhost:80" })
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials()
.SetIsOriginAllowed((host) => true)); //for signalr cors
});
builder.WebHost.CaptureStartupErrors(true);
builder.Logging.ClearProviders();
builder.Logging.AddConsole();
builder.Services.AddControllers();
builder.Services.AddSignalR(hubOptions =>
{
hubOptions.EnableDetailedErrors = true;
hubOptions.KeepAliveInterval = TimeSpan.FromMinutes(1);
});
// CORS Policies
//builder.Services.AddCors(p => p.AddPolicy("corsapp", builder =>
//{
// builder.WithOrigins("*").AllowAnyMethod().AllowAnyHeader();
//}));
var app = builder.Build();
app.MapHub<HubPTP>("/HubPTP", options =>
{
Console.WriteLine($"Authorization data items: {options.AuthorizationData.Count}");
});
// Enable Cors
app.UseCors();
app.UseRouting();
app.Run();
launchSettings.json
{
"profiles": {
"http": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production"
},
"dotnetRunMessages": true,
"applicationUrl": "http://172.28.8.2:80"
},
"https": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production"
},
"dotnetRunMessages": true,
"applicationUrl": "https://172.168.8.2:80",
"remoteDebugEnabled": true
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production"
}
}
},
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://172.28.8.2:8006/Hubs/HubPTP",
"sslPort": 8006
}
}
}
I'm unable to change the port, in Visual Studio I can change it and works fine if I launch the app but the port does not change if I install the App. It is always in port 5000 outside Visual Studio.
EDIT- WORKING Finally after the @Blindy answer my signalr server works and I can connect to him from my own pc.
var app = builder.Build();
app.Urls.Add($"http://*:5000");
app.MapHub<HubPTP>("/Hubs/HubPTP");
app.UseStaticFiles();
app.UseRouting();
app.Run();
With this method in my case didn't works: app.UseHsts();
so I deleted this line.
the server is in one pc with windows server 2012 and I'm trying to connect from my pc win 11
Your start URI for the socket is wrong, http://localhost:5000
means it will only bind to sockets on your local machine. That's why you see it on the launch screen, it's very important to set it correctly.
The URI you want is http://*:5000
, or whatever port you want to use. This means that it will bind to any incoming socket.