private void Create(String name, Guid typeId)
        {
            AdminSupportClient adminClient = new AdminSupportClient(Properties.Settings.Default.AdminSupportEndpoint);
            Guid organizationId            = Guid.Empty;

            try
            {
                AdminSupportReference.MethodResponseguid response;
                String currentOrganization;

                lock (DataModel.SyncRoot)
                    currentOrganization = DataModel.User.UserKey.Find(UserContext.Instance.UserId).TenantRow.ExternalId0;

                response = adminClient.AddOrganization(name, currentOrganization);
                adminClient.Close();

                if (!response.IsSuccessful)
                {
                    if (response.Errors.Length > 0)
                    {
                        GuardianObject.ThrowErrorInfo(response.Errors[0]);
                    }
                    else
                    {
                        throw new Exception("Unknown error occured");
                    }
                }
                else
                {
                    organizationId = response.Result;
                }
            }
            catch
            {
                MessageBox.Show(Properties.Resources.OperationFailed);
                return;
            }

            try
            {
                Guid parentId;

                lock (DataModel.SyncRoot)
                    parentId = FindUserFolder();

                Guid entityId = Entity.Create(typeId, parentId, organizationId);
                TradingSupportReference.TradingSupportClient client = new TradingSupportReference.TradingSupportClient(Properties.Settings.Default.TradingSupportEndpoint);
                client.UpdateEntity(new TradingSupportReference.Entity[] { new TradingSupportReference.Entity()
                                                                           {
                                                                               RowId = entityId, Name = name
                                                                           } });
            }
            catch
            {
                MessageBox.Show(Properties.Resources.OperationFailed);
                return;
            }
        }
        private void ExecuteSlice(object state)
        {
            List <WorkingOrderRow> workingOrders = state as List <WorkingOrderRow>;

            try
            {
                List <TradingSupportReference.DestinationOrderInfo> destinationOrders = new List <TradingSupportReference.DestinationOrderInfo>();

                lock (DataModel.SyncRoot)
                {
                    DestinationRow destinationRow = DataModel.Destination.DestinationKeyExternalId0.Find("GUARDIAN ECN");

                    foreach (WorkingOrderRow workingOrderRow in workingOrders)
                    {
                        decimal sourceOrderQuantity = 0.0M;
                        foreach (SourceOrderRow sourceOrderRow in workingOrderRow.GetSourceOrderRows())
                        {
                            sourceOrderQuantity += sourceOrderRow.OrderedQuantity;
                        }

                        Decimal destinationOrderQuantity = 0.0M;
                        foreach (DestinationOrderRow destinationOrderRow in workingOrderRow.GetDestinationOrderRows())
                        {
                            destinationOrderQuantity += destinationOrderRow.OrderedQuantity;
                        }

                        if (sourceOrderQuantity > destinationOrderQuantity)
                        {
                            TradingSupportReference.DestinationOrderInfo destinationOrderInfo = new TradingSupportReference.DestinationOrderInfo();
                            destinationOrderInfo.BlotterId         = workingOrderRow.BlotterId;
                            destinationOrderInfo.DestinationId     = destinationRow.DestinationId;
                            destinationOrderInfo.OrderedQuantity   = sourceOrderQuantity - destinationOrderQuantity;
                            destinationOrderInfo.OrderTypeId       = workingOrderRow.OrderTypeId;
                            destinationOrderInfo.SecurityId        = workingOrderRow.SecurityId;
                            destinationOrderInfo.SettlementId      = workingOrderRow.SettlementId;
                            destinationOrderInfo.SideCodeId        = workingOrderRow.SideId;
                            destinationOrderInfo.TimeInForceCodeId = workingOrderRow.TimeInForceId;
                            destinationOrderInfo.WorkingOrderId    = workingOrderRow.WorkingOrderId;

                            destinationOrders.Add(destinationOrderInfo);
                        }
                    }
                }

                // Create a channel to the middle tier.
                TradingSupportReference.TradingSupportClient tradingSupportClient = new TradingSupportReference.TradingSupportClient(Guardian.Properties.Settings.Default.TradingSupportEndpoint);
                tradingSupportClient.CreateDestinationOrders(destinationOrders.ToArray());

                // At this point the client can be shut down gracefully.
                tradingSupportClient.Close();
            }
            catch (FaultException <OptimisticConcurrencyFault> optimisticConcurrencyException)
            {
                // The record is busy.
                this.Dispatcher.Invoke(DispatcherPriority.Normal,
                                       (MessageDelegate)((string message) => { MessageBox.Show(message, Application.Current.MainWindow.Title); }),
                                       String.Format(FluidTrade.Core.Properties.Resources.OptimisticConcurrencyError,
                                                     optimisticConcurrencyException.Detail.TableName));
            }
            catch (FaultException <RecordNotFoundFault> recordNotFoundException)
            {
                // The record is busy.
                this.Dispatcher.Invoke(DispatcherPriority.Normal,
                                       (MessageDelegate)((string message) => { MessageBox.Show(message, Application.Current.MainWindow.Title); }),
                                       String.Format(FluidTrade.Core.Properties.Resources.RecordNotFoundError,
                                                     CommonConversion.FromArray(recordNotFoundException.Detail.Key),
                                                     recordNotFoundException.Detail.TableName));
            }
            catch (CommunicationException communicationException)
            {
                // Log communication problems.
                this.Dispatcher.Invoke(DispatcherPriority.Normal,
                                       (MessageDelegate)((string message) => { MessageBox.Show(message, Application.Current.MainWindow.Title); }),
                                       communicationException.Message);
            }
            catch (Exception exception)
            {
                // Log communication problems.
                EventLog.Error("{0}, {1}", exception.Message, exception.StackTrace);
            }
        }