🔰

ClickOnceアプリケーションの簡単なデバッグ方法

に公開

はじめに

ClickOnceというWindowsアプリケーションをWebページを通じて配布、更新する技術があります。ClickOnceで配布するアプリケーションをWebページから起動した場合のみ行われる処理をデバッグしたいことがありましたので、その時に使った方法を書き残します。

次のようなコードがあったとします。

Program.cs
using System;
using System.Deployment.Application;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    internal static class Program
    {
        /// <summary>
        /// アプリケーションのメイン エントリ ポイントです。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            if (ApplicationDeployment.IsNetworkDeployed)
            {
                // URLパラメーターの取得等の処理
            }

            Application.Run(new Form1());
        }
    }
}

Webページから起動した場合、ApplicationDeployment.IsNetworkDeployed == trueになり、// URLパラメーターの取得等の処理が行われます。この// URLパラメーターの取得等の処理をデバッグしたいです。
しかし普通にVisual Studioからデバッグを開始してもURLパラメーターを取得することができません。なのでClickOnceで発行してWebページに配置した状態でデバッグをする必要があります。

方法

デバッグしたい箇所の少し前にSystem.Diagnostics.Debugger.Launch()を書き足します。
Debugビルドだけで動作するよう、ディレクティブ#if DEBUGで囲んでいます。

Program.cs
using System;
using System.Deployment.Application;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    internal static class Program
    {
        /// <summary>
        /// アプリケーションのメイン エントリ ポイントです。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            if (ApplicationDeployment.IsNetworkDeployed)
            {
+ #if DEBUG
+               System.Diagnostics.Debugger.Launch();
+ #endif
                // URLパラメーターの取得等の処理
            }

            Application.Run(new Form1());
        }
    }
}

Debugビルドで発行した後、Webページから起動すると、デバッガを選択する画面が表示されるので、適当に選びます。

後はデバッガが起動するので、System.Diagnostics.Debugger.Launch()の行からステップ実行できます。

ラグザイア

Discussion