11using System ;
2- using System . Collections ;
32using System . Collections . Generic ;
43using System . Runtime . Serialization ;
54using System . Text . Json ;
65using System . Threading . Tasks ;
76using Azure ;
7+ using Azure . Identity ;
88using Azure . Messaging ;
99using Azure . Messaging . EventGrid ;
1010using GeneXus . Messaging . Common ;
1111using GeneXus . Services ;
1212using GeneXus . Utils ;
13+ using log4net ;
1314
1415namespace GeneXus . Messaging . GXAzureEventGrid
1516{
@@ -19,6 +20,7 @@ public class AzureEventGrid : EventRouterBase, IEventRouter
1920 private EventGridPublisherClient _client ;
2021 private string _endpoint ;
2122 private string _accessKey ;
23+ static readonly ILog logger = LogManager . GetLogger ( typeof ( AzureEventGrid ) ) ;
2224 public AzureEventGrid ( ) : this ( null )
2325 {
2426 }
@@ -32,11 +34,20 @@ private void Initialize(GXService providerService)
3234 _endpoint = serviceSettings . GetEncryptedPropertyValue ( PropertyConstants . URI_ENDPOINT ) ;
3335 _accessKey = serviceSettings . GetEncryptedPropertyValue ( PropertyConstants . ACCESS_KEY ) ;
3436
35- if ( ! string . IsNullOrEmpty ( _endpoint ) ) {
37+ if ( ! string . IsNullOrEmpty ( _endpoint ) ) {
3638
39+ if ( ! string . IsNullOrEmpty ( _accessKey ) )
3740 _client = new EventGridPublisherClient (
3841 new Uri ( _endpoint ) ,
3942 new AzureKeyCredential ( _accessKey ) ) ;
43+ else
44+ //Try authenticating using AD
45+ {
46+ GXLogging . Debug ( logger , "Authentication using Oauth 2.0." ) ;
47+ _client = new EventGridPublisherClient (
48+ new Uri ( _endpoint ) ,
49+ new DefaultAzureCredential ( ) ) ;
50+ }
4051 }
4152 else
4253 throw new Exception ( "Endpoint URI must be set." ) ;
@@ -50,22 +61,15 @@ public bool SendEvent(GXCloudEvent gxCloudEvent, bool binaryData)
5061 {
5162 CloudEvent evt = ToCloudEvent ( gxCloudEvent , binaryData ) ;
5263 bool success = false ;
53- try
64+ Task < bool > task ;
65+ if ( _client != null )
5466 {
55- Task task ;
56- if ( _client != null )
57- {
58- task = Task . Run ( async ( ) => await sendEvtAsync ( evt ) . ConfigureAwait ( false ) ) ;
59- success = true ;
60- }
61- else
62- {
63- throw new Exception ( "There was an error at the Event Grid initialization." ) ;
64- }
67+ task = Task . Run ( async ( ) => await sendEvtAsync ( evt ) . ConfigureAwait ( false ) ) ;
68+ success = task . Result ;
6569 }
66- catch ( AggregateException ae )
70+ else
6771 {
68- throw ae ;
72+ throw new Exception ( "There was an error at the Event Grid initialization." ) ;
6973 }
7074 return success ;
7175 }
@@ -76,22 +80,15 @@ public bool SendEvents(IList<GXCloudEvent> gxCloudEvents, bool binaryData)
7680 evts . Add ( ToCloudEvent ( e , binaryData ) ) ;
7781
7882 bool success = false ;
79- try
83+ Task < bool > task ;
84+ if ( _client != null )
8085 {
81- Task task ;
82- if ( _client != null )
83- {
84- task = Task . Run ( async ( ) => await sendEvtsAsync ( evts ) . ConfigureAwait ( false ) ) ;
85- success = true ;
86- }
87- else
88- {
89- throw new Exception ( "There was an error at the Event Grid initialization." ) ;
90- }
86+ task = Task . Run ( async ( ) => await sendEvtsAsync ( evts ) . ConfigureAwait ( false ) ) ;
87+ success = task . Result ;
9188 }
92- catch ( AggregateException ae )
89+ else
9390 {
94- throw ae ;
91+ throw new Exception ( "There was an error at the Event Grid initialization." ) ;
9592 }
9693 return success ;
9794 }
@@ -112,11 +109,11 @@ public bool SendCustomEvents(string jsonString, bool isBinary)
112109 bool success = false ;
113110 try
114111 {
115- Task task ;
112+ Task < bool > task ;
116113 if ( _client != null )
117114 {
118115 task = Task . Run ( async ( ) => await sendEventGridSchemaEventsAsync ( eventGridEvents ) . ConfigureAwait ( false ) ) ;
119- success = true ;
116+ success = task . Result ;
120117 }
121118 else
122119 {
@@ -137,11 +134,11 @@ public bool SendCustomEvents(string jsonString, bool isBinary)
137134 bool success = false ;
138135 try
139136 {
140- Task task ;
137+ Task < bool > task ;
141138 if ( _client != null )
142139 {
143140 task = Task . Run ( async ( ) => await sendEventGridSchemaEventAsync ( ToEventGridSchema ( evt , isBinary ) ) . ConfigureAwait ( false ) ) ;
144- success = true ;
141+ success = task . Result ;
145142 }
146143 else
147144 {
@@ -166,39 +163,46 @@ public bool SendCustomEvents(string jsonString, bool isBinary)
166163 /// Send asynchronously an event formatted as Azure EventGrid Schema.
167164 /// </summary>
168165 /// <param name="evt"></param>
169- /// <returns></returns>
170- private async Task sendEventGridSchemaEventAsync ( EventGridEvent evt )
166+ /// <returns>bool </returns>
167+ private async Task < bool > sendEventGridSchemaEventAsync ( EventGridEvent evt )
171168 {
172- await _client . SendEventAsync ( evt ) . ConfigureAwait ( false ) ;
173-
169+ Response response = await _client . SendEventAsync ( evt ) . ConfigureAwait ( false ) ;
170+ GXLogging . Debug ( logger , string . Format ( "Send Event GridSchema: {0} {1}" , response . Status , response . ReasonPhrase ) ) ;
171+ return ! response . IsError ;
172+
174173 }
175174 /// <summary>
176175 /// Send asynchronously a list of events formatted as Azure EventGrid Schema.
177176 /// </summary>
178177 /// <param name="evts"></param>
179- /// <returns></returns>
180- private async Task sendEventGridSchemaEventsAsync ( IEnumerable < EventGridEvent > evts )
178+ /// <returns>bool </returns>
179+ private async Task < bool > sendEventGridSchemaEventsAsync ( IEnumerable < EventGridEvent > evts )
181180 {
182- await _client . SendEventsAsync ( evts ) . ConfigureAwait ( false ) ;
181+ Response response = await _client . SendEventsAsync ( evts ) . ConfigureAwait ( false ) ;
182+ GXLogging . Debug ( logger , string . Format ( "Send Events GridSchema: {0} {1}" , response . Status , response . ReasonPhrase ) ) ;
183+ return ! response . IsError ;
183184 }
184185 /// <summary>
185186 /// Send asynchronously an event formatted as CloudEvent Schema.
186187 /// </summary>
187188 /// <param name="cloudEvent"></param>
188- /// <returns></returns>
189- private async Task sendEvtAsync ( CloudEvent cloudEvent )
189+ /// <returns>bool </returns>
190+ private async Task < bool > sendEvtAsync ( CloudEvent cloudEvent )
190191 {
191- await _client . SendEventAsync ( cloudEvent ) . ConfigureAwait ( false ) ;
192-
192+ Response response = await _client . SendEventAsync ( cloudEvent ) . ConfigureAwait ( false ) ;
193+ GXLogging . Debug ( logger , string . Format ( "Send Event: {0} {1}" , response . Status , response . ReasonPhrase ) ) ;
194+ return ! response . IsError ;
193195 }
194196 /// <summary>
195197 /// Send asynchronously a list of CloudEvent Schema formatted events.
196198 /// </summary>
197199 /// <param name="cloudEvents"></param>
198- /// <returns></returns>
199- private async Task sendEvtsAsync ( IEnumerable < CloudEvent > cloudEvents )
200+ /// <returns>bool </returns>
201+ private async Task < bool > sendEvtsAsync ( IEnumerable < CloudEvent > cloudEvents )
200202 {
201- await _client . SendEventsAsync ( cloudEvents ) . ConfigureAwait ( false ) ;
203+ Response response = await _client . SendEventsAsync ( cloudEvents ) . ConfigureAwait ( false ) ;
204+ GXLogging . Debug ( logger , string . Format ( "Send Events: {0} {1}" , response . Status , response . ReasonPhrase ) ) ;
205+ return ! response . IsError ;
202206 }
203207
204208 #endregion
0 commit comments