WebSocket4Net icon indicating copy to clipboard operation
WebSocket4Net copied to clipboard

Xamarin.Android throws not implemented exception

Open rusty21 opened this issue 7 years ago • 28 comments

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.

rusty21 avatar Oct 09 '17 13:10 rusty21

Do you have the stacktrace of this exception?

kerryjiang avatar Oct 09 '17 16:10 kerryjiang

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

rusty21 avatar Oct 09 '17 17:10 rusty21

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.

kerryjiang avatar Oct 09 '17 17:10 kerryjiang

Looks like it's monoandroid10, or is that not what you were looking for?

rusty21 avatar Oct 09 '17 17:10 rusty21

Are you sue if you use WebSocket4net 0.15?

kerryjiang avatar Oct 09 '17 17:10 kerryjiang

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.

rusty21 avatar Oct 09 '17 19:10 rusty21

So the version you have problem now is 0.14.1?

kerryjiang avatar Oct 09 '17 20:10 kerryjiang

Installing version 0.14.1 works, but installing version 0.15 does NOT work

rusty21 avatar Oct 09 '17 20:10 rusty21

So the exception was thrown when you used websocket4net 0.14.1?

kerryjiang avatar Oct 09 '17 20:10 kerryjiang

No the exception is thrown on version 0.15

rusty21 avatar Oct 09 '17 20:10 rusty21

That means you use websocket4net 0.15 .nerstandard version in Xamarin. Is my understanding correct?

kerryjiang avatar Oct 09 '17 20:10 kerryjiang

That is true

rusty21 avatar Oct 09 '17 20:10 rusty21

Ok, let me see if we have a work around for this issue.

kerryjiang avatar Oct 09 '17 20:10 kerryjiang

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");

kerryjiang avatar Oct 18 '17 06:10 kerryjiang

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.

rusty21 avatar Oct 19 '17 12:10 rusty21

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.

kerryjiang avatar Oct 19 '17 16:10 kerryjiang

Can you use the .net standard version?

kerryjiang avatar Oct 20 '17 06:10 kerryjiang

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.

berdon avatar Dec 13 '17 20:12 berdon

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

berdon avatar Dec 13 '17 21:12 berdon

There is a patch for Mono that we need to review, but would love a test case:

https://github.com/mono/mono/pull/6431

migueldeicaza avatar Jan 06 '18 19:01 migueldeicaza

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

chrswgnr avatar Apr 03 '18 12:04 chrswgnr

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.

psharrma avatar Apr 11 '18 16:04 psharrma

Ok, let me check this issue again.

kerryjiang avatar Apr 11 '18 16:04 kerryjiang

Well i updated visual studio and xamarin for my project recently and somehow i dont get that error anymore

chrswgnr avatar Apr 11 '18 16:04 chrswgnr

@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.

psharrma avatar Apr 11 '18 16:04 psharrma

On real ones, iPhone 8 and Samsung Galaxy A5 2017 to be specific. Can test it with airplane mode in about a week

chrswgnr avatar Apr 11 '18 17:04 chrswgnr

Fwiw, we no longer receive this exception after updating mono.

berdon avatar Apr 11 '18 18:04 berdon

This issue is resolved after upgrading mono.

psharrma avatar May 24 '18 11:05 psharrma