输入' System.Web.HttpInputStream'未标记为可序列化

时间:2016-04-03 18:44:33

标签: c# asp.net wcf azure-storage azure-storage-blobs

我遇到的问题是,当我尝试将byte[]上传到 Azure blob存储时,我收到以下异常:

  

错误:键入' System.Web.HttpInputStream'在Assembly&System;系统中,   Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a'是   没有标记为可序列化。

因此,我开始使用[Serializable]标记代码所在的类,但仍会引发相同的异常。

Upload.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 Cloud服务不具有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();
    }
}

1 个答案:

答案 0 :(得分:2)

您正在尝试序列化整个流对象:

byte[] bytes = ObjectToByteArray(filePosted.InputStream);

你应该只是将流中的字节复制到byte[]并提交。

以下是使用内存流的快速示例:

        byte[] bytes; // you'll upload this byte array after you populate it.
        HttpPostedFile file = Request.Files["File1"];
        using (var mS = new MemoryStream())
        {
            file.InputStream.CopyTo(mS);
            bytes = mS.ToArray();
        }