WCFクライアントアプリケーションのコーディング方法には,アプリケーション構成ファイルにWCFを設定する方法と設定しない方法があります。
アプリケーション構成ファイルにWCFを設定すると,設定した内容がプロキシオブジェクトによって取得され,接続先のOpenTP1のサービスグループが決定します。
複数のサービスグループを呼び出す場合には,アプリケーション構成ファイルに接続先のOpenTP1の設定を複数定義する必要があります。また,プロキシオブジェクトも呼び出すサービスグループの数だけ必要です。
アプリケーション構成ファイルにWCFを設定する場合のコーディング手順を次に示します。
アプリケーション構成ファイルに次のようにWCF連携機能が設定されている場合のコーディング例を示します。
<system.serviceModel>
<client>
<endpoint
name="Gyoumu1"
address="opentp1.rpc://MyProfile1/Gyoumu1"
binding="tp1IntegrationBinding"
contract="Hitachi.OpenTP1.ServiceModel.
TP1Integration.ITP1Rpc"
behaviorConfiguration="TP1BehaviorConfig">
</endpoint>
<endpoint
name="Gyoumu2"
address="opentp1.rpc://MyProfile1/Gyoumu2"
binding="tp1IntegrationBinding"
contract="Hitachi.OpenTP1.ServiceModel.
TP1Integration.ITP1Rpc"
behaviorConfiguration="TP1BehaviorConfig">
</endpoint>
</client>
…
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using Hitachi.OpenTP1.ServiceModel.TP1Integration;
namespace CSSample
{
class Program
{
static void Main(string[] args)
{
try
{
// (1) プロキシクラスのインスタンス化
TP1RpcClient client = new TP1RpcClient("Gyoumu1");
// (2) 入力パラメタにデータを設定
int inLen = 24;
int outLen = 32;
byte[] inBuffer = new byte[inLen];
byte[] outBuffer = null;
// (3) サービス要求の最大応答待ち時間を設定
client.Endpoint.Binding.SendTimeout = TimeSpan.MaxValue;
// (4) プロキシオブジェクトのCallメソッドを呼び出す
client.Call(
"Service",
inBuffer, inLen,
ref outBuffer, ref outLen);
// (5) サービスの応答を参照して必要な処理をする
}
catch (CommunicationException wcfExp)
{
// WCFに関連する例外
}
catch (Exception exp)
{
// その他の例外
}
}
}
}
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.ServiceModel
Imports Hitachi.OpenTP1.ServiceModel.TP1Integration
Module VBSample
Sub Main()
Try
' (1) プロキシクラスのインスタンス化
Dim client As TP1RpcClient = New TP1RpcClient("Gyoumu1")
' (2) 入力パラメタにデータを設定
Dim inLen As Integer = 24
Dim outLen As Integer = 32
Dim inBuffer() As Byte = New Byte(inLen) {}
Dim outBuffer() As Byte = Nothing
' (3) サービス要求の最大応答待ち時間を設定
client.Endpoint.Binding.SendTimeout = TimeSpan.MaxValue
' (4) プロキシオブジェクトのCallメソッドを呼び出す
client.Call("Service", _
inBuffer, inLen, _
outBuffer, outLen)
' (5) サービスの応答を参照して必要な処理をする
Catch wcfExp As CommunicationException
' WCFに関連する例外
Catch exp As Exception
' その他の例外
End Try
End Sub
End Module
アプリケーション構成ファイルにWCFの設定をしない場合は,接続先のOepnTP1の位置情報をオブジェクト(System.ServiceModel.EndpointAddress)に設定することで,呼び出すOpenTP1のサービスグループを決定します。
複数のサービスグループを呼び出す場合には,位置情報を設定するオブジェクトが複数必要です。また,プロキシオブジェクトも呼び出すサービスグループの数だけ必要です。
なお,Systemで始まる名前空間に属するクラスの詳細については,.NET Frameworkのドキュメントを参照してください。
アプリケーション構成ファイルにWCFを設定しない場合のコーディング手順を次に示します。
アプリケーション構成ファイルにWCFの設定をしない場合のコーディング例を示します。
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using Hitachi.OpenTP1.ServiceModel.TP1Integration;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
// (1) EndpointAddressクラスのインスタンス化
EndpointAddress address =
new EndpointAddress(
"opentp1.rpc://MyProfile1/Gyoumu1");
// (2) TP1IntegrationBindingクラスのインスタンス化
TP1IntegrationBinding binding =
new TP1IntegrationBinding();
// (3) プロキシクラスのインスタンス化
TP1RpcClient client =
new TP1RpcClient(binding, address);
// (4) TP1IntegrationBehaviorクラスのインスタンス化
TP1IntegrationBehavior behavior =
new TP1IntegrationBehavior();
// (5) KeyedByTypeCollectionオブジェクトの
// Addメソッドを呼び出す
client.Endpoint.Behaviors.Add(behavior);
// (6) 入力パラメタにデータを設定
int inLen = 24;
int outLen = 32;
byte[] inBuffer = new byte[inLen];
byte[] outBuffer = null;
// (7) サービス要求の最大応答待ち時間を設定
client.Endpoint.Binding.SendTimeout = TimeSpan.MaxValue;
// (8) プロキシオブジェクトのCallメソッドを呼び出す
client.Call(
"Service",
inBuffer, inLen,
ref outBuffer, ref outLen);
// (9) サービスの応答を参照して必要な処理をする
}
catch (CommunicationException wcfExp)
{
// WCFに関連する例外
}
catch (Exception exp)
{
// その他の例外
}
}
}
}
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.ServiceModel
Imports Hitachi.OpenTP1.ServiceModel.TP1Integration
Module VBSample
Sub Main()
Try
' (1) EndpointAddressクラスのインスタンス化
Dim address As EndpointAddress = _
New EndpointAddress( _
"opentp1.rpc://MyProfile1/Gyoumu1")
' (2) TP1IntegrationBindingクラスのインスタンス化
Dim binding As TP1IntegrationBinding = _
New TP1IntegrationBinding()
' (3) プロキシクラスのインスタンス化
Dim client As TP1RpcClient = _
New TP1RpcClient(binding, address)
' (4) TP1IntegrationBehaviorクラスのインスタンス化
Dim behavior As TP1IntegrationBehavior = _
New TP1IntegrationBehavior()
' (5) KeyedByTypeCollectionオブジェクトの
' Addメソッドを呼び出す
client.Endpoint.Behaviors.Add(behavior)
' (6) 入力パラメタにデータを設定
Dim inLen As Integer = 24
Dim outLen As Integer = 32
Dim inBuffer() As Byte = New Byte(inLen) {}
Dim outBuffer() As Byte = Nothing
' (7) サービス要求の最大応答待ち時間を設定
client.Endpoint.Binding.SendTimeout = TimeSpan.MaxValue
' (8) プロキシオブジェクトのCallメソッドを呼び出す
client.Call("Service", _
inBuffer, inLen, _
outBuffer, outLen)
' (9) サービスの応答を参照して必要な処理をする
Catch wcfExp As CommunicationException
' WCFに関連する例外
Catch exp As Exception
' その他の例外
End Try
End Sub
End Module