Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NSURLsessionDelegate issues #6493

Closed
Maddy79 opened this issue Jul 4, 2019 · 11 comments · Fixed by #6514
Closed

NSURLsessionDelegate issues #6493

Maddy79 opened this issue Jul 4, 2019 · 11 comments · Fixed by #6514
Assignees
Labels
bug If an issue is a bug or a pull request a bug fix iOS Issues affecting Xamarin.iOS macOS Issues affecting Xamarin.Mac
Milestone

Comments

@Maddy79
Copy link

Maddy79 commented Jul 4, 2019

Steps to Reproduce

  1. Create a class that derives from NSURLProtocol and NSURLSession delegate interfaces:
    Ex: public partial class TestUrlProtocol : NSUrlProtocol, INSUrlSessionDelegate, INSUrlSessionTaskDelegate, INSUrlSessionDataDelegate

  2. In StartLoading, create the URLSessionTask and resume the task

  3. Add delegate methods to handle challenge and redirection

Expected Behavior

This needs to build / run and handle NTLM /certificate challenges

Actual Behavior

First issue:
Gives a warning for the below delegate:
**Warning MT4174: Unable to locate the block to delegate conversion method for the method TestSampleApp.TestUrlProtocol.WillPerformHttpRedirection's parameter #5. (MT4174) Warning

MT4174: Unable to locate the block to delegate conversion method for the method TestSampleApp.TestUrlProtocol.DidReceiveChallenge's parameter #4. (MT4174)**

**And when running this will crash with below error:

Unhandled managed exception: The runtime function get_block_wrapper_creator has been linked away. (ObjCRuntime.RuntimeException)
Crash is pointing at DidReceiveChallenge call.**

Environment

=== Visual Studio Enterprise 2019 for Mac ===

Version 8.1.3 (build 19)
Installation UUID: 5bba6d13-ac9c-4c7c-b27c-1f9467a37562
GTK+ 2.24.23 (Raleigh theme)
Xamarin.Mac 5.6.0.25 (d16-0 / 50f7527)

Package version: 518010028

=== Mono Framework MDK ===

Runtime:
Mono 5.18.1.28 (2018-08/223ea7ef92e) (64-bit)
Package version: 518010028

=== NuGet ===

Version: 5.0.2.5988

=== .NET Core ===

Runtime: /usr/local/share/dotnet/dotnet
Runtime Versions:
2.1.11
2.1.9
2.1.8
2.1.2
2.1.1
SDK: /usr/local/share/dotnet/sdk/2.1.700/Sdks
SDK Versions:
2.1.700
2.1.505
2.1.504
2.1.302
2.1.301
MSBuild SDKs: /Library/Frameworks/Mono.framework/Versions/5.18.1/lib/mono/msbuild/Current/bin/Sdks

=== Xamarin.Profiler ===

Version: 1.6.10
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Updater ===

Version: 11

=== Apple Developer Tools ===

Xcode 10.2.1 (14490.122)
Build 10E1001

=== Xamarin.Mac ===

Version: 5.10.0.157 (Visual Studio Enterprise)
Hash: 6bd9475
Branch: d16-1
Build date: 2019-06-12 17:28:48-0400

=== Xamarin.iOS ===

Version: 12.10.0.157 (Visual Studio Enterprise)
Hash: 6bd9475
Branch: d16-1
Build date: 2019-06-12 17:28:47-0400

=== Xamarin.Android ===

Version: 9.3.0.23 (Visual Studio Enterprise)
Commit: HEAD/d0b48056f
Android SDK: /Users/mbhattathiri/Library/Developer/Xamarin/android-sdk-macosx
Supported Android versions:
7.1 (API level 25)
8.0 (API level 26)
8.1 (API level 27)

SDK Tools Version: 26.1.1
SDK Platform Tools Version: 28.0.1
SDK Build Tools Version: 27.0.3

Build Information:
Mono: mono/mono@3a07bd426d3
Java.Interop: xamarin/java.interop/d16-1@5ddc3e3
LibZipSharp: grendello/LibZipSharp/d16-1@44de300
LibZip: nih-at/libzip@b95cf3f
ProGuard: xamarin/proguard@905836d
SQLite: xamarin/sqlite@8212a2d
Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-1@acabd26

