IIS で Web サイト バインディングを一覧表示するコンソール アプリケーションが 1 つあります。
using (var directoryEntry = new DirectoryEntry("IIS://localhost/w3svc/" + GetWebSiteId())) {
var bindings = directoryEntry.Properties["ServerBindings"];
}
プロセス経由で ASP.NET からこのコンソール アプリケーションを呼び出します
var process = new Process {
StartInfo = new ProcessStartInfo {
FileName = "c:/app.exe",
Arguments = "check",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
Widows 7 / IIS 7.5 の開発マシンではすべて正常に動作しますが、Windows 2012 / IIS 8 でテストすると、「アクセスが拒否されました」というエラーが表示されます。
エラーログ
"System.Runtime.InteropServices.COMException (0x80070005): Access is denied.
at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
at System.DirectoryServices.DirectoryEntry.Bind()
at System.DirectoryServices.DirectoryEntry.get_IsContainer()
at System.DirectoryServices.DirectoryEntries.ChildEnumerator..ctor(DirectoryEntry container)
at System.DirectoryServices.DirectoryEntries.GetEnumerator()
at IISSubdomainManagement.Program.GetWebSiteId()
at IISSubdomainManagement.Program.TotalBindings()
at IISSubdomainManagement.Program.Main(String[] args)"
ps アプリケーション プール ID は「ApplicationPoolIdentity」です
言い忘れましたが、CMD から実行すると、サーバー上でコンソール アプリが正常に動作します。