分散トランザクション処理機能 TP1/Connector for .NET Framework 使用の手引

[目次][用語][索引][前へ][次へ]

1.9.4 WCFクライアントアプリケーションのコーディング方法

WCFクライアントアプリケーションのコーディング方法には,アプリケーション構成ファイルにWCFを設定する方法と設定しない方法があります。

<この項の構成>
(1) アプリケーション構成ファイルにWCFを設定する場合
(2) アプリケーション構成ファイルにWCFを設定しない場合

(1) アプリケーション構成ファイルにWCFを設定する場合

アプリケーション構成ファイルにWCFを設定すると,設定した内容がプロキシオブジェクトによって取得され,接続先のOpenTP1のサービスグループが決定します。

複数のサービスグループを呼び出す場合には,アプリケーション構成ファイルに接続先のOpenTP1の設定を複数定義する必要があります。また,プロキシオブジェクトも呼び出すサービスグループの数だけ必要です。

(a) コーディング手順

アプリケーション構成ファイルにWCFを設定する場合のコーディング手順を次に示します。

  1. プロキシクラス(Hitachi.OpenTP1.ServiceModel.TP1Integration.TP1RpcClient)をインスタンス化します。
    アプリケーション構成ファイルに定義した内容を取得するため,引数には<endpoint>要素のname属性の値を指定します。
  2. 入力パラメタにデータを設定します。
    サービスの応答の領域には,C#の場合はnullを,Visual Basicの場合はNothingを設定します。
  3. 必要に応じて,サービスを要求してからサービスの応答が返るまでの最大応答待ち時間を設定します。
    最大応答待ち時間については,「1.9.6 サービス要求の最大応答待ち時間の設定方法」を参照してください。
  4. プロキシオブジェクトのCallメソッドを呼び出します。
  5. サービスの応答を参照して,必要な処理をします。
(b) コーディング例

アプリケーション構成ファイルに次のように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>
   …
 
コーディング例(C#の場合)
 
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)
      {
       // その他の例外
      }
    }
  }
}
 
コーディング例(Visual Basicの場合)
 
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
 

(2) アプリケーション構成ファイルにWCFを設定しない場合

アプリケーション構成ファイルにWCFの設定をしない場合は,接続先のOepnTP1の位置情報をオブジェクト(System.ServiceModel.EndpointAddress)に設定することで,呼び出すOpenTP1のサービスグループを決定します。

複数のサービスグループを呼び出す場合には,位置情報を設定するオブジェクトが複数必要です。また,プロキシオブジェクトも呼び出すサービスグループの数だけ必要です。

なお,Systemで始まる名前空間に属するクラスの詳細については,.NET Frameworkのドキュメントを参照してください。

(a) コーディング手順

アプリケーション構成ファイルにWCFを設定しない場合のコーディング手順を次に示します。

  1. System.ServiceModel.EndpointAddressクラスをインスタンス化します。
    コンストラクタの引数には,URIを指定します。URIの指定方法については,「1.9.5 接続先OepnTP1のサービスの位置情報の指定形式」を参照してください。
  2. Hitachi.OpenTP1.ServiceModel.TP1Integration.TP1IntegrationBindingクラスをインスタンス化します。
  3. プロキシクラス(Hitachi.OpenTP1.ServiceModel.TP1Integration.TP1RpcClientクラス)をインスタンス化します。
    コンストラクタの引数には,1.でインスタンス化したEndpointAddressオブジェクト,および2.でインスタンス化したTP1IntegrationBindingオブジェクトを指定します。
  4. Hitachi.OpenTP1.ServiceModel.TP1Integration.TP1IntegrationBehaviorクラスをインスタンス化します。
  5. プロキシオブジェクトのEndpoint.Behaviorsプロパティから得られるSystem.Collections.Generic.KeyedByTypeCollection<System.ServiceModel.Description.IEndpointBehavior>オブジェクトのAddメソッドで,4.でインスタンス化したTP1IntegrationBehaviorオブジェクトを追加します。
  6. 入力パラメタにデータを設定します。
    サービスの応答の領域には,C#の場合はnullを,Visual Basicの場合はNothingを設定します。
  7. 必要に応じて,サービスを要求してからサービスの応答が返るまでの最大応答待ち時間を設定します。
    最大応答待ち時間については,「1.9.6 サービス要求の最大応答待ち時間の設定方法」を参照してください。
  8. プロキシオブジェクトのCallメソッドを呼び出します。
  9. サービスの応答を参照して,必要な処理をします。
(b) コーディング例

アプリケーション構成ファイルにWCFの設定をしない場合のコーディング例を示します。

コーディング例(C#の場合)
 
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)
      {
        // その他の例外
      }
    }
  }
}
 
コーディング例(Visual Basicの場合)
 
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