protected override void Process(TypeDefinition type)
        {
            if (!type.Is("ObjCRuntime", "RuntimeOptions"))
            {
                return;
            }

            MethodDefinition method = type.Methods.First(x => x.Name == "GetHttpMessageHandler" && !x.HasParameters);

            AssemblyDefinition systemNetHTTPAssembly = context.GetAssemblies().First(x => x.Name.Name == "System.Net.Http");
            TypeDefinition     handler      = RuntimeOptions.GetHttpMessageHandler(App, LinkContext.Target.LinkerOptions.RuntimeOptions, systemNetHTTPAssembly.MainModule, type.Module);
            MethodReference    handler_ctor = handler.Methods.First(x => x.IsConstructor && !x.HasParameters && !x.IsStatic);

            // HttpClientHandler is defined in System.Net.Http.dll so we need to import
            if (handler.Name.Contains("HttpClientHandler"))
            {
                handler_ctor = type.Module.ImportReference(handler_ctor);
            }

            var body = new MethodBody(method);
            var il   = body.GetILProcessor();

            il.Emit(OpCodes.Newobj, handler_ctor);
            il.Emit(OpCodes.Ret);
            method.Body = body;
        }
示例#2
0
        protected override void Process(TypeDefinition type)
        {
            if (!type.Is("System.Net.Http", "HttpClient"))
            {
                return;
            }

            MethodDefinition default_ctor = null;
            MethodDefinition full_ctor    = null;

            foreach (var m in type.Methods)
            {
                if (m.IsStatic || !m.IsConstructor)
                {
                    continue;
                }
                if (!m.HasParameters)
                {
                    default_ctor = m;
                }
                else if (m.Parameters.Count == 2)
                {
                    full_ctor = m;
                }
            }

            if (default_ctor == null || full_ctor == null)
            {
                throw new Exception("Could not set the default HttpMessageHandler");
            }

            var handler = RuntimeOptions.GetHttpMessageHandler(Options.Application, Options.RuntimeOptions, type.Module);

            MethodDefinition handler_ctor = null;

            foreach (var m in handler.Methods)
            {
                if (m.IsStatic || !m.IsConstructor || m.HasParameters)
                {
                    continue;
                }
                handler_ctor = m;
                break;
            }
            // re-write default ctor
            var body = new MethodBody(default_ctor);
            var il   = body.GetILProcessor();

            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Newobj, handler_ctor);
            il.Emit(OpCodes.Ldc_I4_1);
            il.Emit(OpCodes.Call, full_ctor);
            il.Emit(OpCodes.Ret);
            default_ctor.Body = body;
        }