【问题标题】:HttpListener Crashes in Release mode发布模式下的 HttpListener 崩溃
【发布时间】:2017-07-20 13:28:42
【问题描述】:

重现问题
1. 创建安卓应用
2. 安装 Nito.AsyncEx.Context
3.添加以下代码
4.设置发布模式
5. 部署代码
6. 启动应用程序(看着它燃烧!它在我的10.1" Marshmallow... 模拟器和Samsung Galaxy Note 10.1 上都崩溃了)

using Android.App;
using Android.Widget;
using Android.OS;
using System.Threading.Tasks;
using System.Net;
using System.Text;
using Nito.AsyncEx;

namespace App1
{
    [Activity(Label = "App1", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        private readonly HttpListener listener = new HttpListener();

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Main);
            var thread = new System.Threading.Thread(RunThread);
            thread.Start();
        }

        void RunThread()
        {
            AsyncContext.Run(() => Run()); //Exception in release mode
            //Task.Run(Run); //Silent exception in release mode
        }

        protected async Task Run()
        {
            listener.Prefixes.Add("http://localhost:8082/");
            listener.Start();
            while (true)
            {
                var context = await listener.GetContextAsync();
                var data = Encoding.UTF8.GetBytes("<html><body>Hello world</body></html>");
                context.Response.OutputStream.Write(data, 0, data.Length);
                context.Response.Close();
            }
        }
    }
}

我得到以下异常:

E/mono    (29964): Unhandled Exception:
E/mono    (29964): System.Net.Sockets.SocketException (0x80004005): mono-io-layer-error (10013)
E/mono    (29964):   at System.Net.Sockets.Socket..ctor (System.Net.Sockets.AddressFamily addressFamily, System.Net.Sockets.SocketType socketType, System.Net.Sockets.ProtocolType protocolType) [0x00069] in <12894373fb4d403880bd3e387a4ef038>:0
E/mono    (29964):   at System.Net.EndPointListener..ctor (System.Net.HttpListener listener, System.Net.IPAddress addr, System.Int32 port, System.Boolean secure) [0x0003b] in <12894373fb4d403880bd3e387a4ef038>:0
E/mono    (29964):   at System.Net.EndPointManager.GetEPListener (System.String host, System.Int32 port, System.Net.HttpListener listener, System.Boolean secure) [0x0009d] in <12894373fb4d403880bd3e387a4ef038>:0
E/mono    (29964):   at System.Net.EndPointManager.AddPrefixInternal (System.String p, System.Net.HttpListener listener) [0x0005e] in <12894373fb4d403880bd3e387a4ef038>:0
E/mono    (29964):   at System.Net.EndPointManager.AddListener (System.Net.HttpListener listener) [0x0009c] in <12894373fb4d403880bd3e387a4ef038>:0
E/mono    (29964):   at System.Net.HttpListener.Start () [0x0000f] in <12894373fb4d403880bd3e387a4ef038>:0
...

【问题讨论】:

  • 尝试获取实际的异常消息,将运行代码包装在 try/catch 中,看看它告诉你什么。

标签: c# xamarin.android httplistener


【解决方案1】:

要打开套接字,您的应用程序需要INTERNET 权限。将以下行添加到您的 AndroidManifest.xml 文件中:

<uses-permission android:name="android.permission.INTERNET" />

【讨论】:

  • 因为它是在调试模式下自动添加的?
猜你喜欢
  • 2021-07-17
  • 1970-01-01
  • 1970-01-01
  • 2018-12-31
  • 1970-01-01
  • 2016-02-05
  • 2020-03-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多