Silverlightで利用できるRIA Services、一度触ってみると便利さに驚くことだと思う。先日、RIA Services v1.0 SP1 BetaとToolkit October 2010がリリースされた。これらをインストールすることで、RIA ServicesでWindows Azureストレージ・サービスのテーブルストレージを利用することが可能だ。
そこで今回は、RIA Servicesでテーブル・ストレージを利用するまでの手順を紹介したい。作成するサンプルの起動イメージは以下となるので、参考にして頂きたい。
※RIA Servicesをまだ利用していない方は、以下のサイトで学習することをオススメする。
前提環境
今回の前提環境は以下となっている。
- Visual Stuido 2010(Expressでは例外が発生してうまく動作しなかった。詳細は「注3」参照)
- Windows Azure SDK 1.2.1
- Silverlight 4 SDK 日本語
- WCF RIA Services V1.0 SP1 Beta
- WCF RIA Services Toolkit October 2010
※注1 各コンポーネントは、インストールする前に当該コンポーネントの旧バージョンを削除する必要があることに留意して頂きたい
※注2 SilverlightのSDK、サービスパック、Toolkit http://silverlight.net/riaservices サイトからもダウンロード可能だ。本記事のリンクが切れている場合は、別途こちらを参照して頂きたい
※注3 Express版のVisual Studio 2010で今回のサンプルを作成したところ、以下のエラーが表示された。原因は不明だが、Expressで開発する方は注意して頂きたい。
RIA Services利用までの流れ
プロジェクトを作成するまでの流れを解説する。
- RIA Service用のソリューション作成と設定
- ソリューションの作成とプロジェクトの作成
- 接続文字列の設定
- Webロールプロジェクト側
- アセンブリ参照の追加
- TableEntityの継承クラスの作成
- TableEntityContextの継承クラスの作成
- ドメインサービスクラスの作成
- Silverlightプロジェクト側
- 親画面へのデータソースの追加
- 子ウィンドウの作成とデータ追加画面の作成
1. RIA Service用のソリューション作成と設定
- 1.1 ソリューションの作成とプロジェクトの作成
Visual Studio を立ち上げ、新規に「Windows Azure クラウドサービス」を作成し、Web Roleをひとつ追加する。
次に、同ソリューションにSilverlightプロジェクトを追加で新規作成し、RIA Servicesを有効化する。
- 1.2 接続文字列の設定
RIA Servicesが利用するストレージへの接続文字列を設定する。Cloud Serviceプロジェクトの「ロール」フォルダから作成したWebロールをダブルクリックし、[設定]タブを選択して「接続文字列」を以下の例に従って設定して頂きたい。
プロパティ名 | 値 |
---|---|
名前 | DataConnectionString |
種類 | 接続文字列 |
値 | UseDevelopmentStorage=true |
2. Webロールプロジェクト側
- 2.1 アセンブリ参照の追加
ストレージ・サービスでRIA Servicesを利用する場合、Webロール・プロジェクトにアセンブリ参照を追加する必要がある。[参照の追加 > .NETタブ]から次のアセンブリを追加して頂きたい(下図参照)。
- Microsoft.ServiceModel.DomainServices.WindowsAzure
- System.Data.Services.Client
- 2.2 TableEntityの継承クラスの作成
次に、テーブルストレージにアクセスするためのエンティティクラスを作成する。以下のサンプルに従って、Entityクラスを作成して頂きたい。
namespace WebRole1.Model { using Microsoft.ServiceModel.DomainServices.WindowsAzure; //継承するクラスがRIA Services用になっている public class CommentEntity : TableEntity { public string Name { get; set; } public string Body { get; set; } } }
- 2.3 TableEntityContextの継承クラスの作成
次に、エンティティクラスを制御するデータコンテキストクラスを作成する。以下のサンプルに従って、データコンテキストクラスを作成して頂きたい。
namespace WebRole1.Model { using Microsoft.ServiceModel.DomainServices.WindowsAzure; using Microsoft.WindowsAzure.ServiceRuntime; //継承するクラスがRIA Services用になっている public class CommentEntityContext : TableEntityContext { public CommentEntityContext() : base(RoleEnvironment.GetConfigurationSettingValue("DataConnectionString")) { } public TableEntitySet<CommentEntity> CommentEntities { get { return base.GetEntitySet<CommentEntity>(); } } } }
- 2.4 ドメインサービスクラスの作成
RIA Services側にサービスを公開するため、ドメインサービスクラスを作成する必要がある。まず、[追加(D) > 新しい項目の追加(W)]から「ドメインサービスクラス」を選択し、空のドメインサービスクラス(この例では CommentDomainService.cs)を作成して頂きたい。
次に、以下のサンプルに従ってドメインサービスクラスを修正して欲しい。コードを参照して頂ければ一目瞭然だが、RIA Services側にCRUD処理が実現できるメソッドを提供している。
namespace WebRole1 { using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Linq; using System.ServiceModel.DomainServices.Hosting; using System.ServiceModel.DomainServices.Server; using Microsoft.ServiceModel.DomainServices.WindowsAzure; using WebRole1.Model; // RIA Services向けにCRUD処理が出来る形でメソッドを実装している [EnableClientAccess()] public class CommentDomainService : TableDomainService<CommentEntityContext> { public IQueryable<CommentEntity> GetCommentEntities() { return this.EntityContext.CommentEntities; } public void AddCommentEntity(CommentEntity entity) { this.EntityContext.CommentEntities.Add(entity); } public void DeleteCommentEntity(CommentEntity entity) { this.EntityContext.CommentEntities.Delete(entity); } public void UpdateCommentEntity(CommentEntity entity) { this.EntityContext.CommentEntities.Update(entity); } } }
3. Silverlightプロジェクト側
次にRIA Servicesを利用するSilverlight側のプロジェクトを作成する。
-
- 3.1 親画面へのデータソースの追加
MainPage.xamlを開き、「データソース」ウィンドウからエンティティを選択し、ウィンドウに対してドラッグ&ドロップする。エンティティ・クラスのプロパティに従ったDataGridが自動生成されるので、こちらを利用する。
プロジェクトの実行
[スタートアッププロジェクト]がCloud Service側になっていることを確認して、Visual Studio 上から[F5]を押してサンプルを実行する。以下の画面が表示されれば動作は成功している。
エンティティの追加を行っていないためDataGridの中身が空になっているが、別の回にエンティティの追加処理の実装方法を紹介したいと思う。
また、Azure Storage Explorer でテーブル・ストレージを参照したところ、「CommentEntity」の空テーブルが作成されていたことも認識頂きたい。