英文:
SignalR server in winforms console with C# .net 7
问题
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 successfully.
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 seconds 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, it works. If I install this app on my pc, it works, but on my Windows Server 2012, it does not work. I have no error messages; just immediately 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'm also trying to get it in a Web environment.
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);
});
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 it works fine if I launch the app, but the port does not change if I install the App. It is always on port 5000 outside Visual Studio.
EDIT- WORKING
Finally, after the @Blindy answer, my SignalR server works, and I can connect to it 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, it didn't work: app.UseHsts();
so I deleted this line.
英文:
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.
答案1
得分: 2
服务器在一台装有Windows Server 2012的个人电脑上,我正试图从我的Windows 11电脑连接。
你的套接字的起始URI是错误的,http://localhost:5000
意味着它只会绑定到您本地计算机上的套接字。这就是为什么你只能在启动屏幕上看到它,设置正确非常重要。
你想要的URI是 http://*:5000
,或者你想要使用的端口。这意味着它将绑定到任何传入的套接字。
英文:
> 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论