/// <summary>
        /// Run an async task. This method will block execution (and use the calling thread as a worker thread) until the async task has completed.
        /// </summary>
        public static void Run(Func <Task> main, int degreeOfParallelism = 1)
        {
            var asyncOperatingContext = new AsyncOperatingContext();

            asyncOperatingContext.DegreeOfParallelism = degreeOfParallelism;
            asyncOperatingContext.RunMain(main);
        }
        /// <summary>
        /// Run an async task. This method will block execution (and use the calling thread as a worker thread) until the async task has completed.
        /// </summary>
        public static T Run <T>(Func <Task <T> > main, int degreeOfParallelism = 1)
        {
            var asyncOperatingContext = new AsyncOperatingContext();

            asyncOperatingContext.DegreeOfParallelism = degreeOfParallelism;
            return(asyncOperatingContext.RunMain(main));
        }
예제 #3
0
        /// <summary>
        /// Invokes the webservice, the only twist here is an optional parameter to skip my implementation and use the old and original one. can be useful in some cases.
        /// I reapplied all the WS stuff originally found in the invoke methods and his siblongs, so all your extensions, headers and assorted ws-stuff should also work here.
        /// </summary>
        /// <param name="methodName">Method name.</param>
        /// <param name="parameters">Parameters.</param>
        /// <param name="legacyClient">If set to <c>true</c> uses the original SOAP client as implemented in Mono.</param>
        protected new object[] Invoke(string methodName, object[] parameters, bool legacyClient = false)
        {
            //skips the goodies!
            if (legacyClient)
            {
                return(base.Invoke(methodName, parameters));
            }

            try {
                object            theTypeInfoObject  = ReflectionHelper.GetFieldValue(this, "type_info");
                object            soapMethodStubInfo = ReflectionHelper.ExecuteMethod(theTypeInfoObject, "GetMethod", null, methodName);
                SoapClientMessage soapClientMessage  = (SoapClientMessage)ReflectionHelper.CreateInstance("System.Web.Services", "System.Web.Services.Protocols.SoapClientMessage", this, soapMethodStubInfo, base.Url, parameters);
                object            methodStubInfo     = ReflectionHelper.GetFieldValue(soapClientMessage, "MethodStubInfo");
                object            headers            = ReflectionHelper.GetFieldValue(methodStubInfo, "Headers");


                ReflectionHelper.ExecuteMethod(soapClientMessage, "CollectHeaders", null, this, headers, SoapHeaderDirection.In);

                object[] ClassConfiguredExtensions  = (object[])ReflectionHelper.GetFieldValue(theTypeInfoObject, "SoapExtensions");
                object[] MethodConfiguredExtensions = (object[])ReflectionHelper.GetFieldValue(soapMethodStubInfo, "SoapExtensions");

                Type[] methodTypes = new Type[] {
                    SoapExtensionRuntimeConfigArrayType,
                    SoapExtensionRuntimeConfigArrayType,
                    SoapExtensionRuntimeConfigArrayType
                };

                //extension chain
                SoapExtension[] extensions = (SoapExtension[])ReflectionHelper.ExecuteStaticMethod(SoapExtensionType, "CreateExtensionChain", methodTypes, ClassConfiguredExtensions[0], MethodConfiguredExtensions, ClassConfiguredExtensions[1]);

                //uri
                object thisUri = ReflectionHelper.GetFieldValue(this, "uri");

                //body
                string requestData = null;
                using (MemoryStream memoryStream = new MemoryStream()) {
                    SerializeRequest(memoryStream, soapClientMessage, extensions);
                    requestData = Encoding.UTF8.GetString(memoryStream.ToArray());
                }

                //HttpClient sync call
                using (HttpClientResponse clientResponse = AsyncOperatingContext.Run <HttpClientResponse>(() => {
                    HttpClientHelper.Proxy = Proxy;
                    HttpClientHelper.UserAgent = UserAgent;
                    HttpClientHelper.EnableDecompression = EnableDecompression;
                    HttpClientHelper.Timeout = Timeout;
                    return(HttpClientHelper.GetResponse((Uri)thisUri, requestData, soapClientMessage));
                })) {
                    object[] result = DeserializeResponse(clientResponse.DataStream, clientResponse.StatusCode, clientResponse.ContentType, (SoapClientMessage)soapClientMessage, (SoapExtension[])extensions);
                    return(result);
                }
            } catch (Exception ex) {
                Console.Write(ex);
                throw ex;
            }
        }