使用 SQL 适配器创建通道

在 WCF 通道模型中,调用SQL Server数据库上的操作,并通过 WCF 通道与 Microsoft BizTalk 适配器交换 SOAP 消息以SQL Server来接收结果。

  • 可以使用 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 通道,以从适配器接收 轮询TypedPolling通知 操作。

创建并打开 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 通道模型开发应用程序