=== Microsoft Mobile OpenJDK ===

Java SDK: /Users/mbhattathiri/Library/Developer/Xamarin/jdk/microsoft_dist_openjdk_1.8.0.25
1.8.0-25
Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

=== Android Device Manager ===

Version: 1.2.0.44
Hash: aac645b
Branch: remotes/origin/d16-1
Build date: 2019-05-29 19:55:24 UTC

=== Xamarin Designer ===

Version: 16.1.0.467
Hash: f1657e133
Branch: remotes/origin/d16-1-new-document-model
Build date: 2019-06-18 21:57:42 UTC

=== Xamarin Inspector ===

Version: 1.4.3
Hash: db27525
Branch: 1.4-release
Build date: Mon, 09 Jul 2018 21:20:18 GMT
Client compatibility: 1

=== Build Information ===

Release ID: 801030019
Git revision: 2d04b0145da8aa946d2cc9898fe2a59484e4f3f4
Build date: 2019-06-26 15:34:25+00
Build branch: release-8.1
Xamarin extensions: de240bddd6d1f96bfb1e7d11ce7ac5398956fe1d

=== Operating System ===

Mac OS X 10.14.5
Darwin 18.6.0 Darwin Kernel Version 18.6.0
Thu Apr 25 23:16:27 PDT 2019
root:xnu-4903.261.4~2/RELEASE_X86_64 x86_64

Build Logs

Example Project (If Possible)

@spouliot
Copy link
Contributor

spouliot commented Jul 4, 2019

From the warnings/errors it looks like your delegate code is incorrect. Please attach a self-contained test case so we can have a look at the implementation.

It's likely easier to use https://github.com/xamarin/xamarin-macios/blob/master/src/Foundation/NSUrlSessionHandler.cs with HttpClient if you want to use the native NSURLSession API.

@spouliot spouliot added iOS Issues affecting Xamarin.iOS need-info Waiting for more information before the bug can be investigated support The issue is related to support labels Jul 4, 2019
@spouliot spouliot added this to the Future milestone Jul 4, 2019
@Maddy79
Copy link
Author

Maddy79 commented Jul 4, 2019

@spouliot
Attached test app. If http is used it is loading fine, issue is when using https and then it needs to call the challenge delegates.
https://drive.google.com/file/d/1h5m6A-YIrjQwcoEXa-qKekBloo_n2Yya/view?usp=sharing

@spouliot
Copy link
Contributor

spouliot commented Jul 4, 2019

Thanks for the sample. Your issue is that you cannot override (ObjC) block-based arguments without some extra glue code.

E.g. this is how WillPerformHttpRedirection is declared in Xamarin.iOS.dll

[Export ("URLSession:task:willPerformHTTPRedirection:newRequest:completionHandler:")]
[BindingImpl (BindingImplOptions.GeneratedCode | BindingImplOptions.Optimizable)]
public unsafe virtual void WillPerformHttpRedirection (NSUrlSession session, NSUrlSessionTask task, NSHttpUrlResponse response, NSUrlRequest newRequest, [BlockProxy (typeof (ObjCRuntime.Trampolines.NIDActionArity1V1))]global::System.Action<NSUrlRequest> completionHandler)

The [BlockProxy] attribute points to extra code that will ensure a correct transition between ObjC and managed code.

This glue code is provided automatically if you use (or override) existing bindings - but if you declare the [Export] manually then you need to do the full job (and manually provide the required glue code).

