Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / whidbey / NetFXspW7 / ndp / fx / src / Data / Microsoft / SqlServer / Server / SmiContextFactory.cs / 1 / SmiContextFactory.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// [....]
// [....]
//-----------------------------------------------------------------------------
namespace Microsoft.SqlServer.Server {
using System;
using System.Data.Common;
using System.Data.SqlClient;
using System.Diagnostics;
sealed internal class SmiContextFactory {
public static readonly SmiContextFactory Instance = new SmiContextFactory();
private readonly SmiLink _smiLink;
private readonly ulong _negotiatedSmiVersion;
private readonly byte _majorVersion;
private readonly byte _minorVersion;
private readonly short _buildNum;
private readonly string _serverVersion;
private readonly SmiEventSink_Default _eventSinkForGetCurrentContext;
internal const ulong YukonVersion = 100;
internal const ulong KatmaiVersion = 210;
internal const ulong LatestVersion = KatmaiVersion;
private readonly ulong[] __supportedSmiVersions = new ulong[] {YukonVersion, KatmaiVersion};
// Used as the key for SmiContext.GetContextValue()
internal enum ContextKey {
Connection = 0,
SqlContext = 1
}
private SmiContextFactory() {
if (InOutOfProcHelper.InProc) {
Type smiLinkType = Type.GetType("Microsoft.SqlServer.Server.InProcLink, SqlAccess, PublicKeyToken=89845dcd8080cc91");
if (null == smiLinkType) {
Debug.Assert(false, "could not get InProcLink type");
throw SQL.ContextUnavailableOutOfProc(); // Must not be a valid version of Sql Server.
}
System.Reflection.FieldInfo instanceField = GetStaticField(smiLinkType, "Instance");
if (instanceField != null) {
_smiLink = (SmiLink)GetValue(instanceField);
}
else {
Debug.Assert(false, "could not get InProcLink.Instance");
throw SQL.ContextUnavailableOutOfProc(); // Must not be a valid version of Sql Server.
}
System.Reflection.FieldInfo buildVersionField = GetStaticField(smiLinkType, "BuildVersion");
if (buildVersionField != null) {
UInt32 buildVersion = (UInt32)GetValue(buildVersionField);
_majorVersion = (byte)(buildVersion >> 24);
_minorVersion = (byte)((buildVersion >> 16) & 0xff);
_buildNum = (short)(buildVersion & 0xffff);
_serverVersion = (String.Format((IFormatProvider)null, "{0:00}.{1:00}.{2:0000}", _majorVersion, (short) _minorVersion, _buildNum));
}
else {
_serverVersion = String.Empty; // default value if nothing exists.
}
_negotiatedSmiVersion = _smiLink.NegotiateVersion(SmiLink.InterfaceVersion);
bool isSupportedVersion = false;
for(int i=0; !isSupportedVersion && i<__supportedSmiVersions.Length; i++) {
if (__supportedSmiVersions[i] == _negotiatedSmiVersion) {
isSupportedVersion = true;
}
}
// Disconnect if we didn't get a supported version!!
if (!isSupportedVersion) {
_smiLink = null;
}
_eventSinkForGetCurrentContext = new SmiEventSink_Default();
}
}
internal ulong NegotiatedSmiVersion {
get {
if (null == _smiLink) {
throw SQL.ContextUnavailableOutOfProc(); // Must not be a valid version of Sql Server, or not be SqlCLR
}
return _negotiatedSmiVersion;
}
}
internal string ServerVersion {
get {
if (null == _smiLink) {
throw SQL.ContextUnavailableOutOfProc(); // Must not be a valid version of Sql Server, or not be SqlCLR
}
return _serverVersion;
}
}
internal SmiContext GetCurrentContext() {
if (null == _smiLink) {
throw SQL.ContextUnavailableOutOfProc(); // Must not be a valid version of Sql Server, or not be SqlCLR
}
object result = _smiLink.GetCurrentContext(_eventSinkForGetCurrentContext);
_eventSinkForGetCurrentContext.ProcessMessagesAndThrow();
if (null == result) {
throw SQL.ContextUnavailableWhileInProc();
}
Debug.Assert(typeof(SmiContext).IsInstanceOfType(result), "didn't get SmiContext from GetCurrentContext?");
return (SmiContext)result;
}
[System.Security.Permissions.ReflectionPermission(System.Security.Permissions.SecurityAction.Assert, MemberAccess=true)]
private object GetValue(System.Reflection.FieldInfo fieldInfo) {
object result = fieldInfo.GetValue(null);
return result;
}
[System.Security.Permissions.ReflectionPermission(System.Security.Permissions.SecurityAction.Assert, MemberAccess=true)]
private System.Reflection.FieldInfo GetStaticField(Type aType, string fieldName) {
System.Reflection.FieldInfo result = aType.GetField(fieldName, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.GetField);
return result;
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// [....]
// [....]
//-----------------------------------------------------------------------------
namespace Microsoft.SqlServer.Server {
using System;
using System.Data.Common;
using System.Data.SqlClient;
using System.Diagnostics;
sealed internal class SmiContextFactory {
public static readonly SmiContextFactory Instance = new SmiContextFactory();
private readonly SmiLink _smiLink;
private readonly ulong _negotiatedSmiVersion;
private readonly byte _majorVersion;
private readonly byte _minorVersion;
private readonly short _buildNum;
private readonly string _serverVersion;
private readonly SmiEventSink_Default _eventSinkForGetCurrentContext;
internal const ulong YukonVersion = 100;
internal const ulong KatmaiVersion = 210;
internal const ulong LatestVersion = KatmaiVersion;
private readonly ulong[] __supportedSmiVersions = new ulong[] {YukonVersion, KatmaiVersion};
// Used as the key for SmiContext.GetContextValue()
internal enum ContextKey {
Connection = 0,
SqlContext = 1
}
private SmiContextFactory() {
if (InOutOfProcHelper.InProc) {
Type smiLinkType = Type.GetType("Microsoft.SqlServer.Server.InProcLink, SqlAccess, PublicKeyToken=89845dcd8080cc91");
if (null == smiLinkType) {
Debug.Assert(false, "could not get InProcLink type");
throw SQL.ContextUnavailableOutOfProc(); // Must not be a valid version of Sql Server.
}
System.Reflection.FieldInfo instanceField = GetStaticField(smiLinkType, "Instance");
if (instanceField != null) {
_smiLink = (SmiLink)GetValue(instanceField);
}
else {
Debug.Assert(false, "could not get InProcLink.Instance");
throw SQL.ContextUnavailableOutOfProc(); // Must not be a valid version of Sql Server.
}
System.Reflection.FieldInfo buildVersionField = GetStaticField(smiLinkType, "BuildVersion");
if (buildVersionField != null) {
UInt32 buildVersion = (UInt32)GetValue(buildVersionField);
_majorVersion = (byte)(buildVersion >> 24);
_minorVersion = (byte)((buildVersion >> 16) & 0xff);
_buildNum = (short)(buildVersion & 0xffff);
_serverVersion = (String.Format((IFormatProvider)null, "{0:00}.{1:00}.{2:0000}", _majorVersion, (short) _minorVersion, _buildNum));
}
else {
_serverVersion = String.Empty; // default value if nothing exists.
}
_negotiatedSmiVersion = _smiLink.NegotiateVersion(SmiLink.InterfaceVersion);
bool isSupportedVersion = false;
for(int i=0; !isSupportedVersion && i<__supportedSmiVersions.Length; i++) {
if (__supportedSmiVersions[i] == _negotiatedSmiVersion) {
isSupportedVersion = true;
}
}
// Disconnect if we didn't get a supported version!!
if (!isSupportedVersion) {
_smiLink = null;
}
_eventSinkForGetCurrentContext = new SmiEventSink_Default();
}
}
internal ulong NegotiatedSmiVersion {
get {
if (null == _smiLink) {
throw SQL.ContextUnavailableOutOfProc(); // Must not be a valid version of Sql Server, or not be SqlCLR
}
return _negotiatedSmiVersion;
}
}
internal string ServerVersion {
get {
if (null == _smiLink) {
throw SQL.ContextUnavailableOutOfProc(); // Must not be a valid version of Sql Server, or not be SqlCLR
}
return _serverVersion;
}
}
internal SmiContext GetCurrentContext() {
if (null == _smiLink) {
throw SQL.ContextUnavailableOutOfProc(); // Must not be a valid version of Sql Server, or not be SqlCLR
}
object result = _smiLink.GetCurrentContext(_eventSinkForGetCurrentContext);
_eventSinkForGetCurrentContext.ProcessMessagesAndThrow();
if (null == result) {
throw SQL.ContextUnavailableWhileInProc();
}
Debug.Assert(typeof(SmiContext).IsInstanceOfType(result), "didn't get SmiContext from GetCurrentContext?");
return (SmiContext)result;
}
[System.Security.Permissions.ReflectionPermission(System.Security.Permissions.SecurityAction.Assert, MemberAccess=true)]
private object GetValue(System.Reflection.FieldInfo fieldInfo) {
object result = fieldInfo.GetValue(null);
return result;
}
[System.Security.Permissions.ReflectionPermission(System.Security.Permissions.SecurityAction.Assert, MemberAccess=true)]
private System.Reflection.FieldInfo GetStaticField(Type aType, string fieldName) {
System.Reflection.FieldInfo result = aType.GetField(fieldName, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.GetField);
return result;
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- QueryExtender.cs
- UInt64Storage.cs
- RelationshipType.cs
- WebBrowserContainer.cs
- XmlSerializerSection.cs
- AlphabeticalEnumConverter.cs
- HttpInputStream.cs
- InvalidEnumArgumentException.cs
- ThreadExceptionDialog.cs
- SimpleHandlerFactory.cs
- Int64AnimationUsingKeyFrames.cs
- FrameworkTextComposition.cs
- TraceEventCache.cs
- ZipIOBlockManager.cs
- ColorIndependentAnimationStorage.cs
- RadioButtonRenderer.cs
- CommandConverter.cs
- AsymmetricKeyExchangeFormatter.cs
- MouseWheelEventArgs.cs
- BooleanToVisibilityConverter.cs
- ResourceReferenceKeyNotFoundException.cs
- PlanCompilerUtil.cs
- DbMetaDataColumnNames.cs
- CustomError.cs
- StreamSecurityUpgradeInitiator.cs
- ValueType.cs
- ContainerFilterService.cs
- RenderOptions.cs
- ResourceDictionaryCollection.cs
- wmiprovider.cs
- PasswordPropertyTextAttribute.cs
- VersionedStream.cs
- Visitors.cs
- DataFormat.cs
- TemplateBuilder.cs
- TemplateBindingExpressionConverter.cs
- ArraySet.cs
- BitmapEffectInputData.cs
- WindowsAltTab.cs
- TaskForm.cs
- ConnectionInterfaceCollection.cs
- Base64Encoding.cs
- MembershipSection.cs
- NamedPipeChannelFactory.cs
- CodeTypeParameter.cs
- WsatAdminException.cs
- Quaternion.cs
- CheckBoxBaseAdapter.cs
- OdbcHandle.cs
- DBCommand.cs
- StateDesigner.LayoutSelectionGlyph.cs
- HttpContext.cs
- BuildManager.cs
- TaskFileService.cs
- UrlPath.cs
- WasAdminWrapper.cs
- EmissiveMaterial.cs
- DataColumnMappingCollection.cs
- OraclePermissionAttribute.cs
- PageParser.cs
- EmitterCache.cs
- XXXOnTypeBuilderInstantiation.cs
- FillRuleValidation.cs
- DoubleUtil.cs
- RangeValuePattern.cs
- ProviderUtil.cs
- XmlWhitespace.cs
- XamlInterfaces.cs
- KeyValuePair.cs
- ResourcesGenerator.cs
- InputScopeAttribute.cs
- WindowsToolbarAsMenu.cs
- UseLicense.cs
- SeparatorAutomationPeer.cs
- InternalBase.cs
- AutoGeneratedField.cs
- FontStretch.cs
- DesigntimeLicenseContext.cs
- ToolBarButtonDesigner.cs
- ListControlBoundActionList.cs
- MenuBindingsEditorForm.cs
- SelectedDatesCollection.cs
- InternalConfigRoot.cs
- RepeatButtonAutomationPeer.cs
- CodeMemberMethod.cs
- LicenseManager.cs
- parserscommon.cs
- DoubleAnimation.cs
- HMACSHA512.cs
- ToolBarButtonDesigner.cs
- KnownTypeHelper.cs
- cookiecontainer.cs
- UdpReplyToBehavior.cs
- OdbcConnectionStringbuilder.cs
- ImageFormatConverter.cs
- EditingContext.cs
- NotFiniteNumberException.cs
- AllMembershipCondition.cs
- DbDataAdapter.cs
- EncoderBestFitFallback.cs