public async Task CampaignCreatedHandler( [QueueTrigger("%ENVIRONMENT%-" + QueueNames.CampaignCreated, Connection = "StorageConnection")] string message, FunctionContext executionContext ) { var logger = executionContext.GetLogger(FunctionNames.CampaignCreated); logger.LogInformation("Function '{FunctionName}' was triggered.", FunctionNames.CampaignCreated); var campaign = JsonSerializer.Deserialize <CampaignQueueItem>(message, JsonSerializerOptionDefaults.GetDefaultSettings()); if (!campaign.Published) { return; } if (campaign.DeliveryChannel.HasFlag(CampaignQueueItem.CampaignDeliveryChannel.PushNotification)) { await ProcessPushNotifications(campaign); return; } if (campaign.DeliveryChannel.HasFlag(CampaignQueueItem.CampaignDeliveryChannel.Email)) { // TODO: Create next hop to send campaign via email. return; } if (campaign.DeliveryChannel.HasFlag(CampaignQueueItem.CampaignDeliveryChannel.SMS)) { // TODO: Create next hop to send campaign via SMS gateway. return; } }
/// <summary> /// Converts the current instance of <see cref="TranslationDictionary{T}"/> to it's JSON representation. /// </summary> public string ToJson() { if (this != null) { return(JsonSerializer.Serialize(this, JsonSerializerOptionDefaults.GetDefaultSettings())); } return(default);
/// <summary> /// Retrieves information from the <see cref="TempDataDictionary"/> using a specified key, without marking the key for deletion. /// </summary> /// <typeparam name="T">The type of data to retrieve.</typeparam> /// <param name="tempData">Represents a set of data that persists only from one request to the next.</param> /// <param name="key">The key to use in the <see cref="TempDataDictionary"/>.</param> /// <returns>The data persisted in the <see cref="TempDataDictionary"/> under the specified key.</returns> public static T Peek <T>(this ITempDataDictionary tempData, string key) { var item = tempData.Peek(key); if (item != null) { return(JsonSerializer.Deserialize <T>((string)item, JsonSerializerOptionDefaults.GetDefaultSettings())); } return(default);
/// <summary> /// Class constructor. /// </summary> /// <param name="provider">An interface that can be used to create <see cref="IDataProtector"/> instances.</param> public DataProtectionEncryptor(IDataProtectionProvider provider) { if (provider == null) { throw new ArgumentNullException(nameof(provider)); } _protector = provider.CreateProtector(typeof(T).FullName); _serializerOptions = JsonSerializerOptionDefaults.GetDefaultSettings(); }
/// <summary> /// Create a new <see cref="EventDispatcherAzure"/> instance. /// </summary> /// <param name="connectionString">The connection string to the Azure Storage account. By default it searches for <see cref="CONNECTION_STRING_NAME"/> application setting inside ConnectionStrings section.</param> /// <param name="environmentName">The environment name to use. Defaults to 'Production'.</param> /// <param name="enabled">Provides a way to enable/disable event dispatching at will. Defaults to true.</param> /// <param name="getUserFunc">Provides a way to access the current <see cref="ClaimsPrincipal"/> inside a service.</param> public EventDispatcherAzure(string connectionString, string environmentName, bool enabled, Func <ClaimsPrincipal> getUserFunc) { if (string.IsNullOrEmpty(connectionString)) { throw new ArgumentNullException(nameof(connectionString)); } if (string.IsNullOrEmpty(environmentName)) { _environmentName = "production"; } _enabled = enabled; _environmentName = Regex.Replace(environmentName ?? "Development", @"\s+", "-").ToLowerInvariant(); _connectionString = connectionString; _getUserFunc = getUserFunc ?? throw new ArgumentNullException(nameof(getUserFunc)); _jsonSerializerOptions = JsonSerializerOptionDefaults.GetDefaultSettings(); }
/// <summary> /// Create a new <see cref="EventDispatcherAzure"/> instance. /// </summary> /// <param name="connectionString">The connection string to the Azure Storage account. By default it searches for <see cref="CONNECTION_STRING_NAME"/> application setting inside ConnectionStrings section.</param> /// <param name="environmentName">The environment name to use. Defaults to 'Production'.</param> /// <param name="enabled">Provides a way to enable/disable event dispatching at will. Defaults to true.</param> /// <param name="claimsPrincipalSelector">Provides a way to access the current <see cref="ClaimsPrincipal"/> inside a service.</param> /// <param name="tenantIdSelector">Provides a way to access the current tenant id if any.</param> public EventDispatcherAzure(string connectionString, string environmentName, bool enabled, Func <ClaimsPrincipal> claimsPrincipalSelector, Func <Guid?> tenantIdSelector) { if (string.IsNullOrEmpty(connectionString)) { throw new ArgumentNullException(nameof(connectionString)); } if (string.IsNullOrEmpty(environmentName)) { _environmentName = "production"; } _enabled = enabled; _environmentName = Regex.Replace(environmentName ?? "Development", @"\s+", "-").ToLowerInvariant(); _connectionString = connectionString; _claimsPrincipalSelector = claimsPrincipalSelector ?? throw new ArgumentNullException(nameof(claimsPrincipalSelector)); _tenantIdSelector = tenantIdSelector ?? new Func <Guid?> (() => new Guid?()); _jsonSerializerOptions = JsonSerializerOptionDefaults.GetDefaultSettings(); }
public async static Task <HttpResponseData> Welcome( [HttpTrigger(AuthorizationLevel.Anonymous, "GET", Route = "welcome")] HttpRequestData request, FunctionContext executionContext ) { var logger = executionContext.GetLogger(FunctionNames.WelcomeHttpFunction); logger.LogInformation("Function '{FunctionName}' was triggered.", FunctionNames.WelcomeHttpFunction); var response = request.CreateResponse(HttpStatusCode.OK); response.Headers.Add(HeaderNames.Date, DateTime.Now.ToUniversalTime().ToString("R")); await response.WriteAsJsonAsync(new WelcomeMessage { Id = Guid.NewGuid(), Text = "Welcome to Azure functions using .NET 6" }, new JsonObjectSerializer(JsonSerializerOptionDefaults.GetDefaultSettings())); return(response); }
/// <summary> /// Json options defaults. /// </summary> public static JsonSerializerOptions GetDefaultSettings() => JsonSerializerOptionDefaults.GetDefaultSettings();
public async Task FunctionLockingTestDetail() { var operation = "MasterProductImport"; // using a random name :) var bytes = await _FileService.GetAsync($"messages/{operation}.json"); var message = JsonSerializer.Deserialize <(string LeaseId, string Name)>(Encoding.UTF8.GetString(bytes), JsonSerializerOptionDefaults.GetDefaultSettings()); var @lock = await _LockManager.Renew(message.Name, message.LeaseId); await Task.Delay(TimeSpan.FromSeconds(10)); }
public async Task SendPushNotificationHandler( [QueueTrigger("%ENVIRONMENT%-" + QueueNames.SendPushNotification, Connection = "StorageConnection")] string message, FunctionContext executionContext ) { var logger = executionContext.GetLogger(FunctionNames.SendPushNotification); logger.LogInformation("Function '{FunctionName}' was triggered.", FunctionNames.SendPushNotification); var pushNotification = JsonSerializer.Deserialize <PushNotificationQueueItem>(message, JsonSerializerOptionDefaults.GetDefaultSettings()); var data = pushNotification.Campaign?.Data ?? new ExpandoObject(); var dataDictionary = data as IDictionary <string, object>; if (!dataDictionary.ContainsKey("id")) { data.TryAdd("id", pushNotification.Campaign.Id); } if (pushNotification.Broadcast) { await PushNotificationService.BroadcastAsync(pushNotification.Campaign.Title, pushNotification.Campaign.Content, data, pushNotification.Campaign?.Type?.Name); } else { await PushNotificationService.SendAsync(pushNotification.Campaign.Title, pushNotification.Campaign.Content, data, pushNotification.UserCode, classification : pushNotification.Campaign?.Type?.Name); } }
/// <summary> /// Retrieves information from the <see cref="TempDataDictionary"/> using a specified key. /// </summary> /// <typeparam name="T">The type of data to retrieve.</typeparam> /// <param name="tempData">Represents a set of data that persists only from one request to the next.</param> /// <param name="key">The key to use in the <see cref="TempDataDictionary"/>.</param> /// <returns>The data persisted in the <see cref="TempDataDictionary"/> under the specified key.</returns> public static T Get <T>(this ITempDataDictionary tempData, string key) { tempData.TryGetValue(key, out var @object); return(@object == null ? default : JsonSerializer.Deserialize <T>((string)@object, JsonSerializerOptionDefaults.GetDefaultSettings())); }
/// <summary> /// Adds information to the <see cref="TempDataDictionary"/> using a specified key. /// </summary> /// <typeparam name="T">The type of data to persist.</typeparam> /// <param name="tempData">Represents a set of data that persists only from one request to the next.</param> /// <param name="key">The key to use in the <see cref="TempDataDictionary"/>.</param> /// <param name="value">The value to persist.</param> public static void Put <T>(this ITempDataDictionary tempData, string key, T value) => tempData[key] = JsonSerializer.Serialize(value, JsonSerializerOptionDefaults.GetDefaultSettings());
/// <inheritdoc/> static JsonStringValueConverter() { _serializerOptions = JsonSerializerOptionDefaults.GetDefaultSettings(); }
/// <summary> /// Send notifications to devices registered to userId with payload data and classification. /// </summary> /// <typeparam name="TData">The type of data sent in the notification payload.</typeparam> /// <param name="service">Instance of <see cref="IPushNotificationService"/>.</param> /// <param name="title">Message of notification.</param> /// <param name="body">Body of notification.</param> /// <param name="data">Data passed to mobile client, not visible to notification toast.</param> /// <param name="userTag">UserId to be passed as tag.</param> /// <param name="classification">The type of the Push Notification.</param> /// <param name="tags">Optional tag parameters.</param> public static Task SendAsync <TData>(this IPushNotificationService service, string title, string body, TData data, string userTag, string classification = null, params string[] tags) where TData : class => service.SendAsync(title, body, data != null ? JsonSerializer.Serialize(data, JsonSerializerOptionDefaults.GetDefaultSettings()) : null, userTag, classification, tags);
/// <summary> /// Sends a notification to all registered devices. /// </summary> /// <typeparam name="TData">The type of data sent in the notification payload.</typeparam> /// <param name="service">Instance of <see cref="IPushNotificationService"/>.</param> /// <param name="title">Message of notification.</param> /// <param name="body">Body of notification.</param> /// <param name="data">Data passed to mobile client, not visible to notification toast.</param> /// <param name="classification">The type of the Push Notification.</param> public static Task BroadcastAsync <TData>(this IPushNotificationService service, string title, string body, TData data, string classification = null) where TData : class => service.BroadcastAsync(title, body, data != null ? JsonSerializer.Serialize(data, JsonSerializerOptionDefaults.GetDefaultSettings()) : null, classification);