@@ -34,20 +34,60 @@ public void Close() {
3434 }
3535 }
3636
37+ public void Send < T > ( T obj ) {
38+ Send < T > ( obj , null ) ;
39+ }
40+
3741 public void Send < T > ( T obj , IDictionary < string , object > metadata ) {
38- Send < T > ( obj , metadata , configuration . DefaultSerializer . Create ( ) ) ;
42+ Send < T > ( obj , configuration . DefaultSerializer . Create ( ) , metadata ) ;
3943 }
4044
41- public void Send < T > ( T obj , IDictionary < string , object > metadata , IServiceBusSerializer serializer ) {
45+ public void Send < T > ( T obj , IServiceBusSerializer serializer = null , IDictionary < string , object > metadata = null ) {
4246
4347 // Declare a wait object that will be used for synchronization.
4448 var waitObject = new ManualResetEvent ( false ) ;
4549
4650 // Declare a timeout value during which the messages are expected to be sent.
4751 var sentTimeout = TimeSpan . FromMinutes ( 2 ) ;
4852
53+ Exception failureException = null ;
54+
55+ SendAsync < T > ( obj , ( result ) => {
56+ waitObject . Set ( ) ;
57+ failureException = result . ThrownException ;
58+ } ) ;
59+
60+ // Wait until the messaging operations are completed.
61+ bool completed = waitObject . WaitOne ( sentTimeout ) ;
62+ waitObject . Dispose ( ) ;
63+
64+ if ( completed ) {
65+ //DO Nothing
66+ }
67+ else {
68+ if ( failureException != null ) {
69+ throw failureException ;
70+ }
71+ throw new Exception ( "Failed to Send Message for Unknown Reason." ) ;
72+ }
73+ }
74+
75+ public void SendAsync < T > ( T obj , Action < IMessageSentResult < T > > resultCallBack ) {
76+ SendAsync < T > ( obj , resultCallBack , configuration . DefaultSerializer . Create ( ) ) ;
77+ }
78+
79+ public void SendAsync < T > ( T obj , Action < IMessageSentResult < T > > resultCallBack , IDictionary < string , object > metadata ) {
80+ SendAsync < T > ( obj , resultCallBack , configuration . DefaultSerializer . Create ( ) , metadata ) ;
81+ }
82+
83+ public void SendAsync < T > ( T obj , Action < IMessageSentResult < T > > resultCallBack , IServiceBusSerializer serializer = null , IDictionary < string , object > metadata = null ) {
84+
85+ serializer = serializer ?? configuration . DefaultSerializer . Create ( ) ;
86+
4987 Exception failureException = null ;
5088 BrokeredMessage message = null ;
89+ var sw = new Stopwatch ( ) ;
90+ sw . Start ( ) ;
5191
5292 // Use a retry policy to execute the Send action in an asynchronous and reliable fashion.
5393 retryPolicy . ExecuteAction
@@ -86,7 +126,12 @@ public void Send<T>(T obj, IDictionary<string, object> metadata, IServiceBusSeri
86126 serializer . Dispose ( ) ;
87127 serializer = null ;
88128 }
89- waitObject . Set ( ) ;
129+ sw . Stop ( ) ;
130+ resultCallBack ( new MessageSentResult < T > ( ) {
131+ IsSuccess = true ,
132+ Message = obj ,
133+ TimeSpent = sw . Elapsed
134+ } ) ;
90135 }
91136 } ,
92137 ( ex ) => {
@@ -105,23 +150,19 @@ public void Send<T>(T obj, IDictionary<string, object> metadata, IServiceBusSeri
105150 }
106151 ) ;
107152
108- // Wait until the messaging operations are completed.
109- bool completed = waitObject . WaitOne ( sentTimeout ) ;
110- waitObject . Dispose ( ) ;
153+ sw . Stop ( ) ;
154+ resultCallBack ( new MessageSentResult < T > ( ) {
155+ IsSuccess = true ,
156+ Message = obj ,
157+ ThrownException = failureException ,
158+ TimeSpent = sw . Elapsed
159+ } ) ;
111160
112- if ( completed ) {
113- //DO Nothing
114- }
115- else {
116- if ( failureException != null ) {
117- throw failureException ;
118- }
119- throw new Exception ( "Failed to Send Message for Unknown Reason." ) ;
120- }
121161 }
122162
123163 public override void Dispose ( bool disposing ) {
124164 Close ( ) ;
125165 }
166+
126167 }
127168}
0 commit comments