Silverlightアプリケーションを本格的に組もうと思ったとき心配なのが、巨大なアプリを組んだときに、いつまでたっても読み込み中・・・となってしまうことだと思う。
そんな時のための機能?としてアプリケーション ライブラリ キャッシュというものがあります。
これは、今まで1つのxapファイルにdllとか全部ごちゃっと固めてダウンロードしてたものを、複数のzipファイルに分割して、二度目以降のアクセスでは、変更のないファイルはキャッシュを使うことで、高速化しようぜ!っていうテクノロジらしいです。
とりあえず、アプリケーション ライブラリ キャッシュとして切り出すものは2つあります。
- Silverlightのdll
- 自作のdll
ということで、1つずつ見ていきます。
Silverlightのdll
まず、テスト用のアプリを作ります。名前はAppLibCacheTestにしました。
ホストするWebアプリも同時にAppLibCacheTest.Webという名前で作成します。
そして、AppLibCacheTestのプロジェクトのプロパティのSilverlightタブの下のほうにある、「アプリケーション ライブラリ キャッシュを利用してXAPを縮小する」にチェックを入れます。
このチェックですが、「ブラウザ外実行を有効にする」とは共存できないらしいので要注意です!
これで、下準備は出来たので、ためしにSystem.Windows.Controls.Data.Input.dllを、アプリケーション ライブラリ キャッシュにしてみたいと思います。
普通に、参照の追加の.NETタブからDLLを追加するのではなく、Silverlight SDKにあるdllを参照に追加する必要があります。
Silverlight SDKのdllは、通常「C:\Program Files\Microsoft SDKs\Silverlight\v3.0\Libraries\Client」にあります。
ここから、dllを参照に追加します。
この状態で、ソリューションをビルドすると、WebアプリケーションのClientBinに、System.Windows.Controls.Data.Input.zipが作成されます。
これで、一度キャッシュされたSystem.Windows.Controls.Data.Input.zipは、二度目のアクセスからダウンロードが省かれるという寸法です。
仕掛け
さて、この機能が何故有効になっているのか、実際にSilverlight SDKのLibrariesのClientフォルダをのぞいてみます。そうすると、dllのほかにextmap.xmlという拡張子のファイルがあります。
実は、このファイルがあると、アプリケーション ライブラリ キャッシュが有効になります。中身は、そんなに難しくないです。
<?xml version="1.0"?>
<manifest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<assembly>
<name>System.Windows.Controls.Data.Input</name>
<version>2.0.5.0</version>
<publickeytoken>31bf3856ad364e35</publickeytoken>
<relpath>System.Windows.Controls.Data.Input.dll</relpath>
<extension downloadUri="System.Windows.Controls.Data.Input.zip" />
</assembly>
</manifest>
nameにアセンブリの短い名前に、バージョン・publickeytokenなどの情報が書いてあるだけです。
ということで、これを踏まえて、次に自作のdllをアプリケーション ライブラリ キャッシュのターゲットにしてみようと思います。
自作のdllをアプリケーション ライブラリ キャッシュになるように構成してみる
引き続き、プロジェクトにSilverlightクラスライブラリを追加します。
プロジェクト名はAppLibCache.Libにしました。
作成したら、AppLibCacheTestプロジェクトに参照を追加して、ビルドします。
この状態でビルドしても、ClientBinの様子はかわりません。AppLibCache.Lib.dllは、AppLibCacheTest.xap内に組み込まれています。
ということで、AppLibCache.Libプロジェクトに、AppLibCache.Lib.extmap.xmlという名前のファイルを作成します。
<?xml version="1.0"?>
<manifest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<assembly>
<name>AppLibCache.Lib</name>
<version>*</version>
<publickeytoken>*</publickeytoken>
<relpath>AppLibCache.Lib.dll</relpath>
<extension downloadUri="AppLibCache.Lib.zip" />
</assembly>
</manifest>
中身は、さっきのファイルを真似て書いてます。versionとpublickeytokenは、厳密に指定するのがめんどくさかったので*で指定してみたら大丈夫でした。
このファイルのプロパティで、ビルド時に出力フォルダにコピーするように設定します。
これだけで、アプリケーション ライブラリ キャッシュが有効になります。
ためしにビルドをしてみ、ClientBinフォルダを覗くと、AppLibCache.Lib.zipが出来ています。
うまいことキャッシュされれば、初回起動の時さえ我慢すれば、二度目からは、そこそこの早さになることが見込めます。
割と、いい機能だと思いました。
ただ・・・これを使ってアプリケーション ライブラリ キャッシュを有効にしたアセンブリにあるPageをNavigation Frameworkで表示させようとするとうまくいきません。
解決策をご存知の方はご一報を!!
/AppLibraries.Lib;component/Hogehoge.xamlのように指定しても、みつからないといわれてしまいます…