我有一个ASP.NET 6.0 Core Web Api with Signalr发布到Windows Server 2017上的IIS. 然后我有一个React-Native移动应用程序,它使用@microsoft/signalr连接到SignalR中心. 当移动应用程序通过Android模拟器和我的Android手机在调试模式下运行时,它可以连接到Signalr,没有任何问题. 一切都很完美. 现在,当我try 在发布模式下运行相同的应用程序时,它不工作,也无法连接. 任何 idea /帮助将不胜感激.

Program.cs Main


            builder.Services.AddCors(options =>
            {
                options.AddDefaultPolicy(builder =>
                {
                    builder.AllowAnyOrigin()
                           .AllowAnyMethod()
                           .AllowAnyHeader();
                           //.AllowCredentials()
                           //.SetIsOriginAllowed((host) => true);
                });
            }
            );

            // Add services to the container.
            builder.Services.AddControllers()
                            .AddJsonOptions(options =>
                            {
                                options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
                                //options.JsonSerializerOptions.PropertyNamingPolicy = null;
                            });


            builder.Services.AddEndpointsApiExplorer();
            builder.Services.AddSwaggerGen();
            builder.Services.AddSignalR();
            builder.Logging.ClearProviders();
            Serilog.ILogger logger = new AppLogger(builder.Configuration).Logger;
            builder.Logging.AddSerilog(logger, true);

            var app = builder.Build();
            
            // Configure the HTTP request pipeline.
            if (app.Environment.IsDevelopment() || app.Environment.IsProduction())
            {
                app.UseSwagger();
                app.UseSwaggerUI();
            }

            //app.UseHttpsRedirection();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseCors();
            app.UseWebSockets();

            app.MapControllers();
            app.MapHub<FacilityHub>("/facility-hub");

            app.Run();

我运行了以下命令,查看应用程序在发布模式下运行时的日志(log).

NPX REACT-本机日志(log)-Android

NPX REACT-本机运行-Android-模式发布

Here is the error in the logs when mobile app is running is release mode. Release mode error

Here is the error in the logs when mobile app is running is debug mode. Development mode error

推荐答案

在Release模式下,Android对不安全的http请求有限制.我们只需要在配置文件中添加一些配置.

Android Manifest.xml文件:

  1. 确保您已添加使用网络的权限.

    <uses-permission android:name="android.permission.INTERNET" />
    
  2. 在应用程序标记中设置以下属性:

    <application
        ...
        android:usesCleartextTraffic="true"
        ...>
        ...
     </application>
    
  3. 如果您希望在本机代码中使用已弃用的HttpClient来发送http请求,则需要添加以下标签:

    <uses-library android:name="org.apache.http.legacy" android:required="false"/>
    

相关链接:Behavior changes: apps targeting API level 28+

Asp.net相关问答推荐

在Docker Windows中,ASP.NET核心容器在自定义端口8080上运行,但ASP.NET容器在固定端口80上运行

.Net Framework:w3wp.exe 中的异常

SignalR + Autofac + OWIN:为什么 GlobalHost.ConnectionManager.GetHubContext 不起作用?

ASP.NET:获取自 1970 年 1 月 1 日以来的毫秒数

控制器 SessionStateBehavior 是只读的,我可以更新会话变量

如何从 RouteData 获取路由名称?

如何更改 .ASPX 自动格式化设置 (Visual Studio)

什么相当于 ASP.NET 中的 cron 作业(job)?

Httpclient 该实例已经启动了一个或多个请求.只能在发送第一个请求之前修改属性

VirtualPath 在当前应用程序根目录之外

如何在 ASP.NET 中仅在调试模式下执行代码

如何获取 ActionLink 的工具提示?

带有完整内存错误的 WCF 服务(内存门判断失败,因为可用内存) - 如何解决

如何找出我的 Windows 域上托管 LDAP 的服务器?

ASP.NET 中的全局资源与本地资源

将 Http 请求读入字节数组

ASP.NET MVC 4 的 Windows 身份验证 - 它是如何工作的,如何测试它

如何告诉 RadioButtonList 不生成表格

asp.net core 2.0 - 值不能为空.参数名称:连接字符串

ASP.NET MVC 5 Web.config:FormsAuthenticationModule或FormsAuthentication