【发布时间】:2016-07-23 05:22:17
【问题描述】:
我面临的问题是,当我尝试将 byte[] 上传到 Azure blob 存储时,我收到以下异常:
错误:在程序集“System.Web”中键入“System.Web.HttpInputStream”, 版本=4.0.0.0,文化=中性,PublicKeyToken=b03f5f7f11d50a3a' 是 未标记为可序列化。
因此,我开始使用[Serializable] 标记代码所在的类,但仍然引发了相同的异常。
上传.aspx.cs:
[Serializable]
public partial class Upload : System.Web.UI.Page
{
protected void submitButton_Click(object sender, EventArgs args)
{
HttpPostedFile filePosted = Request.Files["File1"];
string fn = Path.GetFileName(filePosted.FileName);
try
{
byte[] bytes = ObjectToByteArray(filePosted.InputStream);
Share[] shares = f.split(bytes);
UploadImageServiceClient client = new UploadImageServiceClient();
client.Open();
foreach (Share share in shares)
{
byte[] serialized = share.serialize();
Response.Write("Processing upload...");
client.UploadImage(serialized);
}
client.Close();
}
catch (Exception ex)
{
Response.Write("Error: " + ex.Message);
}
}
}
我知道有类似的问题,例如 this,它解释了您无法使用 Stream 成员定义数据协定,但我的 WCF 云服务没有 Stream 或 FileStream 成员。
这是我的 WCF 服务实现:
[ServiceContract]
public interface IUploadImageService
{
[OperationContract]
void UploadImage(byte[] bytes);
}
而我的服务如下:
public void UploadImage(byte[] bytes)
{
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting(connString));
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference("test");
// Retrieve reference to a blob passed in as argument.
CloudBlockBlob blockBlob = container.GetBlockBlobReference("sample");
container.CreateIfNotExists();
try
{
blockBlob.UploadFromByteArray(bytes, 0, bytes.Length);
}
catch (StorageException ex)
{
ex.ToString();
}
}
【问题讨论】:
标签: c# asp.net wcf azure-storage azure-blob-storage