はじめに
前回で Windows Azure Storage Service(以下 WASS) を使用する WCF サービスを作って、WebRole にホストさせました。
今回は仕上げとして、Silverlight アプリケーションから WCF サービスを呼び出すようにします。
Silverlight のプロジェクトにサービス参照を追加
ソリューション内を検索して、WebRole プロジェクトにある WCF サービスを指定します。
WCF サービスを呼び出すコードを記述
public partial class Page : UserControl { public Page() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { ServiceReference1.SlipReportServiceClient client = new SlipReport_Page.ServiceReference1.SlipReportServiceClient(); client.FindCompleted += new EventHandler<SlipReport_Page.ServiceReference1.FindCompletedEventArgs>(client_FindCompleted); client.FindAsync(_beginDate.SelectedDate.Value, _endDate.SelectedDate.Value); } private void client_FindCompleted(object sender, SlipReport_Page.ServiceReference1.FindCompletedEventArgs e) { if (e.Cancelled) { MessageBox.Show("集計がキャンセルされました。"); } else if (e.Error != null) { MessageBox.Show(e.Error.Message); } else { _dataGrid.DataContext = e.Result; } ((ServiceReference1.SlipReportServiceClient)sender).CloseAsync(); } }
サービス参照の追加で生成された Proxy を呼び出します。XAML は変更していません。
テストデータを用意
StorageAccountInfo account = StorageAccountInfo.GetDefaultTableStorageAccountFromConfiguration(); SlipDataServiceContext context = new SlipDataServiceContext(account); context.RetryPolicy = RetryPolicies.RetryN(3, TimeSpan.FromSeconds(1)); // Table にデータを入れる string name = SlipDataServiceContext.SlipTableName; context.AddObject(name, new SlipEntity(name, "1") { Date = new DateTime(2009, 2, 1), No = 1, ProductName = "Word", Amount = 20000 }); context.AddObject(name, new SlipEntity(name, "2") { Date = new DateTime(2009, 2, 8), No = 1, ProductName = "Excel", Amount = 15000 }); context.SaveChangesWithRetries();
Table にあらかじめデータを入れておきます。このコードはコンソールアプリケーションで実行。
まとめ
- Silverlight から直接 StorageClient ライブラリを呼び出せない
- Silverlight アプリケーションは WebRole にホストさせることが可能
- WCF サービスからは StorageClient を利用できる
- WCF サービスを WebRole にホストさせることが可能
- Silverlight からは WCF サービスを呼び出せる
- WCF を使って間接的に Silverlight から WASS を使用できそう
でも前に書きましたが、これは良い方法じゃ無いと思います。また、キーを持っていないため Azure Platform 上で動作確認していませんorz
Silverlight と Windows Azure に関して情報をお持ちの方はぜひ教えて下さい><