This is not trivial so you can try to copy/paste existing code (it's largely internal) from Xamarin.iOS.dll into your application.

However I would suggest you to use HttpClient with the NSUrlSessionHandler since it will require a lot less code and ease code sharing across platforms.

@spouliot spouliot closed this as completed Jul 4, 2019
@spouliot spouliot removed the need-info Waiting for more information before the bug can be investigated label Jul 4, 2019
@Maddy79
Copy link
Author

Maddy79 commented Jul 5, 2019

@spouliot , thanks for looking into this. My requirement is to handle all the challenges (NTLM/Basic/ClientCertificate/ServerTrust) from my code , so I was doing this, which used to work till sometime back. Can you point me where I can get the code for this from Xamarin.iOS.dll?

@spouliot
Copy link
Contributor

spouliot commented Jul 5, 2019

Upon re-examination (by @rolfbjarne ) it's actually a supported scenario (i.e. it should work) but there's an issue in the static registrar for inherited interfaces. Re-opening

@spouliot spouliot reopened this Jul 5, 2019
@spouliot spouliot added bug If an issue is a bug or a pull request a bug fix macOS Issues affecting Xamarin.Mac and removed support The issue is related to support labels Jul 5, 2019
@rolfbjarne rolfbjarne self-assigned this Jul 5, 2019
@spouliot
Copy link
Contributor

spouliot commented Jul 5, 2019

@Maddy79 the workaround would be add the [BlockProxy] attributes and copy/paste the source code (of the type referenced) into your application.

Sadly the Trampolines.g.cs file is generated code that is not shipped (like others) inside the package (filed as #6507) but you can use the VS assembly browser to disassemble/decompile it back to C#.

@Maddy79
Copy link
Author

Maddy79 commented Jul 5, 2019

@spouliot , yea when I tried using the BlockProxy , I did get the accessibility error for trampolines.
Will check the decompile option, is there any possibility of getting the issue fix in any upcoming release?

@spouliot
Copy link
Contributor

spouliot commented Jul 5, 2019

yes, it will be fixed in a future release - but I cannot say when it will be released (even in a preview).

Still it will be available (as an untested package, directly from our build bots) soon after this bug is closed (and you'll get a notification when that happens)

rolfbjarne added a commit to rolfbjarne/xamarin-macios that referenced this issue Jul 8, 2019
…amarin#6493.

When we're searching for metadata for marshalling blocks, we must search the
entire interface hierarchy for protocols that implement optional members.

Fixes xamarin#6493.
rolfbjarne added a commit that referenced this issue Jul 8, 2019
…6493. (#6514)

When we're searching for metadata for marshalling blocks, we must search the
entire interface hierarchy for protocols that implement optional members.

Fixes #6493.
monojenkins pushed a commit to monojenkins/xamarin-macios that referenced this issue Jul 8, 2019
…amarin#6493.

When we're searching for metadata for marshalling blocks, we must search the
entire interface hierarchy for protocols that implement optional members.

Fixes xamarin#6493.
monojenkins pushed a commit to monojenkins/xamarin-macios that referenced this issue Jul 8, 2019
…amarin#6493.

When we're searching for metadata for marshalling blocks, we must search the
entire interface hierarchy for protocols that implement optional members.

Fixes xamarin#6493.
@Maddy79
Copy link
Author

Maddy79 commented Jul 9, 2019

@spouliot , is the fix available in the Preview builds of VS (Mac)?

rolfbjarne pushed a commit that referenced this issue Jul 9, 2019
…6493. (#6525)

When we're searching for metadata for marshalling blocks, we must search the
entire interface hierarchy for protocols that implement optional members.

Fixes #6493.
rolfbjarne pushed a commit that referenced this issue Jul 9, 2019
…6493. (#6524)

When we're searching for metadata for marshalling blocks, we must search the
entire interface hierarchy for protocols that implement optional members.

Fixes #6493.
@hmeng7
Copy link

hmeng7 commented Jul 25, 2019

Want to check on this to see if this fix is available now since the thread was closed 16 days ago as of the post. Thanks!

@rolfbjarne
Copy link
Member

The fix is currently available in our Xcode 11 preview channel. It will also be included in an upcoming update to the existing preview release in our Alpha/Beta channels.

@ghost ghost locked as resolved and limited conversation to collaborators May 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug If an issue is a bug or a pull request a bug fix iOS Issues affecting Xamarin.iOS macOS Issues affecting Xamarin.Mac
Projects
None yet
4 participants