繁体   English   中英

如何从Xamarin客户端连接到SignalR服务器?

[英]How to connect to SignalR server, from Xamarin client?

(**)我创建了一个托管SignalR集线器的ASP.NET Web服务器。 我遵循了教程。 有用。 然后,我创建了一个Xamarin.Android客户端(*)。 问题是我无法连接到服务器。 我认为,由于主机地址中存在“ localhost”,因此无法连接。 例外很大。

如果我的怀疑是正确的,我该如何解决? 如何使localhost看起来像普通服务器?

(*)-我也尝试过Xamarin.Forms。

(**)-请查看问题底部的“编辑”。

这是我的方法:

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);

        // Get our button from the layout resource,
        // and attach an event to it
        Button button = FindViewById<Button>(Resource.Id.MyButton);

        button.Click += async delegate 
        {
            button.Text = string.Format("{0} clicks!", count++);
            var hubConnection = new HubConnection("http://localhost:64642");
            var chatHubProxy = hubConnection.CreateHubProxy("ChatHub");
            try
            {
                // Start the connection
                await hubConnection.Start();

                // Invoke the 'UpdateNick' method on the server
                await chatHubProxy.Invoke("Send", new object[] { "TheMessage", "JohnDoe" });
            }
            catch(Exception ex)
            {

            }
        };
    }

{System.Net.WebException:错误:ConnectFailure(拒绝连接)---> System.Net.Sockets.SocketException:在System.Net.Sockets.Socket.Connect(System.Net.EndPoint remoteEP)[0x000cb]处拒绝连接System.Net.WebConnection.Connect上的/Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/System/System.Net.Sockets/Socket.cs:1313(System.Net.HttpWebRequest请求) /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/System/System.Net/WebConnection.cs:195中的[0x0019b]-内部异常堆栈跟踪的结尾-在系统上系统中/Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/System/System.Net/HttpWebRequest.cs:1023中的.Net.HttpWebRequest.EndGetResponse(System.IAsyncResult asyncResult)[0x0005e] .Threading.Tasks.TaskFactory 1[TResult].FromAsyncCoreLogic (System.IAsyncResult iar, System.Func 2 [T,TResult] endFunction,System.Action 1[T] endAction, System.Threading.Tasks.Task 1 [TResult]承诺,System.Boolean requireSynchronization)[0x00014]在/ 用户/助洗剂/数据/通道/ 3511 / 77cb8568 /源/单声道/ MCS /类/ referencesource / mscorlib程序/系统/线程/任务/ FutureFactory.cs:550

---从之前引发异常的位置开始的堆栈跟踪---位于/ Users / builder / data / lanes / 3511 / 77cb8568 / source / mono /中的System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()[0x0000c] mcs / class / referencesource / mscorlib / system / runtime / exceptionservices / exceptionservicescommon.cs:143在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task任务)[0x00047]在/ Users / builder / data / lanes / 3511 / 77cb8568 / source / mono / mcs / class / referencesource / mscorlib / system / runtime / compilerservices / TaskAwaiter.cs:187,位于System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task任务)[ 0x0002e]在/Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156中位于System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd( System.Threading.Tasks.Task任务)[0x0000b]在/ Users / builder / data / lanes / 3511 / 77cb8568 / source / mono / mcs / clas中 s / referencesource / mscorlib / system / runtime / compilerservices / TaskAwaiter.cs:128 at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1 + ConfiguredTaskAwaiter [TResult] .GetResult()在/ Users / builder / data / lanes / 3511中[0x00000] /77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:535,位于/ Users / builder / data中的System.Net.Http.HttpClientHandler + c__async0.MoveNext()[0x003ce] /lanes/3511/77cb8568/source/mono/mcs/class/System.Net.Http/System.Net.Http/HttpClientHandler.cs:379

---从之前引发异常的位置开始的堆栈跟踪---位于/ Users / builder / data / lanes / 3511 / 77cb8568 / source / mono /中的System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()[0x0000c] mcs / class / referencesource / mscorlib / system / runtime / exceptionservices / exceptionservicescommon.cs:143在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task任务)[0x00047]在/ Users / builder / data / lanes / 3511 / 77cb8568 / source / mono / mcs / class / referencesource / mscorlib / system / runtime / compilerservices / TaskAwaiter.cs:187,位于System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task任务)[ 0x0002e]在/Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156中位于System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd( System.Threading.Tasks.Task任务)[0x0000b]在/ Users / builder / data / lanes / 3511 / 77cb8568 / source / mono / mcs / clas中 s / referencesource / mscorlib / system / runtime / compilerservices / TaskAwaiter.cs:128 at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1 + ConfiguredTaskAwaiter [TResult] .GetResult()在/ Users / builder / data / lanes / 3511中[0x00000] /77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:535,位于/ Users / builder / data中的System.Net.Http.HttpClient + c__async0.MoveNext()[0x000a9] /lanes/3511/77cb8568/source/mono/mcs/class/System.Net.Http/System.Net.Http/HttpClient.cs:276

---从之前引发异常的位置开始的堆栈跟踪---位于/ Users / builder / data / lanes / 3511 / 77cb8568 / source / mono /中的System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()[0x0000c] mcs / class / referencesource / mscorlib / system / runtime / exceptionservices / exceptionservicescommon.cs:143在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task任务)[0x00047]在/ Users / builder / data / lanes / 3511 / 77cb8568 / source / mono / mcs / class / referencesource / mscorlib / system / runtime / compilerservices / TaskAwaiter.cs:187,位于System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task任务)[ 0x0002e]在/Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156中位于System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd( System.Threading.Tasks.Task任务)[0x0000b]在/ Users / builder / data / lanes / 3511 / 77cb8568 / source / mono / mcs / clas中 s / referencesource / mscorlib / system / runtime / compilerservices / TaskAwaiter.cs:128在/ Users / builder / data / lanes / 3511 / 77cb8568 / source / mono /中System.Runtime.CompilerServices.TaskAwaiter.GetResult()[0x00000] mcs / class / referencesource / mscorlib / system / runtime / compilerservices / TaskAwaiter.cs:113 at AndroidApp.MainActivity + <> c__DisplayClass1_0 + <b__0> d.MoveNext()[0x000a5]在C:\\ Data \\ Projects \\ SinglaRSamples \\ AndroidApp \\ AndroidApp \\ MainActivity.cs:36}

编辑:

正如梅森Jason)杰森(Jason)建议的那样,我可能必须使用正确的IP而不是“ localhost”。 Android仿真器看到实际localhost的IP。 我正在努力寻找该IP,它位于Android设置中。 任何帮助将是巨大的!

您可以使用Google SDK仿真器中的http://10.0.2.2:64642访问PC主机上运行的ASP.NET站点。 如果无法访问服务器,则可能需要配置Windows防火墙以允许传入的HTTP / HTTPS连接。

暂无
暂无

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

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