Chinaunix首页 | 论坛 | 博客
  • 博客访问: 845933
  • 博文数量: 756
  • 博客积分: 40000
  • 博客等级: 大将
  • 技术积分: 4980
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-13 14:40
文章分类

全部博文(756)

文章存档

2011年(1)

2008年(755)

我的朋友

分类:

2008-10-13 16:09:08

CustomAppException
using System;
using System.Runtime.Serialization;
using System.Threading;
using System.Diagnostics;

namespace CustomExceptions
{
    /// 
    /// Custom exception class.
    /// 
    [Serializable]
    public class CustomAppException : ApplicationException
    {
        #region Custom Exception Properties
        
        private string credentials;
        public string Credentials
        {
            get    {return (credentials);}
            set {credentials = value;}
        }

        private int process_id;
        public int ProcessID
        {
            get    {return (process_id);}
            set {process_id = value;}
        }

        private int thread_id;
        public int ThreadID
        {
            get    {return (thread_id);}
            set {thread_id = value;}
        }

        #endregion
        #region Minimal Implementation

        public CustomAppException ()
        {
            GetExecutionContext ();
        }

        public CustomAppException (string message) : base (message)
        {
            GetExecutionContext ();
        }

        public CustomAppException (string message, Exception inner) : base 
            (message, inner)
        {
            GetExecutionContext ();
        }

        #endregion
        #region ISerializable Implementation

        protected CustomAppException (SerializationInfo info, 
            StreamingContext context) : base (info, context)
        {
            // get the custom property out of the serialization stream and 
            // set the object's property
            thread_id = info.GetInt32 ("ThreadID");
            process_id = info.GetInt32 ("ProcessID");
            credentials = info.GetString ("UserID");
        }

        public override void GetObjectData (SerializationInfo info, 
            StreamingContext context)
        {
            // add the custom property into the serialization stream
            info.AddValue ("ThreadID", thread_id);
            info.AddValue ("ProcessID", process_id);
            info.AddValue ("UserID", credentials);
            
            // call the base exception class to ensure proper serialization
            base.GetObjectData (info, context);
        }
        
        #endregion
        #region Helper methods

        private void GetExecutionContext ()
        {
            credentials = Environment.UserDomainName + @"\" + 
                Environment.UserName;
            thread_id = AppDomain.GetCurrentThreadId ();
            process_id = Process.GetCurrentProcess().Id;
        }

        #endregion
    }
}

Adding Custom Attributes
protected CustomAppException (SerializationInfo info, StreamingContext 
    context) : base (info, context)
{
    // get the custom property out of the serialization stream and
    // set the object's properties
    thread_id = info.GetInt32 ("ThreadID");
    process_id = info.GetInt32 ("ProcessID");
    credentials = info.GetString ("UserID");
}

public override void GetObjectData (SerializationInfo info, 
    StreamingContext context)
{
    // add the custom property into the serialization stream
    info.AddValue ("ThreadID", thread_id);
    info.AddValue ("ProcessID", process_id);
    info.AddValue ("UserID", credentials);
            
    // call the base exception class to ensure proper serialization
    base.GetObjectData (info, context);
}

EnterpriseExceptionGenerator
using System;
using System.EnterpriseServices;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// supply the COM+ info
[assembly: ApplicationName("Enterprise Exception Test")]
[assembly: ApplicationActivation(ActivationOption.Server)]
[assembly: ApplicationAccessControl(false)]
// key file required for GAC deployment
[assembly: AssemblyKeyFile(@"..\..\EnterpriseExceptionsKey.snk")]

namespace EnterpriseExceptions
{
    public interface IExceptionGenerator
    {
        void GenerateException (string message);
    }

    // ServicedComponent-based exception generator
    [EventTrackingEnabled (true)]
    public class EnterpriseExceptionGenerator : ServicedComponent, 
        IExceptionGenerator
    {
        public EnterpriseExceptionGenerator ()
        {
        }

        #region IExceptionGenerator Implementation

        public void GenerateException (string message)
        {
            CustomAppException x = new CustomAppException ("Generating 
                Enterprise CustomAppException");
            throw (x);
        }    

        #endregion
    }
}

Importing System.EnterpriseServices
// portions omitted for clarity
using System.EnterpriseServices;

// supply the COM+ info
[assembly: ApplicationName("Enterprise Exception Test")]
[assembly: ApplicationActivation(ActivationOption.Server)]
// key file required for GAC deployment
[assembly: AssemblyKeyFile(@"..\..\EnterpriseExceptionsKey.snk")]

namespace EnterpriseExceptions
{
    public interface IExceptionGenerator
    {
        void GenerateException (string message);
    }

    // ServicedComponent-based exception generator
    [EventTrackingEnabled (true)]
    public class EnterpriseExceptionGenerator : ServicedComponent, 
                                  IExceptionGenerator
    {
    // remainder omitted for clarity 
}
}

ClassInterfaceType Enumeration

Value Description
AutoDispatch Clients can only late-bind to the class interface (this is the default)
AutoDual Allows clients to bind to the internal class layout, which will result in a class that is difficult to modify without breaking COM clients
None No class interface is generated

Application Exception


Custom Exception


Using a Wrapper Class
using System;
using System.EnterpriseServices;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;

// supply the COM+ info
[assembly: ApplicationName("Enterprise Exception Solution #2")]
[assembly: ApplicationActivation(ActivationOption.Server)]
[assembly: ApplicationAccessControl(false)]
// key file required for GAC deployment
[assembly: AssemblyKeyFile(@"..\..\EnterpriseExceptionsKey.snk")]

//
//  EnterpriseExceptions Solution #1
//  implement a .NET Interface where method has a complex signature
//---------------------------------------------------------------------
namespace EnterpriseExceptions
{
    // formal interface declaration
    public interface IExceptionGenerator
    {
        void GenerateException (string message);
    }

    //
    // class for COM clients that delegates to the managed class
    //-----------------------------------------------------------------
    [ClassInterface (ClassInterfaceType.None)]
    public class COMEnterpriseExceptionGenerator : IExceptionGenerator
    {
        private EnterpriseExceptionGenerator exception--generator = null;

        public COMEnterpriseExceptionGenerator ()
        {
            exception--generator = new EnterpriseExceptionGenerator ();
        }

        #region IExceptionGenerator Members

        public void GenerateException (string message)
        {
            try
            {
                exception--generator.GenerateException ("generating 
                    exception from COMEnterpriseExceptionGenerator");
            }
            catch (CustomAppException x)
            {
                // catch custom exception, serialize contents to XML 
                // string, then rethrow
                throw (new ApplicationException (x.ToXml ()));
            }
        }

        #endregion
    }

    // 
    // ServicedComponent-based exception generator for managed clients
    //---------------------------------------------------------------------
    [EventTrackingEnabled (true)]
    [ClassInterfaceAttribute (ClassInterfaceType.None)]
    public class EnterpriseExceptionGenerator : ServicedComponent
    {
        public EnterpriseExceptionGenerator ()
        {
        }

        public void GenerateException (string message)
        {
            StringBuilder msg = new StringBuilder ();
            msg.AppendFormat ("[message: {0}]", message);
            CustomAppException x = new CustomAppException 
                (msg.ToString ());
            throw (x);
        }
    }
}


--------------------next---------------------

阅读(414) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~