次の方法で共有


SQL アダプターを使用してチャネルを作成する

WCF チャネル モデルでは、SQL Server データベースに対する操作を呼び出し、WCF チャネル経由で SOAP メッセージを Microsoft BizTalk Adapter for SQL Serverと交換して結果を受け取ります。

  • 送信操作を呼び出すには、 IRequestChannel または IOutputChannel を使用してアダプターにメッセージを送信します。

  • 受信操作のメッセージを受信する場合は、ポーリングTypedPolling、または Notification 操作の IInputChannel を介してメッセージを受信します。

    このトピックの手順では、受信および送信操作に使用されるチャネル図形を作成および構成する方法について説明します。

送信 (クライアント) チャネルの作成

IRequestChannel または IOutputChannel を使用して、SQL Server データベースに対する操作を呼び出すことができます。 どちらの場合も、最初に適切なインターフェイスを使用して System.ServiceModel.ChannelFactory を作成します。 次に、ファクトリを使用してチャネルを作成します。 チャネルを作成したら、それを使用してアダプターに対する操作を呼び出すことができます。

送信チャネルを作成して開くには

  1. エンドポイントとバインドを使用して、目的のチャネル図形の ChannelFactory のインスタンスを作成して初期化します。 エンドポイントはSQL Server接続 URI を指定し、バインドは sqlBinding のインスタンスです。

  2. Credentials プロパティSQL Server使用して、チャネル ファクトリの資格情報を指定します。

  3. チャネル ファクトリを開きます。

  4. チャネル ファクトリで CreateChannel メソッドを呼び出して、チャネルのインスタンスを取得します。

  5. チャネルを開きます。

    コードまたは構成からバインディングとエンドポイント アドレスを指定できます。

コードでのバインディングアドレスとエンドポイントアドレスの指定

次のコード例は、コードでバインドとエンドポイント アドレスを指定して IRequestChannel を作成する方法を示しています。 IOutputChannel を作成するコードは同じですが、ChannelFactory とチャネルの種類に IOutputChannel インターフェイスを指定する必要があります。

// Create binding -- set binding properties before you open the factory.  
SqlAdapterBinding sdbBinding = new SqlAdapterBinding();  
  
// Create address.  
EndpointAddress sdbAddress = new EndpointAddress("mssql://<sql_server_name>//<database_name>?");  
  
// Create channel factory from binding and address.  
ChannelFactory<IRequestChannel> factory =   
    new ChannelFactory<IRequestChannel>(sdbBinding, sdbAddress);  
  
// Specify credentials.   
factory.Credentials.UserName.UserName = "myuser";  
factory.Credentials.UserName.Password = "mypassword";  
  
// Open factory  
factory.Open();  
  
// Get channel and open it.  
IRequestChannel channel = factory.CreateChannel();  
channel.Open();  

構成でのバインディングとエンドポイント アドレスの指定

次のコード例は、構成で指定されたクライアント エンドポイントからチャネル ファクトリを作成する方法を示しています。

// Create channel factory from configuration.  
ChannelFactory<IRequestChannel> factory =  
new ChannelFactory<IRequestChannel>("MyRequestChannel");  
  
// Specify credentials.  
factory.Credentials.UserName.UserName = "myuser";  
factory.Credentials.UserName.Password = "mypassword";  
  
// Open the factory.  
factory.Open();  
  
// Get a channel and open it.  
IRequestChannel channel = factory.CreateChannel();  
channel.Open();  

構成設定

次のコードは、前の例で使用した構成設定を示しています。 クライアント エンドポイントのコントラクトは、作成するチャネル図形の種類に応じて、"System.ServiceModel.Channels.IRequestChannel" または "System.ServiceModel.Channels.IOutputChannel" である必要があります。

