public void Load(string connectionString, Action <StorageAccount> onLoaded)
        {
            account = CloudStorageAccount.Parse(connectionString);
            client  = account.CreateCloudBlobClient();

            Containers.Clear();

            Task.Run(() => client.ListContainers())
            .ContinueWith(r =>
            {
                if (!r.IsCompleted)
                {
                    return;
                }

                Containers.AddRange(r.Result);
                onLoaded(this);
            });
        }
示例#2
0
        async Task <bool> ExecuteLoadContainersAsync(bool force = false)
        {
            if (IsBusy)
            {
                return(false);
            }
            var realm = App.GetRealm();

            try
            {
                IsBusy            = true;
                NoContainersFound = false;

                var cte = realm.All <RealmCloudBlobContainer>();
                if (cte.Count() > 0 && force == false)
                {
                    var storageAccounts = realm.All <StorageAccountExt>().Where(sa => sa.IsStorageAccountOn);
                    List <ASECloudBlobContainer> aseContainers = new List <ASECloudBlobContainer>();
                    if (storageAccounts.Count() > 0)
                    {
                        foreach (var container in cte)
                        {
                            StorageAccountsExist = true;
                            var storageAccount = storageAccounts.Where((arg) => arg.Name == container.StorageAccountName).FirstOrDefault();

                            if (storageAccount != null)
                            {
                                var te = new CloudBlobContainer(new Uri(container.ContainerUri),
                                                                new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(storageAccount.Name, storageAccount.PrimaryKey));
                                aseContainers.Add(new ASECloudBlobContainer(te, storageAccount.Name));
                            }
                        }
                        Containers.Clear();
                        Containers.AddRange(aseContainers);
                    }
                }
                else
                {
                    var storageAccounts = realm.All <StorageAccountExt>().Where(sa => sa.IsStorageAccountOn);

                    Containers.Clear();
                    foreach (var account in storageAccounts)
                    {
                        string connectionString = Constants.StorageConnectionString;
                        connectionString = connectionString.Replace("<ACCOUNTNAME>", account.Name);
                        connectionString = connectionString.Replace("<ACCOUNTKEY>", account.PrimaryKey);
                        CloudStorageAccount sa = CloudStorageAccount.Parse(connectionString);
                        var blobClient         = sa.CreateCloudBlobClient();
                        var containers         = await blobClient.ListContainersAsync();

                        List <ASECloudBlobContainer> aseContainers = new List <ASECloudBlobContainer>();
                        for (int i = 0; i < containers.Count; i++)
                        {
                            aseContainers.Add(new ASECloudBlobContainer(containers[i]));
                        }
                        aseContainers.All(c => { c.StorageAccountName = account.Name; return(true); });
                        Containers.AddRange(aseContainers);
                    }
                    if (storageAccounts.Count() > 0)
                    {
                        StorageAccountsExist = true;
                    }
                    else
                    {
                        StorageAccountsExist = false;
                    }

                    await realm.WriteAsync(temprealm =>
                    {
                        temprealm.RemoveAll <RealmCloudBlobContainer>();
                        foreach (var con in Containers)
                        {
                            temprealm.Add(new RealmCloudBlobContainer(con.ContainerName, con.StorageAccountName, con.BaseContainer.Uri.ToString()));
                        }
                    });

                    realm.All <RealmCloudBlobContainer>().SubscribeForNotifications((sender, changes, error) =>
                    {
                        Console.WriteLine("Change to CloudBlobContainers");
                    });
                }

                SortContainers();

                if (Containers.Count == 0)
                {
                    NoContainersFound = true;
                }
                else
                {
                    NoContainersFound = false;
                }
            }
            catch (Exception ex)
            {
                Logger.Report(ex, "Method", "ExecuteLoadContainersAsync");
                MessagingService.Current.SendMessage(MessageKeys.Error, ex);
            }
            finally
            {
                IsBusy = false;
            }
            return(true);
        }
 // Добавление дочерних контейнеров от родителя в ленивую коллекцию
 public void Adds(List <Container> containers)
 {
     Containers.AddRange(containers);
     Count += containers.Count;
 }