12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- using Azure;
- using Azure.Cosmos;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.Logging;
- using Microsoft.Extensions.Options;
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using TEAMModelOS.Models;
- using TEAMModelOS.SDK.Context.Attributes.Azure;
- using TEAMModelOS.SDK.Context.Configuration;
- using TEAMModelOS.SDK.DI.AzureCosmos.Inner;
- using TEAMModelOS.SDK.Helper.Common.ReflectorExtensions;
- using System.Diagnostics;
- using System.IO;
- using System.Linq.Expressions;
- using System.Net;
- using System.Reflection;
- using System.Text;
- using System.Text.Json;
- using System.Threading;
- using TEAMModelOS.SDK.Context.Exception;
- using TEAMModelOS.SDK.DI;
- using Azure.Cosmos.Serialization;
- namespace TEAMModelOS.SDK.DI
- {
- public class AzureCosmosFactory
- {
- private readonly IServiceProvider _services;
- private readonly IOptionsMonitor<AzureCosmosFactoryOptions> _optionsMonitor;
- private readonly ILogger _logger;
- //private Option _option;
- private ConcurrentDictionary<string, CosmosClient> CosmosClients { get; } = new ConcurrentDictionary<string, CosmosClient>();
-
- // private CosmosDatabase database { get; set; }
-
- public AzureCosmosFactory(IServiceProvider services, IOptionsMonitor<AzureCosmosFactoryOptions> optionsMonitor, ILogger<AzureCosmosFactory> logger)
- {
- if (services == null) throw new ArgumentNullException(nameof(services));
- if (optionsMonitor == null) throw new ArgumentNullException(nameof(optionsMonitor));
- _services = services;
- _optionsMonitor = optionsMonitor;
- _logger = logger;
- }
- /// <summary>
- /// 取得CosmosClient,支持安全執行緒
- /// </summary>
- /// <param name="name"></param>
- /// <param name="region">可以使用Regions.{區域}設置,指定此屬性後,SDK會首選該區域來執行操作。此外,SDK會自動選擇後備的地理複製區域以實現高可用性。如果未指定此屬性,則SDK會將寫區域用作所有操作的首選區域</param>
- /// <returns></returns>
- public CosmosClient GetCosmosClient(string region = null, string name = "Default")
- {
- try
- {
- var cm = CosmosClients.GetOrAdd(name, x => new CosmosClient(_optionsMonitor.Get(name).CosmosConnectionString, new CosmosClientOptions() { ApplicationRegion = region, SerializerOptions = new CosmosSerializationOptions() { PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase } }));
- return cm;
- }
- catch (Exception e)
- {
- _logger?.LogWarning(e, e.Message);
- return null;
- }
- }
- }
- }
|