MSDTC インストーラクラス サンプル

昔書いたコード

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Diagnostics;
using Microsoft.Win32;

[RunInstaller(true)]
public partial class MsdtcSettingInstaller : Installer
{
    #region public MsdtcSettingInstaller()

    public MsdtcSettingInstaller()
    {
        InitializeComponent();
    }

    #endregion

    #region public override void Install(System.Collections.IDictionary stateSaver)

    public override void Install(System.Collections.IDictionary stateSaver)
    {
        this.StopService();
        this.SetAutoStartOption();
        this.SetNetworkAccessOption();
        this.StartService();
        base.Install(stateSaver);
    }

    #endregion

    #region private void SetAutoStartOption()

    private void SetAutoStartOption()
    {
        ProcessStartInfo info = new ProcessStartInfo();
        info.FileName = "sc.exe";
        info.Arguments = "config msdtc start= auto";
        info.CreateNoWindow = true;
        info.UseShellExecute = false;

        try
        {
            this.ExecuteProcess(info);
        }
        catch (TimeoutException ex)
        {
            throw new TimeoutException("MSDTC サービスの開始オプションの変更操作がタイムアウトしました。", ex);
        }
    }

    #endregion

    #region private void StartService()

    private void StartService()
    {
        ProcessStartInfo info = new ProcessStartInfo();
        info.FileName = "sc.exe";
        info.Arguments = "start msdtc";
        info.CreateNoWindow = true;
        info.UseShellExecute = false;

        try
        {
            this.ExecuteProcess(info);
        }
        catch (TimeoutException ex)
        {
            throw new TimeoutException("MSDTC サービスの開始操作がタイムアウトしました。", ex);
        }
    }

    #endregion

    #region private void StopService()

    private void StopService()
    {
        ProcessStartInfo info = new ProcessStartInfo();
        info.FileName = "sc.exe";
        info.Arguments = "stop msdtc";
        info.CreateNoWindow = true;
        info.UseShellExecute = false;

        try
        {
            this.ExecuteProcess(info);
        }
        catch (TimeoutException ex)
        {
            throw new TimeoutException("MSDTC サービスの停止操作がタイムアウトしました。", ex);
        }
    }

    #endregion

    #region private void ExecuteProcess(ProcessStartInfo startInfo)

    private void ExecuteProcess(ProcessStartInfo startInfo)
    {
        using (Process process = new Process())
        {
            process.StartInfo = startInfo;
            process.Start();
            const int waitMilliSec = 60000;
            bool complete = process.WaitForExit(waitMilliSec);
            if (!complete)
            {
                throw new TimeoutException("操作がタイムアウトしました。");
            }
        }
    }

    #endregion

    #region private void SetNetworkAccessOption()

    private void SetNetworkAccessOption()
    {
        using (RegistryKey msdtcKey = Registry.LocalMachine.CreateSubKey(@"Software\Microsoft\MSDTC"))
        {
            using (RegistryKey securityKey = msdtcKey.CreateSubKey("Security"))
            {
                securityKey.SetValue("NetworkDtcAccess", 1, RegistryValueKind.DWord);
                securityKey.SetValue("NetworkDtcAccessTransactions", 1, RegistryValueKind.DWord);
                securityKey.SetValue("NetworkDtcAccessInbound", 1, RegistryValueKind.DWord);
                securityKey.SetValue("NetworkDtcAccessOutbound", 1, RegistryValueKind.DWord);
                securityKey.Close();
            }

            msdtcKey.SetValue("AllowOnlySecureRpcCalls", 0, RegistryValueKind.DWord);
            msdtcKey.SetValue("FallbackToUnsecureRPCIfNecessary", 0, RegistryValueKind.DWord);
            msdtcKey.SetValue("TurnOffRpcSecurity", 1, RegistryValueKind.DWord);
            msdtcKey.Close();
        }
    }

    #endregion
}

レジストリの設定はここを参照
分散トランザクション コーディネータ