public void Execute()
        {
            try
            {
                var m = new ServiceManager <GetImageCompletedEventArgs>(delegate(BodyArchitectAccessServiceClient client1, EventHandler <GetImageCompletedEventArgs> operationCompleted)
                {
                    using (OperationContextScope ocs = new OperationContextScope(client1.InnerChannel))
                    {
                        OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("SessionId", "http://MYBASERVICE.TK/", ApplicationState.Current.SessionData.Token.SessionId));
                        OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("PictureId", "http://MYBASERVICE.TK/", picture.PictureIdk__BackingField));
                        OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("Hash", "http://MYBASERVICE.TK/", picture.Hashk__BackingField));
                        ApplicationState.AddCustomHeaders();
                        client1.GetImageCompleted -= operationCompleted;
                        client1.GetImageCompleted += operationCompleted;
                        client1.GetImageAsync();
                    }
                });
                m.OperationCompleted += (s, a) =>
                {
                    if (a.Error != null)
                    {
                        PictureCacheItem item = new PictureCacheItem(null, picture.PictureIdk__BackingField, picture.Hashk__BackingField);
                        PicturesCache.Instance.AddToCache(item);
                        PicturesCache.Instance.Notify(new PictureInfoDTO()
                        {
                            PictureIdk__BackingField = picture.PictureIdk__BackingField, Hashk__BackingField = picture.Hashk__BackingField
                        });
                    }
                    else if (a.Result != null)
                    {
                        MemoryStream  stream = new MemoryStream(a.Result.Result);
                        ExtendedImage desert = new ExtendedImage();
                        desert.SetSource(stream);
                        desert.LoadingFailed += delegate(object sender, UnhandledExceptionEventArgs e)
                        {
                            PictureCacheItem item = new PictureCacheItem(null, picture.PictureIdk__BackingField, picture.Hashk__BackingField);
                            PicturesCache.Instance.AddToCache(item);
                            PicturesCache.Instance.Notify(new PictureInfoDTO()
                            {
                                PictureIdk__BackingField = picture.PictureIdk__BackingField, Hashk__BackingField = picture.Hashk__BackingField
                            });
                        };
                        desert.LoadingCompleted += delegate
                        {
                            ApplicationState.SynchronizationContext.Post(delegate
                            {
                                var image             = desert.ToBitmap();
                                PictureCacheItem item = new PictureCacheItem(image, picture.PictureIdk__BackingField, picture.Hashk__BackingField);
                                PicturesCache.Instance.AddToCache(item);
                                PicturesCache.Instance.Notify(picture);
                                //if (OperationCompleted != null)
                                //{
                                //    OperationCompleted(this, EventArgs.Empty);
                                //}
                            }, null);
                        };
                    }
                };

                m.Run();
            }
            catch (Exception)
            {
                PictureCacheItem item = new PictureCacheItem(null, picture.PictureIdk__BackingField, picture.Hashk__BackingField);
                PicturesCache.Instance.AddToCache(item);
                PicturesCache.Instance.Notify(new PictureInfoDTO()
                {
                    PictureIdk__BackingField = picture.PictureIdk__BackingField, Hashk__BackingField = picture.Hashk__BackingField
                });
            }
        }
Beispiel #2
0
        private void uploadPictureAndSaveProfile()
        {
            var m = new ServiceManager <AsyncCompletedEventArgs>(delegate(BodyArchitectAccessServiceClient client1, EventHandler <AsyncCompletedEventArgs> operationCompleted)
            {
                using (OperationContextScope ocs = new OperationContextScope(client1.InnerChannel))
                {
                    var ggg        = (IBodyArchitectAccessService)client1;
                    PictureDTO dto = new PictureDTO();

                    WriteableBitmap _bitmap = new WriteableBitmap(picture);
                    var extImage            = _bitmap.ToImage();
                    var st = (MemoryStream)extImage.ToStream();
                    st.Seek(0, SeekOrigin.Begin);
                    dto.ImageStream = st.ToArray();
                    OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("SessionId", "http://MYBASERVICE.TK/", ApplicationState.Current.SessionData.Token.SessionId));
                    OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("PictureId", "http://MYBASERVICE.TK/", PictureId));
                    OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("Hash", "http://MYBASERVICE.TK/", "test"));
                    ApplicationState.AddCustomHeaders();

                    ggg.BeginUploadImage(dto, delegate(IAsyncResult aRes)
                    {
                        var proxy   = (IBodyArchitectAccessService)aRes.AsyncState;
                        string hash = null;
                        using (OperationContextScope o = new OperationContextScope(((BodyArchitectAccessServiceClient)proxy).InnerChannel))
                        {
                            try
                            {
                                proxy.EndUploadImage(aRes);
                            }
                            catch (Exception ex)
                            {
                                BugSenseHandler.Instance.SendExceptionAsync(ex);
                                Dispatcher.BeginInvoke(delegate
                                {
                                    progressBar.ShowProgress(false);
                                    ApplicationBar.EnableApplicationBar(true);
                                    BAMessageBox.ShowError(ApplicationStrings.ErrUploadPhoto);
                                });

                                return;
                            }

                            hash = OperationContext.Current.IncomingMessageHeaders.GetHeader <string>("Hash", "http://MYBASERVICE.TK/");
                        }

                        saveProfile(hash, _bitmap);
                    }, client1);
                }
            });

            progressBar.ShowProgress(true, ApplicationStrings.ProfilePage_ProgressUploadPhoto);
            ApplicationBar.EnableApplicationBar(false);
            if (!m.Run())
            {
                progressBar.ShowProgress(false);
                if (ApplicationState.Current.IsOffline)
                {
                    BAMessageBox.ShowError(ApplicationStrings.ErrOfflineMode);
                }
                else
                {
                    BAMessageBox.ShowError(ApplicationStrings.ErrNoNetwork);
                }
            }
        }