2017-04-23 45 views
0

在我的網頁,我需要搜索名稱,類型容器和斑點和上次更改天青IListBlobItem Oftype CloudBlob LINQ的更改列表<CloudBlob>

var selectedValue = ddlFileType.SelectedValue; 
     AzureSettings container = AzureSettingsServices.Select(selectedValue.ParseInt(-1)); 
     if (ViewState[container.Name] == null) 
     { 
      IEnumerable<IListBlobItem> blobList = BlobHelper.GetFileListByContainer(container.Name); 

      //I add the viewstate for not spending money in azure :) 
      ViewState.Add(container.Name, blobList); 

     } 
     List<CloudBlob> list = null; 
     string fileName = txtFileName.Text; 
     if (!string.IsNullOrWhiteSpace(fileName)) 
     { 
      //by name and date 
      list = ((IEnumerable<IListBlobItem>)ViewState[container.Name]).OfType<CloudBlob>().Where(x => x.Name == fileName && x.Properties.LastModified >= dtDate.ValueRange.StartDate && x.Properties.LastModified <= dtDate.ValueRange.EndDate).ToList(); 
     } 
     else if (string.IsNullOrWhiteSpace(fileName)) 
     { 
      //by date 
      list = ((IEnumerable<IListBlobItem>)ViewState[container.Name]).OfType<CloudBlob>().Where(x => x.Properties.LastModified >= dtDate.ValueRange.StartDate && x.Properties.LastModified <= dtDate.ValueRange.EndDate).ToList(); 
     } 
     if (list != null) 
     { 
      // by type 
      list=list.OfType<CloudBlob>().Where(x => x.Name.Contains(selectedValue)).ToList(); 

      SelectedContainer = container.Name; 
      grdFiles.DataSource = list; 
      grdFiles.DataBind(); 
     } 

的問題是我不能給列表< CloudBlob>列表,gridview的數據源這是正常的,但我怎麼能設置到名單< CloudBlob>列表中 IEnumerable的< IListBlobItem> bloblist或在我的視圖狀態(注:在我的視圖狀態是我Ilistblobitem名單)回我的gridview的數據源

UPDATE ERROR 2

grdFiles.DataSource =((IEnumerable的)的ViewState [container.Name]);

grdFiles.DataBind();

下面是有關該錯誤,

'Microsoft.WindowsAzure.Storage,版本= 7.2.1.0,文化=中性公鑰= 31bf3856ad364e35'「Microsoft.WindowsAzure.Storage.Core.Util.CommonUtility + d__0`1 [[Microsoft.WindowsAzure.Storage.Blob.IListBlobItem,Microsoft.WindowsAzure.Storage,版本= 7.2.1.0,文化=中性公鑰= 31bf3856ad364e35]]」

> [SerializationException: 'Microsoft.WindowsAzure.Storage, Version=7.2.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' Derlemesindeki 'Microsoft.WindowsAzure.Storage.Core.Util.CommonUtility+<LazyEnumerable>d__0`1[[Microsoft.WindowsAzure.Storage.Blob.IListBlobItem, Microsoft.WindowsAzure.Storage, Version=7.2.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]' ] 

系統。 Runtime.Serializatio n.FormatterServices.InternalGetSerializableMembers(RuntimeType型)12207601個 System.Runtime.Serialization.FormatterServices.GetSerializableMembers(類型類型,的StreamingContext上下文)230 System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo()143

System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(對象OBJ,ISurrogateSelector surrogateSelector,的StreamingContext上下文,serObjectInfoInit serObjectInfoInit,IFormatterConverter來轉換器,objectWriter objectWriter,SerializationBinder粘合劑)178 System.Runtime.Serialization.Formatters.Binary。 WriteObjectInfo.Serialize(Object obj,ISurrogateSelector surrogateSelector,StreamingContext上下文,SerObjectInfoInit serObjectInfoInit,IFormatterConverter轉換器,Objec tWriter objectWriter,SerializationBinder聯編程序)+512 System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph,Header [] inHeaders,__BinaryWriter serWriter,Boolean fCheck)+540 System.Runtime.Serialization.Formatters.Binary。 BinaryFormatter.Serialize(Stream serializationStream,Object graph,Header [] headers,Boolean fCheck)+13 System.Web.UI。System.Web.UI.Serialize.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream,Object graph)+17 System.Web.UI。 ObjectStateFormatter.SerializeValue(SerializerBinaryWriter作家,對象的值)3046

