/// <summary> /// To Add Metadata from request header, /// It will take device id from request header and header key values in json formate of keys defined in configuration /// </summary> /// <param name="request">HttpRequest with metadata information in header</param> /// <param name="metaDataService">Metadata service</param> /// <returns></returns> public static void AddMetaDataAsync(this HttpRequestMessage request, IMetaDataService metaDataService) { Task.Run(async() => { //Metdata entity object to insert in database MetaData metadata = new MetaData(); metadata.TimeStamp = DateTime.UtcNow; //Find DeviceId Supplied in HttpRequest header if not null assign to metadata object var deviceID = request.Headers.Contains("DeviceId") ? request.Headers.GetValues("DeviceId") : null; if (deviceID != null) { deviceID = deviceID.ToList(); metadata.SetValue("DeviceId", deviceID.FirstOrDefault()); } ///Dictionary to save key value from header and convert it to json and save in metadata var payloadPairs = new Dictionary <string, string>(); //Loop throgh each keys saved in setting.json as Headers foreach (string key in PayLoadKeys) { //If httpRequest header contains that key with value insert it into dictionary var value = request.Headers.Contains(key) ? request.Headers.GetValues(key) : null; if (value != null) { value = value.ToList(); payloadPairs.Add(key, value.FirstOrDefault()); } } //If there is one or more key value pair exist in header assign it to metadata.Payload property as json data if (payloadPairs.Count > 0) { metadata.SetValue("Payload", JsonConvert.SerializeObject(payloadPairs)); } //If DeviceId is assigned from header insert metadata object to database using metaDataService if (!string.IsNullOrEmpty(metadata.DeviceId)) { await metaDataService.InsertMetaDataAsync(metadata); } }); }