インストーラ クラス (インストール コンポーネントとも呼ばれる) は、インストール時にカスタム動作として呼び出される .NET Framework クラスです。この例では、クラス ライブラリ プロジェクトをソリューションに追加します。このクラス ライブラリ プロジェクトでは、インストーラ クラスを作成し、その Install メソッドをオーバーライドして、Web アプリケーションの .config ファイルを修正するためのコードを追加します。インストーラ クラスの詳細については、「インストール コンポーネントの概要」を参照してください。
クラス ライブラリ プロジェクトを作成するには
ソリューション エクスプローラでソリューション ノードを右クリックし、[追加] をポイントして [新しいプロジェクト] をクリックします。
[新しいプロジェクトの追加] ダイアログ ボックスの [Visual Basic] ノードで [クラス ライブラリ] をクリックします。
プロジェクトに「InstallerClassLibrary」という名前を付けます。
インストーラ クラスを追加して実装するには
ソリューション エクスプローラで [InstallerClassLibrary] プロジェクト ノードを右クリックし、[追加] をポイントして [クラス] をクリックします。
[新しい項目の追加] ダイアログ ボックスで、[インストーラ クラス] をクリックし、[ファイル名] ボックスに「WebServiceInstaller.vb」と入力します。
[追加] をクリックすると、このクラスがプロジェクトに追加されて、インストーラ クラスのデザイナが表示されます。
デザイナをダブルクリックしてコード エディタを表示します。
WebServiceInstaller.vb 内のインストーラ クラス モジュールの下部 (End Class 宣言のすぐ上) に次のコードを追加します。このコードでは Install メソッドを実装しています。
Public Overrides Sub Install(ByVal stateSaver As _
System.Collections.IDictionary)
' Gets the parameter passed across in the CustomActionData.
Dim installlog As New System.IO.StreamWriter("Installation.log")
installlog.AutoFlush = True
Try
Dim ProvidedName As String = Me.Context.Parameters.Item("ServerName")
Dim SvcName As String = Me.Context.Parameters.Item("ServiceName")
installlog.WriteLine("Starting Edit of the config file")
If ProvidedName = "" Or SvcName = "" Then
Throw New InstallException("No arguments specified")
End If
' Uses reflection to find the location of the config file.
Dim Asm As System.Reflection.Assembly = _
System.Reflection.Assembly.GetExecutingAssembly
Dim strConfigLoc As String
strConfigLoc = Asm.Location
Dim strTemp As String
strTemp = strConfigLoc
strTemp = strTemp.Remove(strTemp.LastIndexOf("\"), Len(strTemp) - _
strTemp.LastIndexOf("\"))
strTemp = strTemp.Remove(strTemp.LastIndexOf("\"), Len(strTemp) - _
strTemp.LastIndexOf("\"))
Dim FileInfo As System.IO.FileInfo = New System.IO.FileInfo( _
strTemp & "\web.config")
installlog.WriteLine("File info: " & strTemp)
If Not FileInfo.Exists Then
Throw New InstallException("Missing config file")
End If
' Loads the config file into the XML DOM.
Dim XmlDocument As New System.Xml.XmlDocument()
XmlDocument.Load(FileInfo.FullName)
' Finds the right node and change it to the new value.
Dim Node As System.Xml.XmlNode
Dim FoundIt As Boolean = False
For Each Node In _
XmlDocument.Item("configuration").Item("appSettings")
' Skips any comments.
If Node.Name = "add" Then
If Node.Attributes.GetNamedItem("key").Value = _
"servername.service" Then
' Note that "Service1.asmx" should be replaced with the
' actual name of the XML Web service file.
Node.Attributes.GetNamedItem("value").Value = "http://" _
& ProvidedName & "/" & SvcName & "/Service1.asmx"
FoundIt = True
End If
End If
Next Node
If Not FoundIt Then
Throw New InstallException("Config file did not contain a ServerName section")
End If
' Writes out the new config file.
XmlDocument.Save(FileInfo.FullName)
Finally
installlog.WriteLine("Ending edit of config file")
installlog.Close()
End Try
End Sub
上のコードは、まず、カスタム動作の進行状況を記録するインストール ログ ファイルを作成します。次に、System.Reflection 名前空間を使用して、インストールするアセンブリと、関連付けられている .config ファイルを見つけます。次に、XML ドキュメント モデルを使用して、appSettings セクションが見つかるまで .config ファイルを反復処理します。キー servername.service が見つかったら、渡されたパラメータを使って関連付けられている値を変更します。これにより、新しい Web サービスを使用するようにアプリケーションがリダイレクトされます。
ソリューション エクスプローラで、Web.config ファイルをダブルクリックして開きます。
appSettings セクションにある Web サービスのキーの値をコピーします。キーには servername.service という形式を使用します。servername は Web サービスが置かれたサーバー、service は Web サービスの名前です。
コード エディタでインストーラ クラス モジュールを開き、"servername.service" というテキストを先ほどコピーした値で置き換えます。