Remote Exceptions

Remote Exceptions

Exceptions sometime occur in remote code. An exception that is raised in a different application domain is a remote exception. Remote exceptions include exceptions thrown in a .NET Remoting application or a Web service application. Exceptions that cross application domains must be serialized to maintain the state. System exceptions are serializable. However, you need to make application exceptions serializable.

Follow these steps to serialize an application exception:

  1. Adorn the application exception with the serializable attribute.

  2. Implement a two-argument constructor with a SerializationInfo and StreamingContext parameter. Deserialize the exception with the SerializationInfo parameter, which is a state bag. Retrieve state information of the exception using the Get methods of the SerializationInfo object. The StreamingContext parameter provides additional data about the source or target of the serialization process. In addition, call the same constructor in the base class to allow the base class to deserialize its state.

  3. Implement the GetObjectData method to serialize the exception. The method also has two parameters: Serialization and StreamingContext. Use the Serialization.AddValue to serialize the state of the exception. Invoke GetObjectData on the base class to allow it to serialize itself.

  4. For the exception to be available in the client assembly, share the assembly through a global assembly cache or an application configuration file. If the assembly is not shared, the assembly must be copied into the private directory of the client application.

CustomException is an application exception that supports remoting. There is one property, prop_Time, which is serialized in GetObjectData and deserialized in the two-argument constructor:

using System;
using System.Reflection;
using System.Runtime.Serialization;


namespace Donis.CSharpBook{

    public class CustomException: Exception{

        public CustomException()
                : base("custom exception", null) {
            prop_Time=DateTime.Now.ToLongDateString()+" "+

        protected CustomException(SerializationInfo info,
                StreamingContext context) : base(info, context){

        public override void GetObjectData( SerializationInfo info,
                  StreamingContext context ){
            info.AddValue("Time", prop_Time, typeof(string));

        protected string prop_Time=null;
        public string Time {
            get {
                return prop_Time;

In Microsoft Visual Studio, the assembly attributes, such as AssemblyVersion, are found in the AssemblyInfo.cs file.