You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Microsoft SignalR自托管示例报错求助:复制代码运行异常

Hey there, let's troubleshoot your SignalR self-hosting demo issue. First thing I notice—your Startup.Configuration method is cut off (app.Us...)—that's almost certainly contributing to the error you're seeing. Let's walk through the fixes step by step.

1. Complete the Startup Class Configuration

Your Startup class needs to properly enable CORS (critical for cross-origin connections) and map SignalR's routes. Here's the full, corrected code including a sample hub to test with:

using System;
using Microsoft.AspNet.SignalR;
using Microsoft.Owin.Hosting;
using Owin;
using Microsoft.Owin.Cors;

namespace SignalRSelfHost
{
    class Program
    {
        static void Main(string[] args)
        {
            string url = "http://localhost:8080";
            using (WebApp.Start(url))
            {
                Console.WriteLine("Server running on {0}", url);
                Console.ReadLine();
            }
        }
    }

    class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // Enable CORS first - this MUST come before SignalR mapping
            app.UseCors(CorsOptions.AllowAll);
            
            // Map SignalR to its default route (/signalr)
            app.MapSignalR();
        }
    }

    // Add a sample hub to validate functionality
    public class ChatHub : Hub
    {
        public void SendMessage(string name, string message)
        {
            // Broadcast message to all connected clients
            Clients.All.receiveMessage(name, message);
        }
    }
}

2. Ensure Required NuGet Packages Are Installed

Most runtime errors here come from missing dependencies. Install these via the Package Manager Console:

Install-Package Microsoft.AspNet.SignalR.SelfHost
Install-Package Microsoft.Owin.Cors
Install-Package Microsoft.Owin.Hosting

3. Fix Common Error Scenarios

  • Port Conflict: If you get a "port already in use" error, switch to a different port (e.g., http://localhost:8081) or run netstat -ano | findstr :8080 in Command Prompt to locate and terminate the process using the port.
  • Cross-Origin Issues: If your frontend can't connect, double-check that app.UseCors(CorsOptions.AllowAll) runs before app.MapSignalR()—order matters here.
  • Namespace Mix-Up: Make sure you're using Microsoft.AspNet.SignalR (not Microsoft.AspNetCore.SignalR)—this is the full .NET Framework version of SignalR, not the Core variant.

4. Validate the Server

Once you've made these changes, start the server. To confirm SignalR is working, navigate to http://localhost:8080/signalr/hubs in your browser. You should see auto-generated JavaScript proxy code—this means the server is configured correctly.

内容的提问来源于stack exchange,提问作者FirstByte

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:46:33