[E2010][TA][C#] Should TransportAgents implement IDisposable, or should events be -=removed with a finalizer? RRS feed

  • Question

  • I see that most things done with the TransportAgent is done though an event.  Since I think my DLL will be loaded and unloaded frequently, I need to prevent memory leaks from the adding += of the event.

    How should I remove the event?  IDisposable?  The "deconstructor" AKA "~"?

    • Edited by ChrisLaMont Wednesday, June 13, 2012 11:23 PM
    Tuesday, June 12, 2012 3:49 AM

All replies

  • Is this code safe to use with the TransportAgent?

            public void Dispose()
                var unsubscribeResolvedMessage = this.OnResolvedMessage.GetInvocationList().ToList();
                foreach (var item in unsubscribeResolvedMessage)
                    this.OnResolvedMessage -= item;
                var onCategorizedMessage = this.OnCategorizedMessage.GetInvocationList().ToList();
                foreach (var item in onCategorizedMessage)
                    this.OnCategorizedMessage -= item;
                var onRoutedMessage = this.OnRoutedMessage.GetInvocationList().ToList();
                foreach (var item in onRoutedMessage)
                    this.OnRoutedMessage -= item;
                var onSubmittedMessage = this.OnSubmittedMessage.GetInvocationList().ToList();
                foreach (var item in onSubmittedMessage)
                    this.OnSubmittedMessage -= item;

    Tuesday, June 12, 2012 10:31 PM
  • As a followup, what is the right pattern for using Factory.Close?

    How many instances will be returned and is this valid code?

    public class RoutingRuleFactory : RoutingAgentFactory
            List<RoutingAgent> agents = new List<RoutingAgent>();
            /// <summary>
            /// Creates a new instance of <see cref="RoutingRuleAgent"/>
            /// </summary>
            /// <param name="server">Smtp Server representation</param>
            /// <returns>New instance of <see cref="RoutingRuleAgent"/></returns>
            public override RoutingAgent CreateAgent(SmtpServer server)
                var ret = new RoutingRuleAgent();
                agents.Add( ret );
                return ret;
            public override void Close()
                foreach (RoutingRuleAgent item in agents)

    Wednesday, June 13, 2012 11:19 AM
  • bump... no replies yet..
    Thursday, June 21, 2012 3:38 PM