11using System ;
22using System . Collections . Generic ;
3- using System . Net ;
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 ;
@@ -31,9 +31,23 @@ private void Initialize(GXService providerService)
3131 ServiceSettings serviceSettings = new ( PropertyConstants . EVENT_ROUTER , Name , providerService ) ;
3232 _endpoint = serviceSettings . GetEncryptedPropertyValue ( PropertyConstants . URI_ENDPOINT ) ;
3333 _accessKey = serviceSettings . GetEncryptedPropertyValue ( PropertyConstants . ACCESS_KEY ) ;
34- _client = new EventGridPublisherClient (
35- new Uri ( _endpoint ) ,
36- new AzureKeyCredential ( _accessKey ) ) ;
34+
35+ if ( ! string . IsNullOrEmpty ( _endpoint ) ) {
36+ if ( string . IsNullOrEmpty ( _accessKey ) )
37+
38+ //Try using Active Directory authentication
39+ _client = new EventGridPublisherClient (
40+ new Uri ( _endpoint ) ,
41+ new DefaultAzureCredential ( ) ) ;
42+
43+ else
44+
45+ _client = new EventGridPublisherClient (
46+ new Uri ( _endpoint ) ,
47+ new AzureKeyCredential ( _accessKey ) ) ;
48+ }
49+ else
50+ throw new Exception ( "Endpoint URI must be set." ) ;
3751 }
3852 public override string GetName ( )
3953 {
@@ -54,7 +68,7 @@ public bool SendEvent(GXCloudEvent gxCloudEvent, bool binaryData)
5468 }
5569 else
5670 {
57- throw new Exception ( "SendEvent: There was an error at the Event Grid initialization." ) ;
71+ throw new Exception ( "There was an error at the Event Grid initialization." ) ;
5872 }
5973 }
6074 catch ( AggregateException ae )
@@ -80,7 +94,7 @@ public bool SendEvents(IList<GXCloudEvent> gxCloudEvents, bool binaryData)
8094 }
8195 else
8296 {
83- throw new Exception ( "SendEvents: There was an error at the Event Grid initialization." ) ;
97+ throw new Exception ( "There was an error at the Event Grid initialization." ) ;
8498 }
8599 }
86100 catch ( AggregateException ae )
@@ -232,7 +246,7 @@ public bool GetMessageFromException(Exception ex, SdtMessages_Message msg)
232246 {
233247 try
234248 {
235- HttpListenerException az_ex = ( HttpListenerException ) ex ;
249+ RequestFailedException az_ex = ( RequestFailedException ) ex ;
236250 msg . gxTpr_Id = az_ex . ErrorCode . ToString ( ) ;
237251 msg . gxTpr_Description = az_ex . Message ;
238252 return true ;
@@ -255,7 +269,9 @@ private EventGridEvent ToEventGridSchema(GXEventGridSchema gxEventGridSchema, bo
255269 evt . Id = gxEventGridSchema . id ;
256270 if ( ! string . IsNullOrEmpty ( gxEventGridSchema . topic ) )
257271 evt . Topic = gxEventGridSchema . topic ;
258-
272+ if ( gxEventGridSchema . eventtime != DateTime . MinValue )
273+ evt . EventTime = gxEventGridSchema . eventtime ;
274+
259275 return evt ;
260276 }
261277 private CloudEvent ToCloudEvent ( GXCloudEvent gxCloudEvent , bool isBinaryData )
@@ -264,9 +280,13 @@ private CloudEvent ToCloudEvent(GXCloudEvent gxCloudEvent, bool isBinaryData)
264280 if ( string . IsNullOrEmpty ( gxCloudEvent . data ) )
265281 evt = new CloudEvent ( gxCloudEvent . source , gxCloudEvent . type , null ) ;
266282 else
267- {
283+ {
268284 if ( ! isBinaryData )
269- evt = new CloudEvent ( gxCloudEvent . source , gxCloudEvent . type , gxCloudEvent . data ) ;
285+ {
286+ if ( string . IsNullOrEmpty ( gxCloudEvent . datacontenttype ) )
287+ gxCloudEvent . datacontenttype = "application/json" ;
288+ evt = new CloudEvent ( gxCloudEvent . source , gxCloudEvent . type , BinaryData . FromString ( gxCloudEvent . data ) , gxCloudEvent . datacontenttype , CloudEventDataFormat . Json ) ;
289+ }
270290 else
271291 {
272292 if ( string . IsNullOrEmpty ( gxCloudEvent . datacontenttype ) )
@@ -280,6 +300,8 @@ private CloudEvent ToCloudEvent(GXCloudEvent gxCloudEvent, bool isBinaryData)
280300 evt . DataSchema = gxCloudEvent . dataschema ;
281301 if ( ! string . IsNullOrEmpty ( gxCloudEvent . subject ) )
282302 evt . Subject = gxCloudEvent . subject ;
303+ if ( gxCloudEvent . time != DateTime . MinValue )
304+ evt . Time = gxCloudEvent . time ;
283305 return evt ;
284306 }
285307 #endregion
@@ -301,5 +323,12 @@ public class GXEventGridSchema
301323 [ DataMember ]
302324 public string dataversion { get ; set ; }
303325
326+ [ DataMember ]
327+ public DateTime eventtime { get ; set ; }
328+
329+ [ DataMember ]
330+ public string metadataversion { get ; set ; }
331+
332+
304333 }
305334}
0 commit comments