【问题标题】:Can't connect to ASP.NET Core SignalR in my simple xamarin chat app无法在我的简单 xamarin 聊天应用程序中连接到 ASP.NET Core SignalR
【发布时间】:2019-11-15 19:56:04
【问题描述】:

嘿,我是 ASP.NET Core SignalR 的新手,在我正在制作的这个准系统聊天应用程序中,我无法弄清楚如何连接到我的后端。
我不确定我是否做得对。我遇到的大多数文档似乎都已经过时了。

这是我的 Startup.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Backboy.Hubs;

namespace Backboy {
    public class Startup {
        public Startup(IConfiguration configuration) {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services) {
            services.AddRazorPages();
            services.AddSignalR();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
            if (env.IsDevelopment()) {
                app.UseDeveloperExceptionPage();
            }
            else {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints => {
                endpoints.MapRazorPages();
                endpoints.MapHub<ChatHub>("/chatHub");
            });
        }
    }
}

这是我尝试从应用程序连接的方式。我开始像这样创建一个 HubConnection:

hubConnection = new HubConnectionBuilder().WithUrl($"https://localhost:44308/chatHub").Build();

我怀疑这条线出了什么问题。但我不应该使用其他网址。 https://localhost:44308 是我运行程序时在浏览器中打开的内容。

这也是我尝试连接的函数。

async void Connect(object sender, EventArgs e) {
            try {
                await hubConnection.StartAsync();
                await hubConnection.InvokeAsync("JoinChat", name);
                PutOnScreen(name, "Connected");
            }
            catch(Exception ex) {
                PutOnScreen(name, "Could not connect");
            }
        }

我得到的例外是:

 System.Net.Http.HttpRequestException: Network subsystem is down ---> System.Net.Sockets.SocketException: Network subsystem is down
  at System.Net.Http.ConnectHelper.ConnectAsync (System.String host, System.Int32 port, System.Threading.CancellationToken cancellationToken) [0x00110] in <c85119bf7e3e421490ae6b5487992fff>:0 

任何帮助将不胜感激!

【问题讨论】:

  • 使用你的服务器的IP地址,而不是“localhost”
  • @Jason 你不能使用 localhost 在本地运行集线器吗?
  • 可能,但是在移动应用程序中使用“localhost”会导致设备(或模拟器)尝试连接到 ITSELF,而不是连接到 PC
  • 是的,就是这样,只需发布​​它并开始工作。谢谢!
  • 您可以将解决方案发布为答案并接受它,这将帮助更多人:)

标签: c# asp.net-core xamarin.forms signalr chat


【解决方案1】:

如果您使用的是 android 模拟器,则不能使用 localhost,因为它会尝试连接到模拟器本身。

您必须使用保留的 IP 地址http://10.0.2.2:{portNumber}/ 才能访问您计算机的 localhost。

【讨论】:

    猜你喜欢
    • 2012-05-06
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多