c# - 类型 'System.Web.HttpInputStream' 未标记为可序列化

标签 c# asp.net wcf azure-storage azure-blob-storage

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

Error: Type 'System.Web.HttpInputStream' in Assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable.

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

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

最佳答案

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

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

关于c# - 类型 'System.Web.HttpInputStream' 未标记为可序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36389594/

相关文章:

c# - 良好的 C#.NET 解决方案来管理频繁的数据库轮询

c# - System.Drawing.ImageFromStream上的System.ArgumentException

c# - 存储数据库项目先前状态的最佳方式

jquery - ASP.NET 的 jqGrid 的免费替代品?

c# - 从 C# 将数据导出到 PDF 时出错

.net - WCF - 已超过传入消息的最大消息大小配额

wcf - 客户端证书通过 ARR 和 AuthorizationContext 具有不同的指纹

C# 获取显示名称的首字母

c# - 为什么build action : None ignored for a *.是cs文件?

c# - 松耦合 NativeMethods