CosmosDBClientSingleton.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using Microsoft.Azure.Documents.Client;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace HaBookCms.AzureCosmos.CosmosDB
  6. {
  7. public sealed class CosmosDBClientSingleton
  8. {
  9. private static string _connectionUrl;
  10. private static string _connectionKey;
  11. private DocumentClient CosmosClient;
  12. private CosmosDBClientSingleton() { }
  13. public DocumentClient GetCosmosDBClient()
  14. {
  15. if (CosmosClient != null)
  16. {
  17. return CosmosClient;
  18. }
  19. else
  20. {
  21. getInstance(_connectionUrl, _connectionKey);
  22. return CosmosClient;
  23. }
  24. }
  25. public static CosmosDBClientSingleton getInstance(string connectionUrl,string connectionKey)
  26. {
  27. _connectionUrl = connectionUrl;
  28. _connectionKey = connectionKey;
  29. return SingletonInstance.instance;
  30. }
  31. private static class SingletonInstance
  32. {
  33. public static CosmosDBClientSingleton instance = new CosmosDBClientSingleton()
  34. {
  35. CosmosClient = new DocumentClient(new Uri(_connectionUrl), _connectionKey)
  36. };
  37. }
  38. }
  39. }