Error creating a new System.Net.Http.HttpClient

danipendanipen USMember ✭✭
edited May 31 in Xamarin.Mac

Hi, I'm experiencing a weird issue, that maybe one of the experts at xamarin could help me to fix...

I'm executing the following piece of code:

new System.Net.Http.HttpClient()

in the following three scenarios:

  1. From a Xamarin.Mac GUI application
  2. From a Xamarin.Mac console application
  3. From a single Mono application

In cases 1) and 3), it works fine. However, in case 2) I'm getting the following exception when creating the new HttpClient instance:

Unhandled Exception:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentNullException: Value cannot be null.
Parameter name: obj
  at (wrapper managed-to-native) System.Object.__icall_wrapper_mono_monitor_enter_v4_internal(object,intptr)
  at ObjCRuntime.Class.GetClassHandle (System.Type type, System.Boolean throw_if_failure, System.Boolean& is_custom_type) [0x00007] in /Library/Frameworks/Xamarin.Mac.framework/Versions/5.0.0.0/src/Xamarin.Mac/ObjCRuntime/Class.cs:121 
  at ObjCRuntime.Class.GetClassHandle (System.Type type) [0x00001] in /Library/Frameworks/Xamarin.Mac.framework/Versions/5.0.0.0/src/Xamarin.Mac/ObjCRuntime/Class.cs:151 
  at ObjCRuntime.Class..ctor (System.Type type) [0x00008] in /Library/Frameworks/Xamarin.Mac.framework/Versions/5.0.0.0/src/Xamarin.Mac/ObjCRuntime/Class.cs:59 
  at ObjCRuntime.RuntimeOptions.Read () [0x00001] in /Library/Frameworks/Xamarin.Mac.framework/Versions/5.0.0.0/src/Xamarin.Mac/ObjCRuntime/RuntimeOptions.cs:197 
  at ObjCRuntime.RuntimeOptions.GetHttpMessageHandler () [0x00001] in /Library/Frameworks/Xamarin.Mac.framework/Versions/5.0.0.0/src/Xamarin.Mac/ObjCRuntime/RuntimeOptions.cs:217 
  at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke(System.Reflection.MonoMethod,object,object[],System.Exception&)
  at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in /Library/Frameworks/Xamarin.Mac.framework/Versions/5.0.0.0/src/Xamarin.Mac/mcs/class/corlib/System.Reflection/MonoMethod.cs:305 
   --- End of inner exception stack trace ---
  at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00043] in /Library/Frameworks/Xamarin.Mac.framework/Versions/5.0.0.0/src/Xamarin.Mac/mcs/class/corlib/System.Reflection/MonoMethod.cs:313 
  at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in /Library/Frameworks/Xamarin.Mac.framework/Versions/5.0.0.0/src/Xamarin.Mac/mcs/class/referencesource/mscorlib/system/reflection/methodbase.cs:229 
  at System.Net.Http.HttpClient.GetDefaultHandler () [0x00016] in /Library/Frameworks/Xamarin.Mac.framework/Versions/5.0.0.0/src/Xamarin.Mac/mcs/class/System.Net.Http/System.Net.Http/HttpClient.mac.cs:20 
  at System.Net.Http.HttpClient..ctor () [0x00000] in /Library/Frameworks/Xamarin.Mac.framework/Versions/5.0.0.0/src/Xamarin.Mac/mcs/class/System.Net.Http/System.Net.Http/HttpClient.mac.cs:10 
  at Codice.Client.cm.MainClass.Main (System.String[] args) [0x00001] in /Users/jesus/wkspaces/wkLocal/01plastic/src/client/cm/Main.cs:26 

Apparently, the references in 1) and 2) are exactly the same, and also the target framework. But something must be different, because 1) works fine but 2) fails.

I tried to isolate the issue in a simple console application (3), but it works fine. So I'm a little bit stucked.

Someone could give a clue about what is happening from the stacktrace?

Thank you very much for your help.

Best Answers

Answers

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    Can you post the source to your console XM application? How are you starting up Xamarin.Mac?

  • danipendanipen USMember ✭✭
    edited May 31

    This is my entry point.

        [STAThread]
        public static int Main(string[] args)
        {
            // some stuff
            System.Net.Http.HttpClient client = new System.Net.Http.HttpClient(); // at this point it fails
            // some other stuff
         }
    

    I'm not really starting up Xamarin.Mac in any way. Maybe this is the problem. Could be that I didn't needed Xamarin.Mac previousy but now that I use System.Net.Http.HttpClient, may I need it? In that case, could you provide an example about how to start it up?

  • danipendanipen USMember ✭✭
    edited June 4

    Sorry for the delayed anwswer, Chris.

    I reviewed my code, and we realized that we ARE creating the console app using mpp.

    So, ¿Do I still need to do the magic and setup Xamarin.Mac as you pointed below, because it's a console app? or ¿Should Xamarin.Mac correctly setup when I create a console app using mpp?

    Thanks for your help.

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    If you are using mmp, then you just need NSApplication.Init () as the first line of your main.

    It is undefined behavior to invoke any code that could invoke Cocoa without it.

  • danipendanipen USMember ✭✭
    edited June 4

    Chris, thanks for your anwser.

    Our console application is shared between windows, linux and macOS.

    So, it's equivalent to do?:

                var xm = Assembly.LoadFile (Path.Combine (Path.GetDirectoryName (typeof (MainClass).Assembly.Location), "Stuff", "Xamarin.Mac.dll"));
                xm.GetType ("AppKit.NSApplication").GetMethod ("Init").Invoke (null, new object[] { });
                var o = xm.GetType ("Foundation.NSObject").GetConstructor (new Type[] { }).Invoke (new object[] { });
    

    And:
    NSApplication.Init()

    The first snippet would compile in all platforms, but the second one, no, because we're not directly referencing Xamarin.Mac.dll in the main project compilation. Of course, we can make the trick with conditional compilation, or other technique, just for curiosity.

Sign In or Register to comment.