Beispiel #1
0
        /// <summary>
        /// Creates a new web request and returns the result as a task.
        /// </summary>
        /// <param name="methodProperties"> The method properties. </param>
        /// <returns> The created <see cref="Task"/>. </returns>
        private Task <WebResponse> CreateRequest(MethodMetadata methodProperties)
        {
            WebRequest request = methodProperties.CreateRequest(
                this.BaseUri, this.Credentials, this.MediaRegistry);

            // Raise event to callers that the request has been created
            this.OnBeforeSend(new WebRequestEventArgs(request));

            return(Task <WebResponse> .Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, null));
        }
Beispiel #2
0
        /// <summary>
        /// Executes a service request based on metadata provided by the given <see cref="MethodInfo"/>,
        /// and supplied runtime arguments.
        /// </summary>
        /// <param name="methodProperties"> The details about the method to invoke. </param>
        /// <typeparam name="T"> The type to be returned by the service call </typeparam>
        /// <returns> The result of calling the service. </returns>
        protected T SyncRequestWithResult <T>(MethodMetadata methodProperties)
        {
            IMediaTypeHandler handler;

            if (!this.MediaRegistry.TryGetHandler(typeof(T), methodProperties.Consumes, out handler))
            {
                throw new EasyPeasyException(methodProperties.Produces + " does not have a valid handler");
            }

            WebResponse response = SyncRequestWithRawResponse(methodProperties);

            return((T)handler.ReadObject(response, response.GetResponseStream(), typeof(T)));
        }
Beispiel #3
0
        /// <summary>
        /// Executes a service request based on metadata provided by the given <see cref="MethodInfo"/>,
        /// and supplied runtime arguments.
        /// </summary>
        /// <param name="methodProperties"> The details about the method to invoke. </param>
        /// <returns> The raw web response. </returns>
        protected WebResponse SyncRequestWithRawResponse(MethodMetadata methodProperties)
        {
            Task <WebResponse> task = CreateRequest(methodProperties);

            if (!task.Wait(Timeout))
            {
                throw new TimeoutException();
            }

            CheckTaskForException(task);
            this.OnResponseReceived(new WebResponseEventArgs(task.Result));

            return(task.Result);
        }
Beispiel #4
0
        /// <summary>
        /// Executes a service request based on metadata provided by the given <see cref="MethodInfo"/>,
        /// and supplied runtime arguments.
        /// </summary>
        /// <param name="methodProperties"> The details about the method to invoke. </param>
        protected void SyncVoidRequest(MethodMetadata methodProperties)
        {
            Task <WebResponse> task = CreateRequest(methodProperties);

            if (!task.Wait(Timeout))
            {
                throw new TimeoutException();
            }

            CheckTaskForException(task);
            if (task.IsCompleted)
            {
                this.OnResponseReceived(new WebResponseEventArgs(task.Result));
            }
        }
Beispiel #5
0
        /// <summary>
        /// Executes a service request based on metadata provided by the given <see cref="MethodInfo"/>,
        /// and supplied runtime arguments.
        /// </summary>
        /// <param name="methodProperties"> The details about the method to invoke. </param>
        /// <typeparam name="T"> The type to be returned by the service call </typeparam>
        /// <returns> The <see cref="Task"/> which when run to completion, returns the result of
        /// calling the service. </returns>
        protected Task <T> AsyncRequestWithResult <T>(MethodMetadata methodProperties)
        {
            IMediaTypeHandler handler;

            if (!this.MediaRegistry.TryGetHandler(typeof(T), methodProperties.Consumes, out handler))
            {
                throw new EasyPeasyException(methodProperties.Produces + " does not have a valid handler");
            }

            Task <WebResponse> task = CreateRequest(methodProperties);

            return(task.ContinueWith(t =>
            {
                CheckTaskForException(t);
                this.OnResponseReceived(new WebResponseEventArgs(task.Result));
                return (T)handler.ReadObject(t.Result, t.Result.GetResponseStream(), typeof(T));
            }));
        }