SmiContextFactory.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //------------------------------------------------------------------------------
  2. // <copyright file="SmiContextFactory.cs" company="Microsoft">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. // <owner current="true" primary="true">Microsoft</owner>
  6. // <owner current="true" primary="false">Microsoft</owner>
  7. //------------------------------------------------------------------------------
  8. namespace Microsoft.SqlServer.Server {
  9. using System;
  10. using System.Data.Common;
  11. using System.Data.SqlClient;
  12. using System.Diagnostics;
  13. sealed internal class SmiContextFactory {
  14. public static readonly SmiContextFactory Instance = new SmiContextFactory();
  15. private readonly SmiLink _smiLink;
  16. private readonly ulong _negotiatedSmiVersion;
  17. private readonly byte _majorVersion;
  18. private readonly byte _minorVersion;
  19. private readonly short _buildNum;
  20. private readonly string _serverVersion;
  21. private readonly SmiEventSink_Default _eventSinkForGetCurrentContext;
  22. internal const ulong YukonVersion = 100;
  23. internal const ulong KatmaiVersion = 210;
  24. internal const ulong LatestVersion = KatmaiVersion;
  25. private readonly ulong[] __supportedSmiVersions = new ulong[] {YukonVersion, KatmaiVersion};
  26. // Used as the key for SmiContext.GetContextValue()
  27. internal enum ContextKey {
  28. Connection = 0,
  29. SqlContext = 1
  30. }
  31. private SmiContextFactory() {
  32. if (InOutOfProcHelper.InProc) {
  33. Type smiLinkType = Type.GetType("Microsoft.SqlServer.Server.InProcLink, SqlAccess, PublicKeyToken=89845dcd8080cc91");
  34. if (null == smiLinkType) {
  35. Debug.Assert(false, "could not get InProcLink type");
  36. throw SQL.ContextUnavailableOutOfProc(); // Must not be a valid version of Sql Server.
  37. }
  38. System.Reflection.FieldInfo instanceField = GetStaticField(smiLinkType, "Instance");
  39. if (instanceField != null) {
  40. _smiLink = (SmiLink)GetValue(instanceField);
  41. }
  42. else {
  43. Debug.Assert(false, "could not get InProcLink.Instance");
  44. throw SQL.ContextUnavailableOutOfProc(); // Must not be a valid version of Sql Server.
  45. }
  46. System.Reflection.FieldInfo buildVersionField = GetStaticField(smiLinkType, "BuildVersion");
  47. if (buildVersionField != null) {
  48. UInt32 buildVersion = (UInt32)GetValue(buildVersionField);
  49. _majorVersion = (byte)(buildVersion >> 24);
  50. _minorVersion = (byte)((buildVersion >> 16) & 0xff);
  51. _buildNum = (short)(buildVersion & 0xffff);
  52. _serverVersion = (String.Format((IFormatProvider)null, "{0:00}.{1:00}.{2:0000}", _majorVersion, (short) _minorVersion, _buildNum));
  53. }
  54. else {
  55. _serverVersion = String.Empty; // default value if nothing exists.
  56. }
  57. _negotiatedSmiVersion = _smiLink.NegotiateVersion(SmiLink.InterfaceVersion);
  58. bool isSupportedVersion = false;
  59. for(int i=0; !isSupportedVersion && i<__supportedSmiVersions.Length; i++) {
  60. if (__supportedSmiVersions[i] == _negotiatedSmiVersion) {
  61. isSupportedVersion = true;
  62. }
  63. }
  64. // Disconnect if we didn't get a supported version!!
  65. if (!isSupportedVersion) {
  66. _smiLink = null;
  67. }
  68. _eventSinkForGetCurrentContext = new SmiEventSink_Default();
  69. }
  70. }
  71. internal ulong NegotiatedSmiVersion {
  72. get {
  73. if (null == _smiLink) {
  74. throw SQL.ContextUnavailableOutOfProc(); // Must not be a valid version of Sql Server, or not be SqlCLR
  75. }
  76. return _negotiatedSmiVersion;
  77. }
  78. }
  79. internal string ServerVersion {
  80. get {
  81. if (null == _smiLink) {
  82. throw SQL.ContextUnavailableOutOfProc(); // Must not be a valid version of Sql Server, or not be SqlCLR
  83. }
  84. return _serverVersion;
  85. }
  86. }
  87. internal SmiContext GetCurrentContext() {
  88. if (null == _smiLink) {
  89. throw SQL.ContextUnavailableOutOfProc(); // Must not be a valid version of Sql Server, or not be SqlCLR
  90. }
  91. object result = _smiLink.GetCurrentContext(_eventSinkForGetCurrentContext);
  92. _eventSinkForGetCurrentContext.ProcessMessagesAndThrow();
  93. if (null == result) {
  94. throw SQL.ContextUnavailableWhileInProc();
  95. }
  96. Debug.Assert(typeof(SmiContext).IsInstanceOfType(result), "didn't get SmiContext from GetCurrentContext?");
  97. return (SmiContext)result;
  98. }
  99. [System.Security.Permissions.ReflectionPermission(System.Security.Permissions.SecurityAction.Assert, MemberAccess=true)]
  100. private object GetValue(System.Reflection.FieldInfo fieldInfo) {
  101. object result = fieldInfo.GetValue(null);
  102. return result;
  103. }
  104. [System.Security.Permissions.ReflectionPermission(System.Security.Permissions.SecurityAction.Assert, MemberAccess=true)]
  105. private System.Reflection.FieldInfo GetStaticField(Type aType, string fieldName) {
  106. System.Reflection.FieldInfo result = aType.GetField(fieldName, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.GetField);
  107. return result;
  108. }
  109. }
  110. }