【问题标题】:AsyncFileUpload SerializationExceptionAsyncFileUpload 序列化异常
【发布时间】:2010-12-15 02:59:25
【问题描述】:

我正在使用 AsyncFileUpload 控件来上传图像。 它在本地主机上运行良好,但是当我将其上传到服务器时出现以下错误。

我什至无法理解这个错误的原因。我会很感激任何答案。

这是我正在使用的方法

protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e) 
{
    string email = WriteYourEmailTXT.Text;
    var dt = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day).ToString("ddMMyyyy");
    string parentFolder = Server.MapPath("uploads");
    string childFolder = parentFolder + "\\" + email + "\\";
    if (!Directory.Exists(childFolder))
        Directory.CreateDirectory(childFolder);

    string counter = getCount();
    if (AsyncFileUpload1.HasFile) {
        string ext = getExtension(AsyncFileUpload1.FileName);
        string finalName = dt + "_" + counter + ext;
        string finalPath = childFolder + finalName;
        AsyncFileUpload1.SaveAs(finalPath);
        SetCount();
        SetFileName(finalName);
    }
}

protected void SetCount() 
{
    HttpCookie aCookie = Request.Cookies["CountPhoto"];
    int cookieNum = 0;
    if (aCookie != null)
        if (aCookie.Value != "")
            cookieNum = int.Parse(aCookie.Value.ToString());

    string newvalue = (cookieNum + 1).ToString();
    Response.Cookies["CountPhoto"].Value = newvalue;
}

protected void SetFileName(string fileName) 
{
    HttpCookie aCookie = Request.Cookies["FileName"];
    var filename = "";
    if (aCookie != null)
        if (aCookie.Value != "") {
            filename = aCookie.Value;
        }
    string newFileName = filename + "," + fileName;
    Response.Cookies["FileName"].Value = newFileName;
}

错误

无法序列化会话状态。 在“StateServer”和“SQLServer”模式下, ASP.NET 将序列化会话 状态对象,因此 不可序列化的对象或 MarshalByRef 对象不是 允许。相同的限制 如果类似的序列化是适用的 由自定义会话状态存储完成 在“自定义”模式下。

堆栈跟踪: [SerializationException:类型 'System.Web.HttpPostedFile' 在 程序集'System.Web,版本=2.0.0.0, 文化=中性, PublicKeyToken=b03f5f7f11d50a3a' 是 未标记为可序列化。]
System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType 类型)+7733643
System.Runtime.Serialization.FormatterServices.GetSerializableMembers(类型 类型,StreamingContext 上下文)+258
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo() +111 System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(对象 obj,ISurrogateSelector 代理选择器,StreamingContext 上下文,SerObjectInfoInit serObjectInfoInit, IFormatterConverter 转换器,ObjectWriter objectWriter) +161 System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(对象 obj,ISurrogateSelector 代理选择器,StreamingContext 上下文,SerObjectInfoInit serObjectInfoInit, IFormatterConverter 转换器,ObjectWriter objectWriter) +51 System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(对象 图,Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck) +410
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(流 序列化流,对象图, 标头 [] 标头,布尔 fCheck) +134 System.Web.Util.AltSerialization.WriteValueToStream(对象 值,BinaryWriter 写入器)+1577

[HttpException (0x80004005): 无法 序列化会话状态。在 'StateServer' 和 'SQLServer' 模式, ASP.NET 将序列化会话 状态对象,因此 不可序列化的对象或 MarshalByRef 对象不是 允许。相同的限制 如果类似的序列化是适用的 由自定义会话状态存储完成 在“自定义”模式下。]
System.Web.Util.AltSerialization.WriteValueToStream(对象 值,BinaryWriter 写入器)+1662
System.Web.SessionState.SessionStateItemCollection.WriteValueToStreamWithAssert(对象 值,BinaryWriter 写入器)+34
System.Web.SessionState.SessionStateItemCollection.Serialize(BinaryWriter 作家)+606
System.Web.SessionState.SessionStateUtility.Serialize(SessionStateStoreData 项目,流流)+239
System.Web.SessionState.SessionStateUtility.SerializeStoreData(SessionStateStoreData 项目,Int32 初始流大小,字节[]& buf, Int32& 长度) +72
System.Web.SessionState.OutOfProcSessionStateStore.SetAndReleaseItemExclusive(HttpContext 上下文,字符串 ID, SessionStateStoreData 项,对象 lockId,布尔值 newItem) +87
System.Web.SessionState.SessionStateModule.OnReleaseState(对象 源,EventArgs eventArgs) +560
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +68 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean & completedSynchronously) +75

【问题讨论】:

    标签: asp.net ajaxcontroltoolkit


    【解决方案1】:

    问题在于本地计算机和 Web 服务器之间的 IIS 配置差异。

    您的服务器似乎设置为通过使用 StateServer(另一台 PC 处理内存中的大量用户数据)或使用 SQL Server(您的数据库将用户对象存储在特殊表中)来处理会话。为了将数据传输到状态服务器或您的数据库服务器,IIS 将序列化您的会话信息以将其传输到其他服务器。基本上,这会获取给定对象的所有详细信息并将其转换为 XML,以便可以根据需要将对象“保存”到内存或从内存“加载”。

    这种情况下的具体问题是 'System.Web.HttpPostedFile' 类不支持序列化。通常这个问题出现在我们自己的开发人员编写的类中,我们可以只为类添加一个属性,但这里显示问题存在于 .NET 框架提供的类中。恐怕您需要找到一种方法来解决这个问题,例如编写自己的类或不使用这个类。

    【讨论】:

    • 另外,您可以尝试通过 IIS 配置切换在您的 Web 服务器上处理会话的方式,但我猜这超出了您的控制范围。
    • 非常感谢您的回答。很有帮助。
    猜你喜欢
    • 1970-01-01
    • 2014-04-18
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多