ClientLibraryUtils.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // -
  2. // <copyright file="ClientLibraryUtils.cs" company="Microsoft Corporation">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. // -
  6. namespace Microsoft.Hawaii
  7. {
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Reflection;
  12. using System.Security;
  13. /// <summary>
  14. /// A class to store utility functions used by the client libraries.
  15. /// </summary>
  16. public static class ClientLibraryUtils
  17. {
  18. /// <summary>
  19. /// Looks for service scope.
  20. /// </summary>
  21. /// <param name="configFilePath">The config file path to look for.</param>
  22. /// <param name="defaultServiceScope">The default value to return if the config file does not exist.</param>
  23. /// <returns>Returns the service service scope.</returns>
  24. [SecuritySafeCritical]
  25. public static string LookupServiceScopeFromConfig(string configFilePath, string defaultServiceScope)
  26. {
  27. return LookupConfigFromFile(configFilePath, defaultServiceScope);
  28. }
  29. /// <summary>
  30. /// Looks for service host name.
  31. /// </summary>
  32. /// <param name="configFilePath">The config file path to look for.</param>
  33. /// <param name="defaultHostName">The default value to return if the config file does not exist.</param>
  34. /// <returns>Returns the service host name</returns>
  35. [SecuritySafeCritical]
  36. public static string LookupHostNameFromConfig(string configFilePath, string defaultHostName)
  37. {
  38. return LookupConfigFromFile(configFilePath, defaultHostName);
  39. }
  40. /// <summary>
  41. /// The function is used on a desktop client to allow override of defautl service path.
  42. /// It looks for settings within specified file. If it does not exist, it returns the default.
  43. /// The [SecurityCritical] sttribute is needed due to CA2140 analysis rules.
  44. /// </summary>
  45. /// <param name="configFilePath">The config file path to look for.</param>
  46. /// <param name="defaultValue">The default value to return if the config file does not exist.</param>
  47. /// <returns>The config value specified in the config file, if present. The default otherwise.</returns>
  48. [SecuritySafeCritical]
  49. public static string LookupConfigFromFile(string configFilePath, string defaultValue)
  50. {
  51. // Use reflection to invoke Win32-specific code on Win32 Platform.
  52. // If we run on Win32, then look for files on the disk to get the config value
  53. Type fileType = Type.GetType("System.IO.File", false);
  54. if (fileType == null)
  55. {
  56. return defaultValue;
  57. }
  58. MethodInfo existsMethodInfo = null;
  59. MethodInfo readLinesMethodInfo = null;
  60. #if !NETFX_CORE // not Win8
  61. existsMethodInfo = fileType.GetMethod("Exists");
  62. // get the method for File.Readlines(string)
  63. readLinesMethodInfo = fileType.GetMethod("ReadLines", new Type[] { typeof(string) });
  64. #endif
  65. if (existsMethodInfo == null || readLinesMethodInfo == null)
  66. {
  67. return defaultValue;
  68. }
  69. // The following try/finally block is needed to avoid CA2202 and CA2000 warnings.
  70. try
  71. {
  72. bool fileExists = (bool)existsMethodInfo.Invoke(null, new object[] { configFilePath });
  73. if (fileExists)
  74. {
  75. IEnumerable<string> lines = (IEnumerable<string>)readLinesMethodInfo.Invoke(null, new object[] { configFilePath });
  76. foreach (string line in lines)
  77. {
  78. if (!string.IsNullOrEmpty(line))
  79. {
  80. return line;
  81. }
  82. else
  83. {
  84. break;
  85. }
  86. }
  87. }
  88. }
  89. catch (IOException)
  90. {
  91. // If we hit an IO exception, we fall back to the default.
  92. }
  93. catch (InvalidOperationException)
  94. {
  95. // on Win8 XAML app
  96. }
  97. catch (UnauthorizedAccessException)
  98. {
  99. // If we hit an access exception when reading the config, use the default.
  100. // These catches are not expected to be hit, but mainly here to keep from
  101. // crashing if somebody outside the test team manages to create these exact
  102. // files but not have read permissions set.
  103. }
  104. // Otherwise use the default.
  105. return defaultValue;
  106. }
  107. }
  108. }