jezell / iserviceoriented

iserviceoriented / IServiceOriented.ServiceBus / Listener.cs
100644 122 lines (107 sloc) 2.932 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
 
namespace IServiceOriented.ServiceBus
{
    /// <summary>
    /// Base class for all service host listeners.
    /// </summary>
    /// <remarks>
    /// Listeners are responsible for receiving messages and publishing them to the bus.
    /// </remarks>
[Serializable]
[DataContract]
    public abstract class Listener : IDisposable
    {
[NonSerialized]
        ServiceBusRuntime _runtime;
        /// <summary>
        /// Gets the service bus instance that this listener is associated with.
        /// </summary>
        public ServiceBusRuntime Runtime
        {
            get
            {
                return _runtime;
            }
            internal set
            {
                _runtime = value;
            }
        }
 
[NonSerialized]
        ListenerEndpoint _endpoint;
        /// <summary>
        /// Gets the listener endpoint that this listener is associated with.
        /// </summary>
        public ListenerEndpoint Endpoint
        {
            get
            {
                return _endpoint;
            }
            internal set
            {
                _endpoint = value;
            }
        }
 
[NonSerialized]
        bool _started;
        /// <summary>
        /// Gets a boolean value indicating whether this listener has been started.
        /// </summary>
        public bool Started
        {
            get
            {
                return _started;
            }
            private set
            {
                _started = value;
            }
        }
 
        internal void StartInternal()
        {
            OnStart();
            Started = true;
        }
 
        internal void StopInternal()
        {
            OnStop();
            Started = false;
        }
 
        /// <summary>
        /// Perform any actions that should be performed when this listener starts.
        /// </summary>
        protected virtual void OnStart()
        {
        }
 
        /// <summary>
        /// Perform any actions that should be performed when this listener stops.
        /// </summary>
        protected virtual void OnStop()
        {
        }
 
        /// <summary>
        /// Dispose any resources held by this listener.
        /// </summary>
        /// <param name="disposing">Indicates whether this method is being called as a result of a direct call to Dispose (true) or by the object's finalizer (false).</param>
        protected virtual void Dispose(bool disposing)
        {
 
        }
        #region IDisposable Members
 
        public void Dispose()
        {
            Dispose(true);
 
            GC.SuppressFinalize(this);
        }
 
        #endregion
 
        ~Listener()
        {
            Dispose(false);
        }
    }
 
}