<?xml version="1.0" encoding="utf-8"?>  
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">  
    <system.serviceModel>  
        <bindings>  
            <sqlBinding>  
                <binding name="SqlAdapterBinding" closeTimeout="00:01:00" openTimeout="00:01:00"  
                    receiveTimeout="00:10:00" sendTimeout="00:01:00" maxConnectionPoolSize="100"  
                    encrypt="false" useAmbientTransaction="true" batchSize="20"  
                    polledDataAvailableStatement="" pollingStatement="" pollingIntervalInSeconds="30"  
                    pollWhileDataFound="false" notificationStatement="" notifyOnListenerStart="true"  
                    enableBizTalkCompatibilityMode="true" chunkSize="4194304"  
                    inboundOperationType="Polling" useDatabaseNameInXsdNamespace="false"  
                    allowIdentityInsert="false" enablePerformanceCounters="false"  
                    xmlStoredProcedureRootNodeName="" xmlStoredProcedureRootNodeNamespace="" />  
            </sqlBinding>  
        </bindings>  
        <client>  
            <endpoint address="mssql://mysqlserver//mydatabase?" binding="sqlBinding"  
                bindingConfiguration="SqlAdapterBinding" contract="System.ServiceModel.Channels.IRequestChannel"  
                name="MyRequestChannel" />  
        </client>  
    </system.serviceModel>  
</configuration>  

受信 (サービス) チャネルの作成

sqlBinding のインスタンスにバインド プロパティを設定して、SQL Serverデータベースのテーブルとビューをポーリングするように SQL アダプターを構成します。 次に、このバインディングを使用してチャネル リスナーを作成します。このリスナーから、アダプターからポーリングTypedPolling、または Notification 操作を受け取る IInputChannel チャネルを取得できます。

受信操作を受信する IInputChannel を作成して開くには

  1. SQLBinding のインスタンスを作成します。

  2. 受信操作に必要なバインド プロパティを設定します。 たとえば、ポーリング操作では、少なくとも InboundOperationTypePolledDataAvailableStatement、および PollingStatement バインド プロパティを設定して、SQL Server データベースをポーリングするように SQL アダプターを構成する必要があります。

  3. SQLBindingBuildChannelListener<IInputChannel メソッドを呼び出して、>チャネル リスナーを作成します。 このメソッドのパラメーターの 1 つとして、SQL Server接続 URI を指定します。

  4. リスナーを開きます。

  5. リスナーで AcceptChannel メソッドを呼び出して、IInputChannel チャネルを取得します。

  6. チャネルを開きます。

    次のコードは、チャネル リスナーを作成し、アダプターからデータ変更メッセージを受信する IInputChannel を取得する方法を示しています。

重要

SQL アダプターでは、一方向の受信のみがサポートされます。 そのため、IInputChannel を使用して、SQL Serverからの受信操作のメッセージを受信する必要があります。

// Create a binding: specify the InboundOperationType, the PolledDataAvailableStatement, and   
// the PollingStatement binding properties.  
SqlAdapterBinding binding = new SqlAdapterBinding();  
binding.InboundOperationType = InboundOperation.Polling;  
binding.PolledDataAvailableStatement = "SELECT COUNT (*) FROM EMPLOYEE";  
binding.PollingStatement = "SELECT * FROM Employee;EXEC MOVE_EMP_DATA;EXEC ADD_EMP_DETAILS John, Tester, 100000";  
  
// Create a binding parameter collection and set the credentials  
ClientCredentials credentials = new ClientCredentials();  
credentials.UserName.UserName = "myuser";  
credentials.UserName.Password = "mypassword";  
  
BindingParameterCollection bindingParams = new BindingParameterCollection();  
bindingParams.Add(credentials);  
  
// Get a listener from the binding and open it.  
Uri connectionUri = new Uri("mssql://mysqlserver//mydatabase?");  
IChannelListener<IInputChannel> listener = binding.BuildChannelListener<IInputChannel>(connectionUri, bindingParams);  
listener.Open();  
  
// Get a channel from the listener and open it.  
IInputChannel channel = listener.AcceptChannel();  
channel.Open();  

参照

WCF チャネル モデルを使用してアプリケーションを開発する