WCF入門-07。クライアントのアプリケーション構成ファイルを作成する。
サンプルコード。WCFService-07.zip
[サービス参照の追加]を使うとWSDLで公開されたウェブサービスにアクセスするためのアプリケーション構成ファイルとカスタムクラスを作成できますが、WCFサーバーを自作している場合は、WCFインタフェースのクラスライブラリがあるので、WSDL経由でカスタムクラスを作成すると返って修正が面倒になってしまいます。インタフェースを変更するとWSDLを再度取り込む必要があるためです。今回は、サービス参照の追加は使用せずにWCFインタフェースのクラスライブラリを参照設定することにし、アプリケーション構成ファイルを手動で作成します。
- クライアントのアプリケーション構成ファイル(最小構成)
アドレス:"http://localhost:8000/WCFService/HelloWCF" 、バインディング:"basicHttpBinding"、コントラクトが"WCFInterface.ISampleService"で構成しています。
- クライアント側のサービス呼び出しロジック
アプリケーション構成ファイルを使用したカスタムクラスを用意する必要があります。System.ServiceModel.ClientBaseクラスを継承して、サービスのインタフェースをインプリメントします。
Imports System.ServiceModel
Imports WCFInterfacePublic Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim client As New SampleServiceClient
Dim param As New WCFInterface.BasicParameter
Dim result As WCFInterface.BasicResult = Nothingparam.Type = TypeEnum.Text
param.Message = "Hello WCF TextMessage"result = client.HelloWCF(param)
MsgBox(result.Result.ToString + vbCrLf + result.Message)
param.Type = TypeEnum.XML
param.Message = "- Hello WCF XMLMessage
"result = client.HelloWCF(param)
MsgBox(result.Result.ToString + vbCrLf + result.Message)
client.Close()
End Sub
End Class
Public Class SampleServiceClient
Inherits System.ServiceModel.ClientBase(Of WCFInterface.ISampleService)
Implements WCFInterface.ISampleServicePublic Function HelloWCF(ByVal param As WCFInterface.BasicParameter) As WCFInterface.BasicResult Implements WCFInterface.ISampleService.HelloWCF
Return MyBase.Channel.HelloWCF(param)
End FunctionEnd Class