WCF入門-03。基本構成のWCFサンプルコード。

WCFの基本的な構成のサンプルコードを作ります。WCFService.zip 直

以下の3つのプロジェクトを作成し、WCFシステムを構成していきます。

  • WCFサーバー(コンソールアプリケーション:.exe)
  • WCFクライアント(ウインドウアプリケーション:.exe)
  • WCFインタフェース(クラスライブアラリ:.dll)

WCFでクライアント/サーバ間でやりとりするオブジェクトやサービスは、お互いに参照しあうのでWCFインタフェースプロジェクトとして分離しクライアント/サーバの両方のプロジェクトから参照設定するようにします。Visual Studio 2008 では、マルチスタートアッププロジェクトという機能があり、複数のアプリ(今回であればクライアントとサーバ)を起動して同時にデバッグできる機能があります。この機能は、ソリューションエクスプローラでソリューションの項目を選択したうえで右クリックを押すとメニューがでてきます。両方のエンドポイントにブレークポイントを設定し、通信できることを確かめてみたいと思います。

各プロジェクト共通の設定
WCFアプリケーションを作成する場合、以下のコンポーネントを参照に追加する必要があります。WCFを構成する基本ライブラリです。


System.ServiceModel

  • WCFインタフェース(クラスライブアラリ:.dll)

サービスとして公開したいクラスには、System.ServiceModel.ServiceContract属性、公開したいメソッドにはSystem.ServiceModel.OperationContract属性を付けます。また、メソッドを通してオブジェクトをやり取りする場合には通信に使用するオブジェクトにSystem.Serializable属性を付けます。(System.Serializable属性は必須では無いようです。)


Imports System.ServiceModel

_
Public Interface ISampleService
_
Function HelloWCF(ByVal param As BasicParameter) As BasicResult
End Interface

Public Enum TypeEnum
Text = 1
XML = 2
End Enum

Public Enum ResultEnum
Success = 0
Failed = 1
End Enum

_
Public Class BasicParameter
Public Type As TypeEnum = TypeEnum.Text
Public Message As String = String.Empty
End Class

_
Public Class BasicResult
Public Result As ResultEnum = ResultEnum.Success
Public Message As String = String.Empty
End Class

  • WCFサーバー(コンソールアプリケーション:.exe)

インタフェースを継承したクラスと、WCFのスタートアップ処理を書きます。ServiceHostをオープンするとクライアントからのリクエストを受け付ける状態になり、リクエストが着たら別スレッドを起こしてインタフェースを継承したクラスを実行する処理をバックグラウンドでやってくれます。各リクエストの実行処理はWCFが行います。


Imports System.ServiceModel

Public Class StartUp

Public Shared Sub Main()

Dim baseUri As Uri = New Uri("http://localhost:8000/WCFService")
Dim host As ServiceHost = New ServiceHost(GetType(SampleService), baseUri)

Dim httpBind As System.ServiceModel.BasicHttpBinding = Nothing
httpBind = New System.ServiceModel.BasicHttpBinding()

host.AddServiceEndpoint(GetType(WCFInterface.ISampleService), httpBind, "HelloWCF")

host.Open()

Try
Console.WriteLine("何か入力すると終了します。")
Console.ReadLine()
Finally
host.Close()
End Try

End Sub

Public Class SampleService
Implements WCFInterface.ISampleService

Public Function HelloWCF(ByVal param As WCFInterface.BasicParameter) As WCFInterface.BasicResult Implements WCFInterface.ISampleService.HelloWCF

Console.WriteLine(param.Type.ToString)
Console.WriteLine(param.Message.ToString)

Dim result As New WCFInterface.BasicResult

result.Result = WCFInterface.ResultEnum.Success
result.Message = "Hello WCF is success"

Return result

End Function

End Class

End Class

  • WCFクライアント(ウインドウアプリケーション:.exe)

WCFサービスのエンドポイントに接続してサーバにリクエストを行います。ChannelFactory.CreateChannelによってエンドポイントで公開しているオブジェクトを取得することが出来ます。(サーバのオブジェクトを作成しています。)その後は通常のメソッド呼び出しで、処理を記載できます。


Imports System.ServiceModel
Imports WCFInterface

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim endPoint As EndpointAddress = Nothing
endPoint = New EndpointAddress("http://localhost:8000/WCFService/HelloWCF")

Dim httpBind As System.ServiceModel.BasicHttpBinding = Nothing
httpBind = New System.ServiceModel.BasicHttpBinding()

Dim proxy As WCFInterface.ISampleService = Nothing
proxy = ChannelFactory(Of WCFInterface.ISampleService).CreateChannel(httpBind, endPoint)

Dim param As New WCFInterface.BasicParameter
Dim result As WCFInterface.BasicResult = Nothing

param.Type = TypeEnum.Text
param.Message = "Hello WCF TextMessage"

result = proxy.HelloWCF(param)

MsgBox(result.Result.ToString + vbCrLf + result.Message)

param.Type = TypeEnum.XML
param.Message = "Hello WCF XMLMessage"

result = proxy.HelloWCF(param)

MsgBox(result.Result.ToString + vbCrLf + result.Message)

End Sub

End Class