Beispiel #1
0
        public GetUserPhotoResponseMessageType EndGetUserPhoto(IAsyncResult asyncResult)
        {
            object[] array;
            try
            {
                array = base.EndInvoke(asyncResult);
            }
            catch (WebException ex)
            {
                this.WriteTracesCollectedByRemoteServerOntoLocalTracer(PhotosDiagnostics.Instance.ReadGetUserPhotoTracesFromResponse(ex.Response));
                throw;
            }
            GetUserPhotoResponseMessageType getUserPhotoResponseMessageType = (GetUserPhotoResponseMessageType)array[0];

            this.WriteTracesCollectedByRemoteServerOntoLocalTracer(PhotosDiagnostics.Instance.ReadGetUserPhotoTracesFromResponseHeaders(base.ResponseHttpHeaders));
            if (getUserPhotoResponseMessageType.ResponseClass != ResponseClassType.Success)
            {
                this.tracer.TraceError((long)this.GetHashCode(), "Request to remote service FAILED.  Returning HTTP 500: Internal Server Error.");
                getUserPhotoResponseMessageType.StatusCode = HttpStatusCode.InternalServerError;
                return(getUserPhotoResponseMessageType);
            }
            if (!getUserPhotoResponseMessageType.HasChanged)
            {
                getUserPhotoResponseMessageType.StatusCode = HttpStatusCode.NotModified;
            }
            else
            {
                getUserPhotoResponseMessageType.StatusCode = HttpStatusCode.OK;
            }
            string text;

            if (!base.ResponseHttpHeaders.TryGetValue("ETag", out text) || string.IsNullOrEmpty(text))
            {
                this.tracer.TraceDebug((long)this.GetHashCode(), "Remote service returned NO ETag or ETag is blank.");
            }
            getUserPhotoResponseMessageType.CacheId = text;
            string text2;

            if (!base.ResponseHttpHeaders.TryGetValue("Expires", out text2) || string.IsNullOrEmpty(text2))
            {
                this.tracer.TraceDebug((long)this.GetHashCode(), "Remote service returned NO Expires header or header is blank.");
            }
            getUserPhotoResponseMessageType.Expires = text2;
            this.tracer.TraceDebug((long)this.GetHashCode(), "Returning message from proxy with cacheId: {0}; HTTP status: {1};  HTTP Expires: {2};  Content-type: '{3}'", new object[]
            {
                getUserPhotoResponseMessageType.CacheId,
                getUserPhotoResponseMessageType.StatusCode,
                getUserPhotoResponseMessageType.Expires,
                getUserPhotoResponseMessageType.ContentType
            });
            return(getUserPhotoResponseMessageType);
        }
Beispiel #2
0
        public GetUserPhotoResponseMessageType EndGetUserPhoto(IAsyncResult asyncResult)
        {
            ArgumentValidator.ThrowIfNull("asyncResult", asyncResult);
            GetUserPhotoResponseMessageType result;

            try
            {
                using (HttpWebResponse httpWebResponse = (HttpWebResponse)this.httpPhotoRequest.EndGetResponse(asyncResult))
                {
                    this.WriteTracesCollectedByRemoteServerOntoLocalTracer(httpWebResponse);
                    HttpStatusCode statusCode = httpWebResponse.StatusCode;
                    if (statusCode <= HttpStatusCode.NotModified)
                    {
                        if (statusCode == HttpStatusCode.OK || statusCode == HttpStatusCode.NotModified)
                        {
                            goto IL_98;
                        }
                    }
                    else if (statusCode == HttpStatusCode.NotFound)
                    {
                        goto IL_98;
                    }
                    this.tracer.TraceError <HttpStatusCode, string>((long)this.GetHashCode(), "REST service proxy: request to remote service FAILED.  Returning HTTP {0}: {1}", httpWebResponse.StatusCode, httpWebResponse.StatusDescription);
                    return(new GetUserPhotoResponseMessageType
                    {
                        StatusCode = httpWebResponse.StatusCode
                    });

IL_98:
                    using (Stream responseStream = httpWebResponse.GetResponseStream())
                    {
                        using (MemoryStream memoryStream = new MemoryStream())
                        {
                            string text  = httpWebResponse.Headers[HttpResponseHeader.ETag];
                            string text2 = httpWebResponse.Headers[HttpResponseHeader.Expires];
                            this.tracer.TraceDebug((long)this.GetHashCode(), "REST service proxy: returning photo from proxy.  HTTP status: {0};  ETag: {1};  Expires: {2};  Content-Length: {3};  Content-Type: {4}", new object[]
                            {
                                httpWebResponse.StatusCode,
                                text,
                                text2,
                                httpWebResponse.ContentLength,
                                httpWebResponse.ContentType
                            });
                            responseStream.CopyTo(memoryStream);
                            result = new GetUserPhotoResponseMessageType
                            {
                                StatusCode  = httpWebResponse.StatusCode,
                                CacheId     = text,
                                Expires     = text2,
                                HasChanged  = (httpWebResponse.StatusCode != HttpStatusCode.NotModified),
                                PictureData = memoryStream.ToArray(),
                                ContentType = httpWebResponse.ContentType
                            };
                        }
                    }
                }
            }
            catch (WebException ex)
            {
                this.WriteTracesCollectedByRemoteServerOntoLocalTracer(ex.Response);
                HttpStatusCode httpStatusCodeFromWebException = RestService.GetHttpStatusCodeFromWebException(ex);
                this.tracer.TraceDebug <HttpStatusCode>((long)this.GetHashCode(), "REST service proxy: caught WebException and translated it to HTTP {0}", httpStatusCodeFromWebException);
                HttpStatusCode httpStatusCode = httpStatusCodeFromWebException;
                if (httpStatusCode != HttpStatusCode.NotModified)
                {
                    if (httpStatusCode != HttpStatusCode.NotFound)
                    {
                        if (httpStatusCode != HttpStatusCode.InternalServerError)
                        {
                            throw;
                        }
                        result = new GetUserPhotoResponseMessageType
                        {
                            StatusCode = HttpStatusCode.InternalServerError
                        };
                    }
                    else
                    {
                        result = new GetUserPhotoResponseMessageType
                        {
                            Expires    = RestService.GetHeaderValueFromWebException(ex, HttpResponseHeader.Expires),
                            StatusCode = HttpStatusCode.NotFound
                        };
                    }
                }
                else
                {
                    result = new GetUserPhotoResponseMessageType
                    {
                        StatusCode = HttpStatusCode.NotModified,
                        CacheId    = RestService.GetHeaderValueFromWebException(ex, HttpResponseHeader.ETag),
                        Expires    = RestService.GetHeaderValueFromWebException(ex, HttpResponseHeader.Expires),
                        HasChanged = false
                    };
                }
            }
            return(result);
        }