C# HttpListener在发布模式下崩溃

C# HttpListener在发布模式下崩溃,c#,xamarin.android,httplistener,C#,Xamarin.android,Httplistener,重新创建问题 1.创建Android应用程序 2.安装Nito.AsyncEx.Context 3.在下面添加代码 4.设置释放模式 5.部署代码 6.启动应用程序(并观看它燃烧!它在我的10.1“棉花糖…模拟器和三星Galaxy Note 10.1上崩溃) 使用Android.App; 使用Android.Widget; 使用Android.OS; 使用System.Threading.Tasks; Net系统; 使用系统文本; 使用Nito.AsyncEx; 名称空间App1 { [活动(L

重新创建问题
1.创建Android应用程序
2.安装Nito.AsyncEx.Context
3.在下面添加代码
4.设置释放模式
5.部署代码
6.启动应用程序(并观看它燃烧!它在我的
10.1“棉花糖…
模拟器和
三星Galaxy Note 10.1
上崩溃)

使用Android.App;
使用Android.Widget;
使用Android.OS;
使用System.Threading.Tasks;
Net系统;
使用系统文本;
使用Nito.AsyncEx;
名称空间App1
{
[活动(Label=“App1”,MainLauncher=true,Icon=“@drawable/Icon”)]
公共课活动:活动
{
私有只读HttpListener侦听器=新HttpListener();
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
var thread=new System.Threading.thread(RunThread);
thread.Start();
}
void RunThread()
{
AsyncContext.Run(()=>Run());//发布模式下出现异常
//Task.Run(Run);//发布模式下的静默异常
}
受保护的异步任务运行()
{
listener.Prefixes.Add(“http://localhost:8082/");
listener.Start();
while(true)
{
var context=await listener.GetContextAsync();
var data=Encoding.UTF8.GetBytes(“Hello world”);
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
...
E/mono(29964):未处理的异常:
E/mono(29964):System.Net.Sockets.SocketException(0x80004005):mono io层错误(10013)
E/mono(29964):位于System.Net.Sockets.Socket..ctor(System.Net.Sockets.AddressFamily AddressFamily,System.Net.Sockets.SocketType SocketType,System.Net.Sockets.ProtocolType)[0x00069]in:0
E/mono(29964):位于System.Net.EndPointListener..ctor(System.Net.HttpListener listener,System.Net.IPAddress addr,System.Int32端口,System.Boolean安全)[0x0003b]中:0
E/mono(29964):位于System.Net.EndPointManager.GetEPListener(System.String主机、System.Int32端口、System.Net.HttpListener侦听器、System.Boolean安全)[0x0009d]中:0
E/mono(29964):位于System.Net.EndPointManager.AddPrefixInternal(System.String p,System.Net.HttpListener listener)[0x0005e]中:0
E/mono(29964):位于System.Net.EndPointManager.AddListener(System.Net.HttpListener listener)[0x0009c]中:0
E/mono(29964):位于System.Net.HttpListener.Start()[0x0000f]中:0
...

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

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


尝试获取实际的异常消息,将运行代码包装在Try/catch中,看看它告诉了您什么。这是因为它是在调试模式下自动添加的
<uses-permission android:name="android.permission.INTERNET" />