Windows AzureのWebロールをDevelopment Fabricから(.NET Framework 4ではかろうじて残っている)System.Data.OracleClientクラスライブラリを経由してOracle Databaseに接続してみました。
ついでにWebロールに敢えてASP.NET XML Webサービスを実装して、そいつをWindows Phone 7のエミュレータからつないでみました。
図示すると↓のような感じです。
Azureアプリ側
まず、Azureアプリ側から作成してみます。
新規プロジェクトでWindows Azureサービスを選んで、Webロールのみ選択してソリューションを生成します(ソリューション名はOracleCloudとしました)。
初期状態のWebロールにはいろいろなaspxファイルが含まれていますが、Global.asax、Web.Config、WebRole.vbだけ残して削除して、項目の新規追加でWebサービスを選び「OracleWebService.asmx」とします。
OracleWebService.asmx.vbには次のようなコードを記述します。
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Data.OracleClient 'Oracle.DataAccess.Client
Imports System.Configuration.ConfigurationManager
<system.web.services.webservice (NAMESPACE:="http://microsoft.com/webservices/”> _
<system.web.services.webservicebinding (ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<toolboxitem (FALSE)> _
Public Class OracleWebService
Inherits System.Web.Services.WebService
<webmethod ()> _
Public Function GetRecordsList(ByVal userName As String,
ByVal Password As String) As EMPData()
Dim dsList As New List(Of EMPData)
Dim ds As System.Data.DataSet
ds = GetRecords(userName, Password)
For Each row As System.Data.DataRow In ds.Tables("EMP").Rows
dsList.Add(New EMPData With {.EMPNO = row.Item("EMPNO").ToString,
.ENAME = row.Item("ENAME").ToString,
.JOB = row.Item("JOB").ToString})
Next
Return dsList.ToArray
End Function
Private Function GetRecords(ByVal userName As String,
ByVal password As String) As System.Data.DataSet
Dim ds As New System.Data.DataSet
Using _cn As New OracleConnection
Try
_cn.ConnectionString = String.Format(AppSettings("ConnectionString"),
userName,
password)
_cn.Open()
Using _cmd As New OracleCommand
_cmd.Connection = _cn
_cmd.CommandText = "SELECT * FROM SCOTT.EMP ORDER BY EMPNO"
Using _da As New OracleDataAdapter
_da.SelectCommand = _cmd
_da.Fill(ds, "EMP")
End Using
End Using
Finally
_cn.Close()
End Try
End Using
Return ds
End Function
End Class
Public Class EMPData
Public EMPNO As String
Public ENAME As String
Public JOB As String
End Class
動かしてみます。
Windows Phone 7アプリ側
AzureアプリはASP.NET XML Webサービスとして実装しているロジックを呼び出すので、Windows Phone 7アプリのプロジェクトではサービス参照の追加を行います。
XAMLでのデザインをおこないます。
<!--LayoutRoot is the root grid where all page content is placed-->
<StackPanel x:Name="LayoutRoot" Background="Transparent" Width="480">
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Margin="12,17,0,3">
<TextBlock x:Name="ApplicationTitle" Text="SAMPLE APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text=" Client" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<StackPanel x:Name="LoginPanel" Margin="12,17,0,3" Visibility="Visible">
<TextBox x:Name="UserID_TextBox" Text="" />
<TextBox x:Name="Password_TextBox" Text="" />
<Button x:Name="Login_Button" Content="ログイン" />
</StackPanel>
<StackPanel x:Name="ResultPanel" Margin="12,17,0,3" Visibility="Collapsed">
<Button x:Name="Search_Button" Content="再検索" />
<Button x:Name="Logout_Button" Content="ログアウト" />
<ListBox HorizontalAlignment="Left" Name="Result_ListBox" VerticalAlignment="Top">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding EMPNO}" TextWrapping="NoWrap" Foreground="Green" />
<TextBlock Text="{Binding ENAME}" TextWrapping="NoWrap" />
<TextBlock Text="{Binding JOB}" TextWrapping="NoWrap" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</StackPanel>
次にコードを記述します。
Partial Public Class MainPage
Inherits PhoneApplicationPage
' Constructor
Public Sub New()
InitializeComponent()
End Sub
Private Sub Login_Button_Click(ByVal sender As System.Object,
ByVal e As System.Windows.RoutedEventArgs) _
Handles Login_Button.Click
Dim webs As New OracleServiceReference.OracleWebServiceSoapClient
Me.Cursor = Cursors.Wait
Me.UserID_TextBox.IsEnabled = False
Me.Password_TextBox.IsEnabled = False
Me.Login_Button.IsEnabled = False
AddHandler webs.GetRecordsListCompleted, AddressOf GetRecordsCompleted
webs.GetRecordsListAsync(Me.UserID_TextBox.Text,
Me.Password_TextBox.Text)
End Sub
Private Sub GetRecordsCompleted(ByVal sender As System.Object,
ByVal e As OracleServiceReference.GetRecordsListCompletedEventArgs)
If e.Error Is Nothing Then
Me.LoginPanel.Visibility = Visibility.Collapsed
Me.ResultPanel.Visibility = Visibility.Visible
Me.Result_ListBox.ItemsSource = e.Result
Else
MessageBox.Show(e.Error.Message)
End If
Me.UserID_TextBox.IsEnabled = True
Me.Password_TextBox.IsEnabled = True
Me.Login_Button.IsEnabled = True
Me.Cursor = Cursors.Arrow
End Sub
Private Sub Logout_Button_Click(ByVal sender As System.Object,
ByVal e As System.Windows.RoutedEventArgs) _
Handles Logout_Button.Click
Me.LoginPanel.Visibility = Visibility.Visible
Me.ResultPanel.Visibility = Visibility.Collapsed
End Sub
End Class
それではさっそく実行してみましょう。まずAzureアプリのソリューションを実行て、それからWindows Phone 7アプリのソシューリョンで実行します。

開発環境だけの夢なのでしょうか?
これまでの手順でうまくいのは、AzureアプリがDevelopment Fabric上で動いているからです。
Development Fabricなので開発環境でインストールしたOracle Clientを使ってOracle Databaseを接続している。
そのため、このままAzureアプリをWindows Azureに発行してもOracle Databaseに接続するのは難しいようです。
考慮しなければならないのは次の2点
- ClientをどうやってWindows Azureに発行するか
- WebロールからOracle Databaseに接続しに行けるのか
前者については、Oracle Clientには「Oracle Instant Client」といってファイルのコピーで動作する版がありますのでOracle Instant Clientをコンテンツとして常にコピーするようにAzureアプリのプロジェクトで設定しておけば良いはずです。
後者については、やってみたらWebロールからWindows Azure外に接続ができるという事ですので、まだ確認していませんが、公開されているOracle Databaseには接続できるかもしれません。