WebSocket4Net
WebSocket4Net copied to clipboard
Xamarin.Android throws not implemented exception
On Xamarin Android when attempting to open a connection when there is no internet connection causes the app to crash with a System.NotImplementedException
. I've put this code in my MainActivity
in the OnCreate()
method
var _websocket = new WebSocket("wss://ws-us2.pusher.com/app/1?protocol=5&client=pusher-dotnet-client&version=0.0.1")
{
EnableAutoSendPing = true,
AutoSendPingInterval = 8
};
_websocket.Error += (sender, args) =>
{
};
_websocket.Open();
I'd be happy to provide additional information if necessary.
Do you have the stacktrace of this exception?
Xamarin caused by: android.runtime.JavaProxyThrowable: System.NotImplementedException: The method or operation is not implemented.
System.Net.Sockets.SocketAsyncEventArgs.FinishOperationAsyncFailure(Exception exception, int bytesTransferred, SocketFlags flags)<6c708cf596db438ebfc6b7e012659eee>:0
System.Net.Sockets.MultipleConnectAsync.AsyncFail(Exception e)<6c708cf596db438ebfc6b7e012659eee>:0
System.Net.Sockets.MultipleConnectAsync.Fail(bool sync, Exception e)<6c708cf596db438ebfc6b7e012659eee>:0
System.Net.Sockets.MultipleConnectAsync.DoDnsCallback(IAsyncResult result, bool sync)<6c708cf596db438ebfc6b7e012659eee>:0
System.Net.Sockets.MultipleConnectAsync.DnsCallback(IAsyncResult result)<6c708cf596db438ebfc6b7e012659eee>:0
System.Runtime.Remoting.Messaging.AsyncResult.Invoke(AsyncResult)(wrapper managed-to-native)
System.Runtime.Remoting.Messaging.AsyncResult.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()<896ad1d315ca4ba7b117efb8dacaedcf>:0
System.Threading.ThreadPoolWorkQueue.Dispatch()<896ad1d315ca4ba7b117efb8dacaedcf>:0
System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()<896ad1d315ca4ba7b117efb8dacaedcf>:0
Which target of the library do you use? I though it is .netstandard one because I didn't release assemblies for 0.15 in the Xamarin platform.
Looks like it's monoandroid10, or is that not what you were looking for?
Are you sue if you use WebSocket4net 0.15?
Installing 0.15 seems to want to target netstandard 1.3, which causes the error, downgrading to 0.14.1 seems to target monoandroid23
which does seem to work. Although when downgrading I also had to uninstall SuperSocket.ClientEngine.Core
to get it to work.
So the version you have problem now is 0.14.1?
Installing version 0.14.1 works, but installing version 0.15 does NOT work
So the exception was thrown when you used websocket4net 0.14.1?
No the exception is thrown on version 0.15
That means you use websocket4net 0.15 .nerstandard version in Xamarin. Is my understanding correct?
That is true
Ok, let me see if we have a work around for this issue.
I did a fix.
Could you build the websocket4net by your own with the latest source code?
And set the environment before you start the connection like the code below:
Environment.SetEnvironmentVariable("PREFER_DNS_IN_ADVANCE", "true");
I downloaded the source today built the WebSocket4Net.Xamarin solution in release mode. I then referenced the MonoDroid version of the dll in a test project. Put this code in the Main Activity
System.Environment.SetEnvironmentVariable("PREFER_DNS_IN_ADVANCE", "true");
var _websocket = new WebSocket("wss://ws-us2.pusher.com/app/1?protocol=5&client=pusher-dotnet-client&version=0.0.1")
{
EnableAutoSendPing = true,
AutoSendPingInterval = 8
};
_websocket.Error += (sender, args) =>
{
};
_websocket.Open();
I disabled wifi and mobile data on the device before launching the application. The error still persists. Same stack trace.
Could you check the version numver of the dll you use? And Could you send me the stacktrace again? IT might be different with the old one.
Can you use the .net standard version?
We too see this issue - it's most easily repeated when attempting to connect to an invalid host from Android.
I'll try adding the PREFER_DNS_IN_ADVANCE and see what happens.
PREFER_DNS_IN_ADVANCE had no appreciable effect; this is running off of:
- WebSocket4Net 0.15.1
- SuperSocket.ClientEngine.Core 0.9.0
The project is all using .NET Standard so I'm assuming it's using the .NET Standard versions of WebSocket4Net and SuperSocket. The wrapping project that consumes these packages are at .NET Standard 1.6.
Here's the stack trace:
The method or operation is not implemented.
at System.Net.Sockets.SocketAsyncEventArgs.FinishOperationAsyncFailure (System.Exception exception, System.Int32 bytesTransferred, System.Net.Sockets.SocketFlags flags) [0x00000] in <6c708cf596db438ebfc6b7e012659eee>:0
at System.Net.Sockets.MultipleConnectAsync.AsyncFail (System.Exception e) [0x0001a] in <6c708cf596db438ebfc6b7e012659eee>:0
at System.Net.Sockets.MultipleConnectAsync.InternalConnectCallback (System.Object sender, System.Net.Sockets.SocketAsyncEventArgs args) [0x000b7] in <6c708cf596db438ebfc6b7e012659eee>:0
at System.Net.Sockets.SocketAsyncEventArgs.OnCompleted (System.Net.Sockets.SocketAsyncEventArgs e) [0x0000e] in <6c708cf596db438ebfc6b7e012659eee>:0
at System.Net.Sockets.SocketAsyncEventArgs.Complete () [0x00000] in <6c708cf596db438ebfc6b7e012659eee>:0
at System.Net.Sockets.Socket+<>c.<.cctor>b__306_3 (System.IAsyncResult ares) [0x00057] in <6c708cf596db438ebfc6b7e012659eee>:0
at System.Net.Sockets.SocketAsyncResult+<>c.<Complete>b__27_0 (System.Object state) [0x0000b] in <6c708cf596db438ebfc6b7e012659eee>:0
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem () [0x00008] in <896ad1d315ca4ba7b117efb8dacaedcf>:0
at System.Threading.ThreadPoolWorkQueue.Dispatch () [0x00074] in <896ad1d315ca4ba7b117efb8dacaedcf>:0
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback () [0x00000] in <896ad1d315ca4ba7b117efb8dacaedcf>:0
There is a patch for Mono that we need to review, but would love a test case:
https://github.com/mono/mono/pull/6431
i used the websocket4net 14.1 for a .net framework projekt and switched to .net standard now to use it within xamarin.android and xamarin.ios as well. Which just doesn't work with my implementation on android and ios because of the issue.
Any news on this? Or steps to get it work for the moment? Any help would be appreciated
Hi,
This issue results in instant application crash on both android & iOS, I believe anyone using current version 0.15.2 will stumble upon this issue.
Any update or when can we get a fix for this issue? Also would really appreciate if we can get a workaround this issue.
Ok, let me check this issue again.
Well i updated visual studio and xamarin for my project recently and somehow i dont get that error anymore
@NeroBoron ,
Are you testing on real device (Android/iOS) or emulator? Also I encountered this issue if i try to open a web-socket while device is in airplane mode.
On real ones, iPhone 8 and Samsung Galaxy A5 2017 to be specific. Can test it with airplane mode in about a week
Fwiw, we no longer receive this exception after updating mono.
This issue is resolved after upgrading mono.