[ArgumentException的: 'Microsoft.WindowsAzure.Storage.Core.Util.CommonUtility + d__0 1[[Microsoft.WindowsAzure.Storage.Blob.IListBlobItem, Microsoft.WindowsAzure.Storage, Version=7.2.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]' türündeki 'Microsoft.WindowsAzure.Storage.Core.Util.CommonUtility+<LazyEnumerable>d__0 1 [Microsoft.WindowsAzure.Storage.Blob.IListBlobItem]' 去erini斯里健壯getirmehatası。] System.Web.UI.ObjectStateFormatter.SerializeValue(SerializerBinaryWriter作家,對象的值)3770 System.Web.UI.ObjectStateFormatter.Serialize(流的OutputStream,對象stateGraph)144 的System.Web。 UI.ObjectStateFormatter.Serialize(對象狀態圖,目的用途)+71 System.Web.UI。ObjectStateFormatter.System.Web.UI.IStateFormatter.Serialize(Object state)+39 System.Web.UI.Util.SerializeWithAssert(IStateFormatter formatter,Object stateGraph)+37 System.Web.UI.Control.EstimateStateSize(Object state) +45 System.Web.UI.Control.BuildProfileTree(String parentId,Boolean calcViewState)+71 System.Web.UI.Page.BuildPageProfileTree(Boolean enableViewState)+42 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint ,布爾includeStagesAfterAsyncPoint)+5761

+0

我發現錯誤,IEnumerable的無法添加到視圖狀態 –

回答

1

正如你所說的IEnumerable不能添加到視圖狀態並且不能成爲gridview的數據源。

據我所知,IListBlobItem是一個接口。IEnumerable包含三種類型的blob項目。

CloudBlockBlob,CloudPageBlob,CloudBlobDirectory 

因此,我建議你可以先檢查類型,並轉換爲數據表或別的東西,然後將其添加到ViewState中。

象下面這樣:

DataTable d1 = new DataTable(); 
      d1.Columns.Add("Type"); 
      d1.Columns.Add("Id"); 
      d1.Columns.Add("Url"); 
      foreach (var item in blobs) 
      { 
       if (item.GetType() == typeof(CloudBlockBlob)) 
       { 
        CloudBlockBlob blob = (CloudBlockBlob)item; 

        d1.Rows.Add("CloudBlockBlob", blob.Name, blob.Uri); 
       } 
       else if (item.GetType() == typeof(CloudPageBlob)) 
       { 
        CloudPageBlob blob = (CloudPageBlob)item; 
        d1.Rows.Add("CloudPageBlob", blob.Name, blob.Uri); 
       } 
       else if (item.GetType() == typeof(CloudBlobDirectory)) 
       { 
        CloudBlobDirectory blob = (CloudBlobDirectory)item; 
        d1.Rows.Add("directory", blob.StorageUri, blob.Uri); 
       } 

      } 
      ViewState.Add(container.Name, d1); 

結果: enter image description here


將更好的ViewState或外殼我nonpostback和回傳再去蔚藍的名單?

據我所知,你需要支付azure中的列表blob($ 0.10 LRS Cold per 10,000)。所以我建議你可以使用viewstate來存儲listblob操作的結果,如果你不需要更新blob的列表的話。


我該如何處理的SAS失效在我的asp.net也將超過1000個用戶。當我刪除blobContainer.GetPermissions();並創建一個新的並設置它。另一個上傳的用戶將失敗,因爲我爲另一個用戶創建了一個新的sas。

據我所知,如果你刪除了blobContainer的權限,它會刪除SAS訪問策略。

然後,根據此SAS訪問策略創建的所有SAS令牌都將無用。

通常情況下,我們可以創建一個無過期日期的訪問策略。

然後,你可以根據這個訪問策略,當你創建面向特定用戶標識的URL指定到期日。

的代碼是象下面這樣:

SharedAccessPolicy sharedAccessPolicy = new SharedAccessPolicy(); 
    sharedAccessPolicy.Permissions = SharedAccessPermissions.Read; 
    sharedAccessPolicy.SharedAccessStartTime = DateTime.UtcNow; 
    //sharedAccessPolicy.SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(1); No need to define expiry time here. 

    BlobContainerPermissions blobContainerPermissions = new BlobContainerPermissions(); 
    blobContainerPermissions.SharedAccessPolicies.Add("default", sharedAccessPolicy); 

    container.SetPermissions(blobContainerPermissions); 

    Console.WriteLine("Press any key to continue...."); 
    Console.ReadLine(); 
    CloudBlob blob = container.GetBlobReference(path); 

    string sas = blob.GetSharedAccessSignature(new SharedAccessPolicy() 
    { 
     SharedAccessExpiryTime = DateTime.UtcNow.AddDays(7),//add expiry date only when you're creating the signed URL 
    } 
     , "default"); 

    Console.WriteLine(blob.Uri.AbsoluteUri + sas); 

    Process.Start(new ProcessStartInfo(blob.Uri.AbsoluteUri + sas)); 

    Console.WriteLine("Press any key to continue...."); 
    Console.ReadLine(); 

此外,我們可以創建5准入政策,所以我建議你可以創建不同的訪問策略,並創建不同的SAS令牌針對不同的用戶。

+0

嗯..是你的權利,新的DataTable是一個好主意。謝謝你的建議,你最好:)會更好地查看狀態或shell我再次在nonpostback和回發列表的天藍色列表? –

+0

我可以問第二個和最後一個問題:)如何處理我的asp.net中的sas過期,它將超過1000個用戶..當我刪除blobContainer.GetPermissions();並創建一個新的並設置它。另一個上傳的用戶將失敗,因爲我爲另一個用戶創建了一個新的sas。 –

+0

更新答案 –