【问题标题】:Type 'System.Web.HttpInputStream' is not marked as serializable类型“System.Web.HttpInputStream”未标记为可序列化
【发布时间】: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


    【解决方案1】:

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

    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();
            }
    

    【讨论】:

    • 谢谢,我不得不改变一些其他的事情来得到我想要的,但你的回答确实消除了我收到的异常
    猜你喜欢
    • 2010-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-15
    • 1970-01-01
    • 2015-09-27
    • 2011-02-06
    • 2012-07-15
    相关资源
    最近更新 更多