共用方式為


使用 SQL 配接器建立通道

在 WCF 通道模型中,您會在 SQL Server 資料庫上叫用作業,並透過 WCF 通道與 Microsoft BizTalk Adapter for SQL Server 交換 SOAP 訊息,以接收結果。

  • 您可以使用 IRequestChannelIOutputChannel 叫用輸出作業,以將訊息傳送至配接器。

  • 您將透過 IInputChannel 接收訊息,以執行 輪詢TypedPolling通知 作業,來接收輸入作業的訊息。

    本主題中的程式提供如何建立及設定用於輸入和輸出作業的通道圖形的相關信息。

建立用戶端外部通道

您可以使用 IRequestChannelIOutputChannel 來叫用 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 配接器來輪詢 SQL Server 資料庫數據表和檢視表。 接著,您可以使用此系結來建置通道接聽程式,您可以從中取得 IInputChannel 通道,以從配接器接收 PollingTypedPollingNotification 作業。

若要建立並開啟 IInputChannel 以接收輸入操作

  1. 建立 SQLBinding 的實例。

  2. 設定輸入作業所需的系結屬性。 例如,針對 輪詢 作業,您至少必須設定 InboundOperationTypePolledDataAvailableStatementPollingStatement 系結屬性,以設定 SQL 配接器來輪詢 SQL Server 資料庫。

  3. SQLBinding 上叫用 BuildChannelListener<IInputChannel> 方法,以建立通道接聽程式。 您可以將 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 通道模型開發應用程式