Click here to Skip to main content
15,885,953 members
Articles / Programming Languages / C#

MSDTC Manager

Rate me:
Please Sign up or sign in to vote.
4.80/5 (15 votes)
20 Feb 2014CPOL5 min read 50.1K   1.2K   20   13
MSDTC manager

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 <code>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:

C#
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
C#
MsdtcManager(bool autoRestartService) 

timeoutMilliseconds default value is 250 milliseconds.

C#
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:

C#
if (msdtcManager.NeedRestart)
 {
     msdtcManager.RestartService();
 }

Demo Sample

The project includes a demo application to cover all the functionality of this library.

Image 1

Image 2

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.

Image 3

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} 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Germany Germany
010011000110100101101011011001010010000001000011011011110110010001101001011011100110011100100001

Comments and Discussions

 
QuestionHelp - Client and admistrator Pin
Member 113577694-Apr-18 0:42
Member 113577694-Apr-18 0:42 
QuestionI use dtc a lot Pin
Sacha Barber12-Dec-14 20:34
Sacha Barber12-Dec-14 20:34 
AnswerRe: I use dtc a lot Pin
Tammam Koujan13-Dec-14 20:58
professionalTammam Koujan13-Dec-14 20:58 
GeneralRe: I use dtc a lot Pin
kiquenet.com7-Jan-18 11:28
professionalkiquenet.com7-Jan-18 11:28 
BugAuthenticationRequiredType - Set and Get Methods are treating diferent behaviors Pin
Raphael.UFRPE20-May-14 9:35
Raphael.UFRPE20-May-14 9:35 
GeneralRe: AuthenticationRequiredType - Set and Get Methods are treating diferent behaviors Pin
Tammam Koujan20-May-14 10:46
professionalTammam Koujan20-May-14 10:46 
GeneralRe: AuthenticationRequiredType - Set and Get Methods are treating diferent behaviors Pin
Raphael.UFRPE5-Jun-14 3:10
Raphael.UFRPE5-Jun-14 3:10 
GeneralRe: AuthenticationRequiredType - Set and Get Methods are treating diferent behaviors Pin
Tammam Koujan8-Jun-14 0:43
professionalTammam Koujan8-Jun-14 0:43 
QuestionGreat! Pin
Gustavo Sabas28-Apr-14 2:02
Gustavo Sabas28-Apr-14 2:02 
AnswerRe: Great! Pin
Tammam Koujan21-May-14 2:20
professionalTammam Koujan21-May-14 2:20 
GeneralThis is just what I needed... Thanks!!! Pin
xondokan24-Feb-14 4:25
xondokan24-Feb-14 4:25 
GeneralRe: This is just what I needed... Thanks!!! Pin
Tammam Koujan24-Feb-14 4:26
professionalTammam Koujan24-Feb-14 4:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.