AsyncFileUpload序列化异常

发布于 2024-08-10 22:03:00 字数 4359 浏览 4 评论 0原文

我正在使用 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.InternalGetSerializedMembers(RuntimeType 类型)+7733643
System.Runtime.Serialization.FormatterServices.GetSerializedMembers(类型 类型,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(对象 图,标题[] inHeaders, __BinaryWriter serWriter,布尔值 fCheck) +410
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(流 序列化流、对象图、 Header[] headers,布尔值 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 item, Int32 initialStreamSize, Byte[]& buf,Int32&长度)+72
System.Web.SessionState.OutOfProcSessionStateStore.SetAndReleaseItemExclusive(HttpContext 上下文、字符串 ID、 SessionStateStore数据项,对象 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) 步骤,布尔值&同步完成) +75

I'm using the AsyncFileUpload control to upload images.
It works fine from localhost, but when I uploaded it on server I get the following error.

I can't even understand the reason of this error. I'll appreciate any answer.

This is the Method that I'm using

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

Error

Unable to serialize the session state.
In 'StateServer' and 'SQLServer' mode,
ASP.NET will serialize the session
state objects, and as a result
non-serializable objects or
MarshalByRef objects are not
permitted. The same restriction
applies if similar serialization is
done by the custom session state store
in 'Custom' mode.

Stack Trace: [SerializationException: Type
'System.Web.HttpPostedFile' in
Assembly 'System.Web, Version=2.0.0.0,
Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a' is
not marked as serializable.]
System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType
type) +7733643
System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type
type, StreamingContext context) +258
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo()
+111 System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object
obj, ISurrogateSelector
surrogateSelector, StreamingContext
context, SerObjectInfoInit
serObjectInfoInit, IFormatterConverter
converter, ObjectWriter objectWriter)
+161 System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Object
obj, ISurrogateSelector
surrogateSelector, StreamingContext
context, SerObjectInfoInit
serObjectInfoInit, IFormatterConverter
converter, ObjectWriter objectWriter)
+51 System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object
graph, Header[] inHeaders,
__BinaryWriter serWriter, Boolean fCheck) +410
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream
serializationStream, Object graph,
Header[] headers, Boolean fCheck) +134
System.Web.Util.AltSerialization.WriteValueToStream(Object
value, BinaryWriter writer) +1577

[HttpException (0x80004005): Unable to
serialize the session state. In
'StateServer' and 'SQLServer' mode,
ASP.NET will serialize the session
state objects, and as a result
non-serializable objects or
MarshalByRef objects are not
permitted. The same restriction
applies if similar serialization is
done by the custom session state store
in 'Custom' mode.]
System.Web.Util.AltSerialization.WriteValueToStream(Object
value, BinaryWriter writer) +1662
System.Web.SessionState.SessionStateItemCollection.WriteValueToStreamWithAssert(Object
value, BinaryWriter writer) +34
System.Web.SessionState.SessionStateItemCollection.Serialize(BinaryWriter
writer) +606
System.Web.SessionState.SessionStateUtility.Serialize(SessionStateStoreData
item, Stream stream) +239
System.Web.SessionState.SessionStateUtility.SerializeStoreData(SessionStateStoreData
item, Int32 initialStreamSize, Byte[]&
buf, Int32& length) +72
System.Web.SessionState.OutOfProcSessionStateStore.SetAndReleaseItemExclusive(HttpContext
context, String id,
SessionStateStoreData item, Object
lockId, Boolean newItem) +87
System.Web.SessionState.SessionStateModule.OnReleaseState(Object
source, EventArgs eventArgs) +560
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
+68 System.Web.HttpApplication.ExecuteStep(IExecutionStep
step, Boolean& completedSynchronously)
+75

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妄断弥空 2024-08-17 22:03:00

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

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

本例中的具体问题是“System.Web.HttpPostedFile”类不支持序列化。通常这个问题发生在我们自己的开发人员编写的类中,我们只需向该类添加一个属性即可,但这里似乎问题存在于.NET框架提供的类中。恐怕您需要找到一种方法来解决这个问题,例如编写自己的类或不使用这个类。

The problem lies with a configuration difference in IIS between your local machine and that of your web server.

Your server seems to be setup to handle the session by using a StateServer (another PC to handle a lot of the user data in memory) or by using your SQL Server (your database stores the user objects in special table). In order to transfer the data to the State Server or your Database server IIS will serialize your session information to transport it to the other server. Basically, this takes all the details for a given object and converts them to XML so that the object can be "saved" and "loaded" to/from memory as needed.

The specific problem in this case is that the 'System.Web.HttpPostedFile' class does not support serialization. Usually this problem occurs with classes written by our own developers and we can just add a attribute to the class but it appears here that the problem exists in a class provided by the .NET framework. I'm afraid you will need to find a way to work around the issue such as writing your own class or not making use of this one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文