Introduction
The Distributed Transaction Coordinator (MSDTC) service is a component of modern versions of Microsoft Windows that is responsible for coordinating transactions that span multiple resource managers, such as databases, message queues, and file systems. MSDTC is included in Windows 2000 and later operating systems, and is also available for Windows NT 4.0.
Distributed transactions allows client applications to include several different sources of data on two or more networked systems in one transaction. As an example, you can use DTC when your application or service is calling more than one stored procedure or entity framework statement from different contexts.
Using transactions is very important in any data-driven application, it is not related to database operations it could also get involved into writing to a local system file or registry along with database manipulation.
For more details about TransactionScope
usage and advantages, please read this article: All About TransactionScope.
Background
During my work on an enterprise ERP system which its architecture use Entity Framework in the Data Access Layer, the system is using TransactionScope
in System.Transactions
to maintain and manage database operations.
The only issue is that MSDTC is a Windows service providing transaction infrastructure for distributed systems, this service needs to be up and running on both database and application machines, you should be careful about firewall settings, also you should configure options for the MSDTC correctly.
This will effect application deployment.
I provide this library as a tool to manage MSDTC from your program to avoid any failure related to MSDTC configurations on user PC, also you can use it in your system setup custom actions to ease the deployment of your solution and make it automated and more effective instead of manual handling for each device.
Using the Code
MsdtcManager
is the class which you need to start using this library, first you should instantiate it in your code, so let us start with its constructors.
You can instantiate MsdtcManager
in three ways as below:
MsdtcManager(bool autoRestartService, int timeoutMilliseconds)
Two parameters contractor:
- autoRestartService: If
true
, MsdtcManager
will restart the service with every configuration change require service restart - timeoutMilliseconds: The period
MsdtcManager
will wait the MSDTC service to reach the specified status or for the specified time-out to expire, in milliseconds
MsdtcManager(bool autoRestartService)
timeoutMilliseconds
default value is 250 milliseconds.
MsdtcManager();
Parameterless default constructor, timeoutMilliseconds
default value is 250 milliseconds, autoRestartService
default value is false
.
General Properties
MsdtcManager
has two properties related to MSDTC service:
Property | Data Type | Available Options |
---|
NeedRestart | bool | Detect if MSDTC service needs to restart after any changes in configuration require service restart. If autoRestartService value is true , will take care about restarting the service automatically, except that you should restart service by calling MsdtcManager.RestartService(); |
MSDTC Service
MsdtcManager
has two properties related to MSDTC service:
Property | Data Type | Description |
---|
IsServiceInstalled | bool | Return true if MSDTC service installed (as mentioned before MSDCT is included since Windows 2000) |
IsServiceInstalledAndRunning | bool | Return true if MSDTC service is installed and running |
MsdtcManager
includes the following method to manage MSDTC service:
tart MSDTC service.
Method | Return Valueue | Description |
---|
GetServiceInfo() | ServiceInfoiceInfo | Return struct includes some information about MSDTC service. |
GetServiceStatus() | ServiceControllerStatus | Return the current state of the service, for more detailed ServiceControllerStatus. |
StartService() | void | Start MSDTC service. |
StopService() | void | Stop MSDTC service. |
RestartService() | void | Restart MSDTC service. |
InstallService() | void | Install MSDTC Service |
UninstallService() | void | Uninstall MSDTC service. |
RegMtxoci() | void | Mtxoci.dll is a dynamic-link library (DLL) that is used internally by the Microsoft ODBC Driver for Oracle and the Microsoft OLEDB Provider for Oracle in conjunction with Microsoft Distributed Transaction Coordinator (DTC) to provide transactional support to Oracle databases.
Specifically, it translates the DTC transactions into the XA transactions that Oracle can understand.
This component currently has no way of tracing the DTC and application messages received by it nor XA messages sent by it.
This can make troubleshooting some problems extremely difficult. |
Firewall
If you are using Windows Firewall to protect the computers in your organization, you must add MSDTC into the exception list in the Windows Firewall settings. To do so, MsdtcManager
provides the following methods:
Property | Data Type | Description |
---|
IsMsdtcRuleGroupEnabled(NET_FW_PROFILE_TYPE2_ profileType) | bool | Return true if MSDTC rule group is enabled in Windows firewall, except that return false . |
EnableWindowsFirewallException(NET_FW_PROFILE_TYPE2_ profileType) | void | Enable MSDTC rule group in Windows firewall |
DisableWindowsFirewallException(NET_FW_PROFILE_TYPE2_ profileType) | void | Disable MSDTC rule group in Windows firewall |
MSDTC Configurations
MsdtcManager
provides the following properties and methods to handle MSDTC settings:
Property / Method | Description |
---|
NetworkDtcAccess | Get or Set value determines whether MSDTC on the local computer can access the network. This setting must be enabled in combination with one of the other settings to enable network MSDTC transactions. |
AllowInbound | Get or Set the value which allows a distributed transaction that originates from a remote computer to run on this computer. |
AllowOutbound | Get or Set the value which allows the local computer to initiate a transaction and run it on a remote computer. |
AuthenticationRequired | Get or set the required authentication type, available values: MutualAuthenticationRequired , IncomingCallerAuthenticationRequired , NoAuthenticationRequired . |
EnableXaTransactions | MSDTC can act as either an XA-compliant resource manager or as a transaction manager. When the DTC acts as an XA-compliant resource manager, it allows SQL Server, Message Queuing (MSMQ), and other OLE transactions-compliant resource managers to participate in transactions that are controlled by X/Open DTP XA-compliant transaction processing monitors. |
EnableSnaLuTransactions | Supporting IBM CICS LU 6.2 Transactions. |
ResetToDefaultSettings | Set MSDTC to its default value when Windows installed. |
If you want to modify more than one setting (usually this is most common scenario), you should initiate MsdtcManager
with default no parameters constructor or set autoRestartService
to false
, one which take parameters (if autoRestartService
is true
, MsdtcManager
will restart the service after each change requires that which is not recommend) after change the settings you can check if it is required to restart the service and restart it as below:
if (msdtcManager.NeedRestart)
{
msdtcManager.RestartService();
}
Demo Sample
The project includes a demo application to cover all the functionality of this library.
I have added below a screenshot for the real DTC Properties window so you can know that is MSDTC Manager almost handle most of DTC properties.
Points of Interest
This library is based on this MSDN page: Distributed Transaction Coordinator
Please read it carefully to understand what you need to configure it correctly, also you can read this article to know how to set the configurations manually: MSDTC Service enable issues when using .NET TransactionScope.
More references:
- Supporting XA Transactions
- Supporting IBM CICS LU 6.2 Transactions
History
-
2014-02-19 Initial release (Beta Version)
- 2014-02-27 Version 1 {Added: InstallService, UninstallService, ChangeLogonAccount, RegMtxoci, EnableXaTransactions, EnableSnaLuTransactions}