123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- using Azure.Core.Serialization;
- using Microsoft.Azure.Cosmos;
- using Microsoft.Extensions.Logging;
- using Microsoft.Extensions.Options;
- using System;
- using System.Collections.Concurrent;
- using System.Text.Json;
- using System.Text.Json.Serialization;
- using System.IO;
- 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
- {
- //CosmosClientOptions 的 SerializerOptions = new CosmosSerializationOptions() { PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase }
- //需等待官方修正
- JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions()
- {
- DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
- };
- var cm = CosmosClients.GetOrAdd(name, x => new CosmosClient(_optionsMonitor.Get(name).CosmosConnectionString, new CosmosClientOptions()
- {
- // Serializer= new CosmosSystemTextJsonSerializer(jsonSerializerOptions),
- ApplicationRegion = region
- }));
- return cm;
- }
- catch (Exception e)
- {
- _logger?.LogWarning(e, e.Message);
- throw;
- }
- }
- }
- /// <summary>
- /// Uses <see cref="Azure.Core.Serialization.JsonObjectSerializer"/> which leverages System.Text.Json providing a simple API to interact with on the Azure SDKs.
- /// </summary>
- // <SystemTextJsonSerializer>
- public class CosmosSystemTextJsonSerializer : CosmosSerializer
- {
- private readonly JsonObjectSerializer systemTextJsonSerializer;
- public CosmosSystemTextJsonSerializer(JsonSerializerOptions jsonSerializerOptions)
- {
- this.systemTextJsonSerializer = new JsonObjectSerializer(jsonSerializerOptions);
- }
- public override T FromStream<T>(Stream stream)
- {
- using (stream)
- {
- if (stream.CanSeek
- && stream.Length == 0)
- {
- return default;
- }
- if (typeof(Stream).IsAssignableFrom(typeof(T)))
- {
- return (T)(object)stream;
- }
- return (T)this.systemTextJsonSerializer.Deserialize(stream, typeof(T), default);
- }
- }
- public override Stream ToStream<T>(T input)
- {
- if (input!= null)
- {
- MemoryStream streamPayload = new MemoryStream();
- this.systemTextJsonSerializer.Serialize(streamPayload, input, input.GetType(), default);
- streamPayload.Position = 0;
- return streamPayload;
- }
- else {
- return new MemoryStream();
- }
- }
- }
- // </SystemTextJsonSerializer>
- }
|