|
|
 |
| C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, win occasional prizes given to our best members, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
|
 |
|
|
 |

May 2nd, 2005, 04:13 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Location: Copenhagen, , Denmark.
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Howto Serialize System.Exception?
Does anyone know how to serialize a System.Exception in C#? When I try to do it I get an error messages saying this...
The property 'TargetSite' on type 'System.Exception' cannot be serialized because it is decorated with declarative security permission attributes. Consider using imperative asserts or demands in the property accessors.
Searching for the answer has not provided me with a solution. I found this article but I doesn't help me much. I cannot figure out what I have to do to make the serialization work. Hope you can help me? Do you know some good information about this?
Thanks, Jacob.
|

May 3rd, 2005, 03:40 PM
|
|
Friend of Wrox
|
|
Join Date: Jul 2004
Location: Tehran, , Iran.
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
|
|
I think if you use BinaryFormatter instead of XmlSerializer there would be no problem,Have you tried that or do you insist on serializing exceptions to the XML?
_____________
Mehdi.
software student.
|

November 3rd, 2006, 12:26 PM
|
|
Registered User
|
|
Join Date: Jun 2003
Location: , , USA.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
quote:Originally posted by jacob
Does anyone know how to serialize a System.Exception in C#?...
|
jacob --
Regarding how to serialize an Exception, did you find an answer?
If you did, then please post your results, if at all possible.
Thank you.
-- Mark Kamoski
|

November 6th, 2006, 11:12 AM
|
|
Registered User
|
|
Join Date: Nov 2006
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hope this helps:
using System;
using System.Collections;
using System.Runtime.Serialization;
namespace Common.CustomExceptions
{
/// <summary>
/// Custom exception.
/// </summary>
[Serializable]
public class CustomExceptionBase: ApplicationException
{
// Local private members
protected DateTime _dateTime = DateTime.Now;
protected String _machineName = Environment.MachineName;
protected String _exceptionType = "";
private String _exceptionDescription = "";
protected String _stackTrace = "";
protected String _assemblyName = "";
protected String _messageName = "";
protected String _messageId = "";
protected Hashtable _data = null;
protected String _source = "";
protected Int32 _exceptionNumber = 0;
public CustomExceptionBase(): base()
{
if (Environment.StackTrace != null)
this._stackTrace = Environment.StackTrace;
}
public CustomExceptionBase(Int32 exceptionNumber): base()
{
this._exceptionNumber = exceptionNumber;
if (Environment.StackTrace != null)
this._stackTrace = Environment.StackTrace;
}
public CustomExceptionBase(Int32 exceptionNumber, String message): base(message)
{
this._exceptionNumber = exceptionNumber;
if (Environment.StackTrace != null)
this._stackTrace = Environment.StackTrace;
}
public CustomExceptionBase(Int32 exceptionNumber, String message, Exception innerException):
base(message, innerException)
{
this._exceptionNumber = exceptionNumber;
if (Environment.StackTrace != null)
this._stackTrace = Environment.StackTrace;
}
public CustomExceptionBase(Int32 exceptionNumber, String message, Exception innerException, String messageName, String mqMessageId):
base(message, innerException)
{
this._exceptionNumber = exceptionNumber;
this._messageId = mqMessageId;
this._messageName = messageName;
if (Environment.StackTrace != null)
this._stackTrace = Environment.StackTrace;
}
public CustomExceptionBase(Int32 exceptionNumber, String message, Exception innerException, String messageName, String mqMessageId, String source):
base(message, innerException)
{
this._exceptionNumber = exceptionNumber;
this._messageId = mqMessageId;
this._messageName = messageName;
this._source = source.Equals("") ? this._source : source;
if (Environment.StackTrace != null)
this._stackTrace = Environment.StackTrace;
}
#region ISerializable members
/// <summary>
/// This CTor allows exceptions to be marhalled accross remoting boundaries
/// </summary>
/// <param name="info"></param>
/// <param name="context"></param>
protected CustomExceptionBase(SerializationInfo info, StreamingContext context) :
base(info,context)
{
this._dateTime = info.GetDateTime("_dateTime");
this._machineName = info.GetString("_machineName");
this._stackTrace = info.GetString("_stackTrace");
this._exceptionType = info.GetString("_exceptionType");
this._assemblyName = info.GetString("_assemblyName");
this._messageName = info.GetString("_messageName");
this._messageId = info.GetString("_messageId");
this._exceptionDescription = info.GetString("_exceptionDescription");
this._data = (Hashtable)info.GetValue("_data", Type.GetType("System.Collections.Hashtable"));
}
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("_dateTime", this._dateTime);
info.AddValue("_machineName", this._machineName);
info.AddValue("_stackTrace", this._stackTrace);
info.AddValue("_exceptionType", this._exceptionType);
info.AddValue("_assemblyName", this._assemblyName);
info.AddValue("_messageName", this._messageName);
info.AddValue("_messageId", this._messageId);
info.AddValue("_exceptionDescription", this._exceptionDescription);
info.AddValue("_data", this._data, Type.GetType("System.Collections.Hashtable"));
base.GetObjectData (info, context);
}
#endregion
}
}
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |