Using latest Refit 4.1.0 on iOS I get this exception and crash when using the "Link sdk frameworks only" build option. I do not have the same problem on Android.
When turning off linking, it works fine. Please see the attached screenshot. The interface where the exception is triggered is pasted below. Its the [Get("/order")] method.
https://github.com/paulcbetts/refit/issues/413 According to the developer, all tags are in place for this to work.

public interface IOrderApi
{
[Get("/order")]
Task<TOrderListResponse> List(string route, string createdAfter, CancellationToken? cancellationToken);
[Post("/order/accept")]
Task Accept(string orderId);
[Post("/order/reject")]
Task Reject(string orderId);
}
[Preserve]
public partial class AutoGeneratedIOrderApi : IOrderApi
{
/// <inheritdoc />
public HttpClient Client { get; protected set; }
readonly Dictionary<string, Func<HttpClient, object[], object>> methodImpls;
public AutoGeneratedIOrderApi(HttpClient client, IRequestBuilder requestBuilder)
{
methodImpls = requestBuilder.InterfaceHttpMethods.ToDictionary(k => k, v => requestBuilder.BuildRestResultFuncForMethod(v));
Client = client;
}
/// <inheritdoc />
public virtual Task<TOrderListResponse> List(string route,string createdAfter,CancellationToken? cancellationToken)
{
var arguments = new object[] { route,createdAfter,cancellationToken };
return (Task<TOrderListResponse>) methodImpls["List"](Client, arguments);
}
/// <inheritdoc />
public virtual Task Accept(string orderId)
{
var arguments = new object[] { orderId };
return (Task) methodImpls["Accept"](Client, arguments);
}
/// <inheritdoc />
public virtual Task Reject(string orderId)
{
var arguments = new object[] { orderId };
return (Task) methodImpls["Reject"](Client, arguments);
}
}
No crash using link sdk frameworks
ArgumentException when using link sdk frameworks.
=== Visual Studio Community 2017 for Mac ===
Version 7.3.3 (build 5)
Installation UUID: dfec3931-428b-4306-bdf4-b73a222dfc74
Runtime:
Mono 5.4.1.7 (2017-06/e66d9abbb27) (64-bit)
GTK+ 2.24.23 (Raleigh theme)
Package version: 504010007
=== NuGet ===
Version: 4.3.1.4445
=== .NET Core ===
Runtime: /usr/local/share/dotnet/dotnet
Runtime Versions:
2.0.0
1.1.4
1.1.2
1.1.1
1.0.7
1.0.5
1.0.4
SDK: /usr/local/share/dotnet/sdk/2.0.2/Sdks
SDK Versions:
2.0.2
2.0.0
1.1.4
1.0.4
1.0.0-rc4-004919
MSBuild SDKs: /Library/Frameworks/Mono.framework/Versions/5.4.1/lib/mono/msbuild/15.0/bin/Sdks
=== Xamarin.Profiler ===
Version: 1.6.0
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler
=== Apple Developer Tools ===
Xcode 9.2 (13772)
Build 9C40b
=== Xamarin.iOS ===
Version: 11.6.1.3 (Visual Studio Community)
Hash: f70a1348
Branch: xcode9.2
Build date: 2017-12-18 14:47:16-0500
=== Xamarin.Android ===
Version: 8.1.3.0 (Visual Studio Community)
Android SDK: /Users/kenneth/Library/Developer/Xamarin/android-sdk-macosx
Supported Android versions:
6.0 (API level 23)
7.0 (API level 24)
7.1 (API level 25)
8.0 (API level 26)
SDK Tools Version: 25.2.5
SDK Platform Tools Version: 27.0.0
SDK Build Tools Version: 25.0.3
Java SDK: /usr
java version "1.8.0_101"
Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)
Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL
=== Xamarin.Mac ===
Version: 4.0.0.215 (Visual Studio Community)
=== Xamarin Inspector ===
Version: 1.4.0
Hash: b3f92f9
Branch: master
Build date: Fri, 19 Jan 2018 22:00:34 GMT
Client compatibility: 1
=== Build Information ===
Release ID: 703030005
Git revision: b1c2982e201e71ef758866c9ade05f253a8c6f47
Build date: 2017-12-21 11:04:40-05
Xamarin addins: f397ddfbacfb39e60c9cc8d9e410f73faf8c2cbc
Build lane: monodevelop-lion-d15-5
=== Operating System ===
Mac OS X 10.13.2
Darwin 17.3.0 Darwin Kernel Version 17.3.0
Thu Nov 9 18:09:22 PST 2017
root:xnu-4570.31.3~1/RELEASE_X86_64 x86_64
=== Enabled user installed addins ===
MvvmCross Template pack 2.0.1
Internet of Things (IoT) development (Preview) 7.1
Hi,
Can you provide a small test case showing the issue?
I am not familiar with the Refit package and I'm having a hard time reproducing the issue only with the code you pasted (:
Thanks.
I did some more research into this.
I changed the signature of my method from
[Get("/order")]
Task<TOrderListResponse> List(string route, string createdAfter, CancellationToken? cancellationToken);
to
[Get("/order")]
Task<TOrderListResponse> List(string route, string createdAfter, CancellationToken token = default(CancellationToken));
Now it works. Any idea why? And remember, this is only an issue when link sdk frameworks is enabled. The calling method always provides a cancellationtoken.
I've recreated the issue in a test project at https://github.com/KennethKr/RefitLinkErrorTest
Thanks for the test case (:
I could confirm this bug with the following environment: https://gist.github.com/VincentDondain/52445d66550d45650f30b09e0c9c5c5a
Moving away from the nullable type (on the List method) does indeed seem to solve the issue.
@KennethKr the linker works by doing a static analysis of the application. It's often the pieces used by reflection that are tricky. If something else use A then it will work with reflection. If nothing use A (statically) then reflection will fail. IOW changing your signature might just bring what's needed (and not really related to the signature itself).
The fact that [Preserve] is added to the generated code is likely not helpful in this case. Because the error mention Handle and there's no such symbol in the (preserved) generated code.
quick workaround, add:
var mre = new System.Threading.ManualResetEvent (false);
mre.Handle = mre.Handle;
inside your application.
That ensure both the getter and setter of Handle are _seen_ by the linker - which means they won't be removed (and won't cause the issue above).
I'll have a look why (in refit) but it's really used thru reflection and (unless preserved) it's normal for the linker to remove it.
Thank you for clearing that up. I鈥檝e changed my usage of Refit, like stated above, as my first attempt isnt really correct (using CancellationToken?). So, from my point of view it鈥檚 fine now.
Based on your comments it seems this behaviour is correct from the linker point of view, and that this could be closed?

@VincentDondain all the hints for the workaround could be seen inside VS using the debugger (when debugging the BCL is enabled).
@KennethKr yes, I'll bring this up with refit soon (out of town next week). Thanks for the sample, that really helps!
Quick question, out of interest, why isnt this an issue on Android? The same code works with linking there.