繁体   English   中英

连接自托管SignalR服务器和客户端的最简单示例?

[英]Simplest possible sample to connect selfhosted signalR-server and client?

我正在尝试将客户端连接到自托管的SignalR服务器。

我的服务器如下所示:

    static void Main(string[] args)
    {
        string url = "http://localhost:8081/";
        var server = new Server(url);

        server.MapConnection<MyConnection>("/echo");

        server.Start();

        Console.WriteLine("Server running on {0}", url);

        Console.ReadKey();
    }

    public class MyConnection : PersistentConnection
    {
    }

这是我想出的最简单的方法。 客户看起来像这样:

    static void Main(string[] args)
    {
        SignalR.Client.Connection conn = new SignalR.Client.Connection("http://localhost:8081/echo");
        Task start = conn.Start();
        start.Wait();

        if (start.Status == TaskStatus.RanToCompletion)
        {
            Console.WriteLine("Connected");
        }

        Console.ReadKey();
    }

我上面的代码无法正常工作。 服务器启动,但是当我运行客户端代码进行连接时,出现错误:

远程服务器返回错误:(500)Internal Server Error。

服务器也给我一个错误:无法访问已处置的对象。

我忘了什么吗? 我究竟做错了什么?

编辑:我在服务器上得到的错误如下。

SignalRtest.vshost.exe错误:0:任务抛出的mscorlib.dll SignalR异常发生“ System.AggregateException”类型的第一次机会异常:System.AggregateException:发生一个或多个错误。 ---> System.ObjectDisposedException:无法访问已处置的对象。 对象名称:“ System.Net.HttpListenerResponse”。 在System.Net.HttpListenerResponse.CheckDisposed()在System.Net.HttpListenerResponse.get_OutputStream()在SignalR.Hosting.Self.Infrastructure.ResponseExtensions。<> c_ DisplayClass4.b _1(IAsyncResult ar)在System.Threading.Tasks.TaskFactory .FromAsyncCoreLogic(IAsyncResult iar,操作1 endMethod, TaskCompletionSource 1 tcs)---内部异常堆栈跟踪的结尾--- --->(内部异常#0)System.ObjectDisposedException:无法访问已处置的对象。 对象名称:“ System.Net.HttpListenerResponse”。 在System.Net.HttpListenerResponse.CheckDisposed()在System.Net.HttpListenerResponse.get_OutputStream()在SignalR.Hosting.Self.Infrastructure.ResponseExtensions。<> c_ DisplayClass4.b _1(IAsyncResult ar)在System.Threading.Tasks.TaskFactory .FromAsyncCoreLogic(IAsyncResult iar,操作1 endMethod, TaskCompletionSource 1 tcs)<-

'client.vshost.exe'(托管(v4.0.30319)):已加载'C:\\ Windows \\ assembly \\ GAC_MSIL \\ Microsoft.VisualStudio.DebuggerVisualizers \\ 10.0.0.0__b03f5f7f11d50a3a \\ Microsoft.VisualStudio.DebuggerVisualizers.dll'

似乎正在处理服务器变量,因为它看不到该函数稍后会使用它。 将GC.KeepAlive(server)添加到Main函数的末尾,以确保它不会被提前处理。 或者更好,但是在using语句中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM