SignalR服务器在使用C# .NET 7的WinForms控制台中。

huangapple go评论52阅读模式
英文:

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(&quot;http://localhost:some port&quot;);
                app.MapHub&lt;HubPTP&gt;(&quot;/Hubs/HubPTP&quot;);
    
                app.UseHsts();
                app.UseHttpsRedirection();
                app.UseStaticFiles();
    
                app.UseRouting();
    
                Console.WriteLine(&quot;3 seg for RUN:&quot;);
                Thread.Sleep(3000);
    
                app.Run();
            
                Console.WriteLine(&quot;Continue..&quot;);
                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 =&gt; {
    options.AddPolicy(&quot;AllowOrigin&quot;, builder =&gt;
    builder.WithOrigins(new[] { &quot;http://localhost:80&quot;, &quot;https://localhost:80&quot; })
    .AllowAnyHeader()
    .AllowAnyMethod()
    .AllowCredentials()
    .SetIsOriginAllowed((host) =&gt; true)); //for signalr cors  
});

builder.WebHost.CaptureStartupErrors(true);
builder.Logging.ClearProviders();
builder.Logging.AddConsole();
builder.Services.AddControllers();

builder.Services.AddSignalR(hubOptions =&gt;
{
    hubOptions.EnableDetailedErrors = true;
    hubOptions.KeepAliveInterval = TimeSpan.FromMinutes(1);
});

// CORS Policies
//builder.Services.AddCors(p =&gt; p.AddPolicy(&quot;corsapp&quot;, builder =&gt;
//{
//    builder.WithOrigins(&quot;*&quot;).AllowAnyMethod().AllowAnyHeader();
//}));

var app = builder.Build();

app.MapHub&lt;HubPTP&gt;(&quot;/HubPTP&quot;, options =&gt;
{
    Console.WriteLine($&quot;Authorization data items: {options.AuthorizationData.Count}&quot;);
});


// Enable Cors
app.UseCors();

app.UseRouting();

app.Run();

launchSettings.json

{
  &quot;profiles&quot;: {
    &quot;http&quot;: {
      &quot;commandName&quot;: &quot;Project&quot;,
      &quot;launchBrowser&quot;: true,
      &quot;environmentVariables&quot;: {
        &quot;ASPNETCORE_ENVIRONMENT&quot;: &quot;Production&quot;
      },
      &quot;dotnetRunMessages&quot;: true,
      &quot;applicationUrl&quot;: &quot;http://172.28.8.2:80&quot;
    },
    &quot;https&quot;: {
      &quot;commandName&quot;: &quot;Project&quot;,
      &quot;launchBrowser&quot;: true,
      &quot;environmentVariables&quot;: {
        &quot;ASPNETCORE_ENVIRONMENT&quot;: &quot;Production&quot;
      },
      &quot;dotnetRunMessages&quot;: true,
      &quot;applicationUrl&quot;: &quot;https://172.168.8.2:80&quot;,
      &quot;remoteDebugEnabled&quot;: true
    },
    &quot;IIS Express&quot;: {
      &quot;commandName&quot;: &quot;IISExpress&quot;,
      &quot;launchBrowser&quot;: true,
      &quot;environmentVariables&quot;: {
        &quot;ASPNETCORE_ENVIRONMENT&quot;: &quot;Production&quot;
      }
    }
  },
  &quot;iisSettings&quot;: {
    &quot;windowsAuthentication&quot;: false,
    &quot;anonymousAuthentication&quot;: true,
    &quot;iisExpress&quot;: {
      &quot;applicationUrl&quot;: &quot;http://172.28.8.2:8006/Hubs/HubPTP&quot;,
      &quot;sslPort&quot;: 8006
    }
  }
}

SignalR服务器在使用C# .NET 7的WinForms控制台中。

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($&quot;http://*:5000&quot;);
app.MapHub&lt;HubPTP&gt;(&quot;/Hubs/HubPTP&quot;);
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.

huangapple
  • 本文由 发表于 2023年3月7日 17:58:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75660441.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定