Skip to content

NSURLsessionDelegate issues  #6493

Closed

Description

@Maddy79

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)

Activity

  1. spouliot commented on Jul 4, 2019

    @spouliot
    Contributor

    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.

  2. added
    iOS
    need-info
    support
    on Jul 4, 2019
  3. added this to the Future milestone on Jul 4, 2019
  4. Maddy79 commented on Jul 4, 2019

    @Maddy79
    Author

    @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

  5. spouliot commented on Jul 4, 2019

    @spouliot
    Contributor

    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.

  6. removed
    need-info
    on Jul 4, 2019
  7. Maddy79 commented on Jul 5, 2019

    @Maddy79
    Author

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

  8. spouliot commented on Jul 5, 2019

    @spouliot
    Contributor

    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

  9. added
    bug
    macOS
    and removed
    support
    on Jul 5, 2019
  10. self-assigned this
    on Jul 5, 2019
  11. spouliot commented on Jul 5, 2019

    @spouliot
    Contributor

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

  12. Maddy79 commented on Jul 5, 2019

    @Maddy79
    Author

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

  13. spouliot commented on Jul 5, 2019

    @spouliot
    Contributor

    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)

  14. added a commit that references this issue on Jul 8, 2019
    22c2b72
  15. added a commit that references this issue on Jul 8, 2019
    0ceced2
  16. added 2 commits that reference this issue on Jul 8, 2019
    748bedd
    c37aba5
  17. Maddy79 commented on Jul 9, 2019

    @Maddy79
    Author

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

  18. added 2 commits that reference this issue on Jul 9, 2019
    14efd53
    a7f4c70
  19. hmeng7 commented on Jul 25, 2019

    @hmeng7

    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!

  20. rolfbjarne commented on Jul 25, 2019

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

  21. ghost locked as resolved and limited conversation to collaborators on May 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugiOSmacOS

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions