Code:
/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / Tools / WSATConfig / Configuration / MsdtcWrapper.cs / 1 / MsdtcWrapper.cs
//------------------------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- namespace Microsoft.Tools.ServiceModel.WsatConfig { using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using System.Runtime.InteropServices.ComTypes; using System.Management; using System.ServiceProcess; using System.IO; class MsdtcWrapper { // the wrapper cache. one entry for one machine name // the cache is ever-growing in size, but this does not matter given the characteristics // the program static Dictionarywrappers = new Dictionary (); // the GUID of the IDtcNetworkAccessConfig interface static Guid IDtcNetworkAccessConfigInterfaceId = typeof(IDtcNetworkAccessConfig).GUID; IDtcNetworkAccessConfig proxy; ConfigurationProvider configProvider; ServiceController controller; string machineName; string virtualServerName; static string GetTransactionManagerHostName(string machineName, string virtualServerName) { if (MsdtcClusterUtils.IsClusterServer(machineName)) { if (!string.IsNullOrEmpty(virtualServerName)) { return virtualServerName; } return Utilities.IsLocalMachineName(machineName) ? Utilities.LocalHostName : machineName; } return Utilities.IsLocalMachineName(machineName) ? string.Empty : machineName; } internal static MsdtcWrapper GetWrapper(string machineName, string virtualServerName, ConfigurationProvider configProvider) { MsdtcWrapper wrapper; string key = GetTransactionManagerHostName(machineName, virtualServerName); wrappers.TryGetValue(key, out wrapper); if (wrapper == null) { wrapper = new MsdtcWrapper(machineName, virtualServerName, configProvider); wrappers.Add(key, wrapper); } return wrapper; } MsdtcWrapper(string machineName, string virtualServerName, ConfigurationProvider configProvider) { if (configProvider == null) { throw new ArgumentNullException("configProvider"); } this.machineName = machineName; this.virtualServerName = virtualServerName; this.configProvider = configProvider; this.proxy = null; int hr; string hostName = GetTransactionManagerHostName(machineName, virtualServerName); if (IsLongHornClusterLocalDtc(machineName, virtualServerName)) { SafeNativeMethods.OLE_TM_CONFIG_PARAMS_V2 configParams = new SafeNativeMethods.OLE_TM_CONFIG_PARAMS_V2(); configParams.dwVersion = SafeNativeMethods.OLE_TM_CONFIG_VERSION_2; configParams.pwszClusterResourceName = "LOCAL"; hr = SafeNativeMethods.DtcGetTransactionManagerEx_WithConfigParams( hostName, string.Empty, ref IDtcNetworkAccessConfigInterfaceId, SafeNativeMethods.OLE_TM_FLAG_NONE, ref configParams, out proxy); } else { hr = SafeNativeMethods.DtcGetTransactionManagerEx( hostName, string.Empty, ref IDtcNetworkAccessConfigInterfaceId, SafeNativeMethods.OLE_TM_FLAG_NONE, IntPtr.Zero, out proxy); } if (proxy == null) { // Chances are we are on a XP machine; even on XP SP2 this interface is not available Utilities.Log("Unable to query IDtcNetworkAccessConfig" + hr); if (Utilities.IsLocalMachineName(machineName)) { controller = new System.ServiceProcess.ServiceController("MSDTC"); } else { // We do not use ServiceController to control remote services for secuirty concerns - use WMI remote process invocation to do that controller = null; } } } static bool IsLongHornClusterLocalDtc(string machineName, string virtualServerName) { if (Utilities.GetOSMajor(machineName) > 5 && MsdtcClusterUtils.IsClusterServer(machineName)) { if (string.IsNullOrEmpty(virtualServerName)) { return true; } } return false; } // Get whether the NetworkTransactions are enabled // it uses a fallback strategy: if the proxy could not be instantiated, it tries to read registry to find out internal bool GetNetworkTransactionAccess() { if (proxy != null) { try { bool retVal = false; proxy.GetNetworkTransactionAccess(ref retVal); return retVal; } catch (COMException e) { if (!IsLongHornClusterLocalDtc(machineName, virtualServerName)) { throw new WsatAdminException(WsatAdminErrorCode.CANNOT_GET_MSDTC_NETWORK_ACCESS_SETTING, SR.GetString(SR.ErrorCannotGetMsdtcNetworkAccessSetting, e.Message), e); } // Otherwise, it's very likely that, due to WinOS bug 1455344, we are not able to call // proxy.GetNetworkTransactionAccess on a local DTC TM of a LHS cluster. // The workaround is to determine network transaction access by msdtc security registry settings } catch (UnauthorizedAccessException e) { throw new WsatAdminException(WsatAdminErrorCode.MSDTC_NETWORK_ACCESS_DENIED, SR.GetString(SR.ErrorMsdtcNetworkAccessDenied), e); } catch (FileNotFoundException e) { throw new WsatAdminException(WsatAdminErrorCode.CANNOT_GET_MSDTC_NETWORK_ACCESS_SETTING, SR.GetString(SR.ErrorCannotGetMsdtcNetworkAccessSettingWithDiagnostics), e); } } return DetermineNetworkTransactionAccessByRegistrySettings(); } bool DetermineNetworkTransactionAccessByRegistrySettings() { ConfigurationProvider dtcSecurityConfigProvider = configProvider.OpenKey(WsatKeys.MsdtcSecurityKey); bool networkDtcAccess, networkDtcAccessInbound, networkDtcAccessOutbound, networkDtcAccessTransactions; networkDtcAccess = (dtcSecurityConfigProvider.ReadUInt32(NetworkDtcAccessRegValues.NetworkDtcAccess, 0) != 0); networkDtcAccessInbound = (dtcSecurityConfigProvider.ReadUInt32(NetworkDtcAccessRegValues.NetworkDtcAccessInbound, 0) != 0); networkDtcAccessOutbound = (dtcSecurityConfigProvider.ReadUInt32(NetworkDtcAccessRegValues.NetworkDtcAccessOutbound, 0) != 0); networkDtcAccessTransactions = (dtcSecurityConfigProvider.ReadUInt32(NetworkDtcAccessRegValues.NetworkDtcAccessTransactions, 0) != 0); return (networkDtcAccess && (networkDtcAccessInbound || networkDtcAccessOutbound) && networkDtcAccessTransactions); } static class NetworkDtcAccessRegValues { public const string NetworkDtcAccess = "NetworkDtcAccess"; public const string NetworkDtcAccessInbound = "NetworkDtcAccessInbound"; public const string NetworkDtcAccessOutbound = "NetworkDtcAccessOutbound"; public const string NetworkDtcAccessTransactions = "NetworkDtcAccessTransactions"; } // Restarts the MSDTC_NOTIF for the local/remote machine // it uses a fallback strategy: if the proxy could not be instantiated, // it tries will restart MSDTC_NOTIF by using the ServiceController class // The proxy ONLY works on Windows VISTA internal void RestartDtcService() { if (proxy != null) { int hr = proxy.RestartDtcService(); if (hr != 0) { throw new WsatAdminException(WsatAdminErrorCode.DTC_RESTART_ERROR, SR.GetString(SR.ErrorRestartMSDTCWithErrorCode, hr)); } } else { System.Diagnostics.Debug.Assert(controller != null, "Do not call RestartDtcService for remote machines, use SaveRemote instead!"); if (controller.Status != System.ServiceProcess.ServiceControllerStatus.Stopped) { if (controller.Status != System.ServiceProcess.ServiceControllerStatus.StopPending) { if (controller.CanStop) { controller.Stop(); } else { throw new WsatAdminException(WsatAdminErrorCode.DTC_RESTART_ERROR, SR.GetString(SR.ErrorRestartMSDTC)); } } controller.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(30)); if (controller.Status != System.ServiceProcess.ServiceControllerStatus.Stopped) { throw new WsatAdminException(WsatAdminErrorCode.DTC_RESTART_ERROR, SR.GetString(SR.ErrorRestartMSDTC)); } } controller.Start(); controller.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Running, TimeSpan.FromSeconds(30)); if (controller.Status != System.ServiceProcess.ServiceControllerStatus.Running) { throw new WsatAdminException(WsatAdminErrorCode.DTC_RESTART_ERROR, SR.GetString(SR.ErrorRestartMSDTC)); } } } } [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("9797C15D-A428-4291-87B6-0995031A678D")] interface IDtcNetworkAccessConfig { void GetAnyNetworkAccess( /* [out] */ ref bool anyNetworkAccess); void SetAnyNetworkAccess( /* [in] */ bool anyNetworkAccess); void GetNetworkAdministrationAccess( /* [out] */ ref bool networkAdministrationAccess); void SetNetworkAdministrationAccess( /* [in] */ bool networkAdministrationAccess); void GetNetworkTransactionAccess( /* [out] */ ref bool networkTransactionAccess); void SetNetworkTransactionAccess( /* [in] */ bool networkTransactionAccess); void GetNetworkClientAccess( /* [out] */ ref bool networkClientAccess); void SetNetworkClientAccess( /* [in] */ bool networkClientAccess); void GetNetworkTipAccess( /* [out] */ ref bool networkTipAccess); void SetNetworkTipAccess( /* [in] */ bool networkTipAccess); void GetXAAccess( /* [out] */ ref bool xaAccess); void SetXAAccess( /* [in] */ bool xaAccess); [PreserveSig()] int RestartDtcService(); } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- PrinterSettings.cs
- WebPartMovingEventArgs.cs
- RepeaterItemCollection.cs
- FrameworkName.cs
- Normalization.cs
- ProfileModule.cs
- StringStorage.cs
- SamlAssertionKeyIdentifierClause.cs
- AmbientEnvironment.cs
- PointCollection.cs
- SelectionGlyph.cs
- TypeConverterHelper.cs
- MemoryMappedFileSecurity.cs
- FixedLineResult.cs
- GeneralTransform3D.cs
- EventLogSession.cs
- ContentPlaceHolder.cs
- SafeCryptContextHandle.cs
- TerminateSequence.cs
- OpenFileDialog.cs
- versioninfo.cs
- DataBoundControl.cs
- FixedSchema.cs
- CodeTypeMember.cs
- InputScopeManager.cs
- StreamInfo.cs
- QueryOptionExpression.cs
- AnnotationResourceChangedEventArgs.cs
- SHA1Managed.cs
- MouseCaptureWithinProperty.cs
- NetworkAddressChange.cs
- GeometryValueSerializer.cs
- NavigationWindow.cs
- ContentElementAutomationPeer.cs
- SwitchAttribute.cs
- DNS.cs
- TemplateField.cs
- HtmlInputButton.cs
- TextServicesContext.cs
- Semaphore.cs
- TypedReference.cs
- bidPrivateBase.cs
- PropertyEntry.cs
- MemberDescriptor.cs
- QilGeneratorEnv.cs
- ConfigurationPropertyCollection.cs
- RecordManager.cs
- DocumentOrderComparer.cs
- PaintEvent.cs
- QueryPageSettingsEventArgs.cs
- Rect3D.cs
- DocumentPageHost.cs
- DetailsViewInsertedEventArgs.cs
- TranslateTransform3D.cs
- RichTextBox.cs
- ExtractedStateEntry.cs
- ProfilePropertyNameValidator.cs
- SizeFConverter.cs
- AttachedPropertyBrowsableWhenAttributePresentAttribute.cs
- CursorConverter.cs
- NamespaceList.cs
- ObjectSet.cs
- XmlSchemaChoice.cs
- DLinqAssociationProvider.cs
- HttpCachePolicyElement.cs
- RouteValueExpressionBuilder.cs
- IndexedGlyphRun.cs
- TextDecorationCollectionConverter.cs
- DataGridViewAddColumnDialog.cs
- MetafileHeaderWmf.cs
- PropertyEmitter.cs
- StylusPointPropertyId.cs
- ServiceNameElement.cs
- GenerateTemporaryAssemblyTask.cs
- CollectionEditorDialog.cs
- EncoderFallback.cs
- sapiproxy.cs
- MSG.cs
- BuilderElements.cs
- ELinqQueryState.cs
- SoapHeaders.cs
- ServiceSecurityContext.cs
- RuntimeEnvironment.cs
- GlyphTypeface.cs
- TextServicesHost.cs
- Thread.cs
- DesignerView.Commands.cs
- Dictionary.cs
- DockAndAnchorLayout.cs
- ZoneLinkButton.cs
- EntityDescriptor.cs
- XmlAggregates.cs
- SqlSupersetValidator.cs
- BroadcastEventHelper.cs
- VerificationAttribute.cs
- FileInfo.cs
- PerformanceCounterCategory.cs
- Control.cs
- FormViewDeletedEventArgs.cs
- TemplateInstanceAttribute.cs