Silverlight は、Web サービスとの通信サポートを実装しています。このクイックスタートでは、Silverlight で Web サービスを利用する方法を紹介します。
このクイックスタートは、次のセクションで構成されています。
Silverlight の Web サービスの詳細については、MSDN の Silverlight ドキュメントの「
Silverlight の Web サービスへのアクセス」を参照してください。
Silverlight クライアント向けのサービスの構築
Web サービスを理解できるように、簡単な例を示します。このクイックスタートの例では、Web サービス CustomerService を使用します。このサービスは、業務で使用する顧客情報を提供します。顧客情報には、顧客数、顧客名、顧客の年齢、および顧客が会員であるかどうかといった項目があります。
次のコードは、CustomerService.scv.cs (CustomerService.scv.vb) の CustomerService コントラクトです。CustomerService コントラクトには、Silverlight クライアントからアクセス可能な 2 つの操作があります。CountUsers 操作は、顧客数を返します。GetUser 操作は、顧客の名前、年齢、および顧客が会員であるかどうかを返します。GetUser は、User 型を返します。これはコントラクトの一部として定義および実装されています。
C#
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Text;
namespace SilverlightApplication1Web
{
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements (RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class CustomerService
{
[OperationContract]
public int CountUsers() { return 2; }
[OperationContract]
public User GetUser(int id) { if (id == 1) { return new User() { IsMember = true, Name = "Paul", Age = 24 }; } else { return new User() { IsMember = false, Name = "John", Age = 64 }; } } }
[DataContract]
public class User { [DataMember] public bool IsMember { get; set; }
[DataMember] public string Name { get; set; }
[DataMember] public int Age { get; set; }
}
}
Visual Basic
Imports System Imports System.Linq Imports System.Runtime.Serialization Imports System.ServiceModel Imports System.ServiceModel.Activation Imports System.Collections.Generic Imports System.Text
Namespace SilverlightApplication1Web <ServiceContract([Namespace] := "")> _ <AspNetCompatibilityRequirements _ (RequirementsMode := AspNetCompatibilityRequirementsMode.Allowed)> _ Public Class CustomerService <OperationContract> _ Public Function CountUsers() As Integer Return 2 End Function <OperationContract> _ Public Function GetUser(id As Integer) As User If id = 1 Then Return New User() Else Return New User() End If End Function End Class
<DataContract> _ Public Class User <DataMember> _ Public Property IsMember() As Boolean Get End Get Set End Set End Property
<DataMember> _ Public Property Name() As String Get End Get Set End Set End Property
<DataMember> _ Public Property Age() As Integer Get End Get Set End Set End Property End Class End Namespace
Silverlight クライアントからサービスへのアクセス
Silverlight では、プロキシを介して Web サービスと通信します。Visual Studio 2008 のサービス参照の追加などのツールを使用すると、プロキシを生成できます。プロキシは、サービスが提供するメタデータを使用して生成します。メタデータは、サービスの機能とこの機能へのアクセス方法を記述します。次の図に、コンポーネント間の通信を示します。右向きの矢印は、クライアントからのメソッドの呼び出しを表します。左向きの矢印は、サービスからの戻り値を表します。
Silverlight の Web サービスの呼び出しはすべて非同期です。このため、プロキシにはサービスの各操作について非同期メソッドと完了イベントという 2 つのメンバーが格納されています。例として、CustomerService の CountUsers サービスの操作を取り上げます。EventHandler を CountUsersCompleted イベントに追加します。このイベントは、サービスが要求されたデータを返すときに呼び出されます。イベントをセットアップしたら、CountUsersAsync を呼び出してサービスを呼び出すことができます。イベント ハンドラーは、サービスによりデータの一部が返されたら proxy_CountUsersCompleted メソッドを呼び出すように指定します。同様の説明が、GetUsers 操作についても当てはまります。
次のコードは、Silverlight クライアント用の Page.xaml.cs (Page.xaml.vb) ファイルです。
C#
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using SilverlightApplication1.ServiceReference1;
namespace SilverlightApplication1 { public partial class Page : UserControl { public Page() { // Required to initialize variables InitializeComponent(); } void OnClick(object sender, EventArgs args) { CustomerServiceClient proxy = new CustomerServiceClient();
proxy.CountUsersCompleted += new EventHandler<CountUsersCompletedEventArgs>(proxy_CountUsersCompleted); proxy.CountUsersAsync();
proxy.GetUserCompleted += new EventHandler<GetUserCompletedEventArgs%gt;(proxy_GetUserCompleted); proxy.GetUserAsync(1); }
void proxy_GetUserCompleted(object sender, GetUserCompletedEventArgs e) { getUserResult.Text = "Property values of user with ID = 1 are: Name:" + e.Result.Name + ", Age:" + e.Result.Age + ", IsMember:" + e.Result.IsMember; }
void proxy_CountUsersCompleted(object sender, CountUsersCompletedEventArgs e) { userCountResult.Text = "The number of users is: " + e.Result; } } }
Visual Basic
Imports System Imports System.Collections.Generic Imports System.Linq Imports System.Net Imports System.Windows Imports System.Windows.Controls Imports System.Windows.Documents Imports System.Windows.Input Imports System.Windows.Media Imports System.Windows.Media.Animation Imports System.Windows.Shapes Imports SilverlightApplication1.ServiceReference1
Namespace SilverlightApplication1 Public Partial Class Page Inherits UserControl
Public Sub New() ' Required to initialize variables InitializeComponent() End Sub
Private Sub OnClick(sender As Object, args As EventArgs) Dim proxy As New CustomerServiceClient()
proxy.CountUsersCompleted += New _ EventHandler(Of CountUsersCompletedEventArgs)(proxy_CountUsersCompleted) proxy.CountUsersAsync()
proxy.GetUserCompleted += New _ EventHandler(Of GetUserCompletedEventArgs)(proxy_GetUserCompleted) proxy.GetUserAsync(1) End Sub
Private Sub proxy_GetUserCompleted(sender As Object, _ e As GetUserCompletedEventArgs) getUserResult.Text = "Property values of user with ID = 1 are: Name:" + _ e.Result.Name + ", Age:" + e.Result.Age + ", IsMember:" + _ e.Result.IsMember End Sub
Private Sub proxy_CountUsersCompleted(sender As Object, _ e As CountUsersCompletedEventArgs) userCountResult.Text = "The number of users is: " + e.Result End Sub End Class End Namespace
次の Page.xaml の XAML は、CustomerService サービスへのアクセスを開始し、結果を表示する方法を示します。CountUsers と GetUser の戻り値は、2 つの TextBlock コントロールに表示されます。
XAML
<UserControl x:Class="SilverlightApplication1.Page" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="600" Height="110"> <Grid x:Name="LayoutRoot" ShowGridLines="True" Background="White"> <Grid.RowDefinitions> <RowDefinition Height="60"/> <RowDefinition Height="25"/> <RowDefinition Height="25"/> </Grid.RowDefinitions> <Button Grid.Row="0" Margin=" 5" Height="50" Content="Click Here to Access the Web Service" Click="OnClick"/> <TextBlock Text="The number of users." x:Name="userCountResult" Grid.Row="1"/> <TextBlock Text="Property values of user with ID = 1." x:Name="getUserResult" Grid.Row="2"/> </Grid> </UserControl>
CountUsers と GetUser は、次のデータを返します。
- The number of users is: 2 (ユーザー数: 2)
- Property values of user with ID = 1 are: Name:Paul, Age:24, IsMember:True (ID が 1 のユーザーのプロパティ値: 名前: ポール、年齢: 24、会員/非会員: 会員)
次の図は、[Click Here to Access the Web Service] (Web サービスにアクセス) ボタンをクリックしたときの出力例です。
参照
フィードバックを送信する