在 WCF 通道模型中,通过 WCF 通道与用于 Oracle 数据库的 Microsoft BizTalk 适配器交换 SOAP 消息来调用 Oracle 数据库上的操作,然后接收轮询查询的结果。
- 通过使用 IRequestChannel 或 IOutputChannel 将消息发送到适配器,您可以调用操作(出站操作)。 
- 您可以通过 IInputChannel 接收 POLLINGSTMT 消息,以获取基于轮询的数据更改消息。 - 本节中的主题提供了有关如何创建和配置用于入站和出站操作的通道形状的信息。 
创建出站(客户端)通道
可以使用 IRequestChannel 或 IOutputChannel 调用 Oracle 数据库上的操作。 在任一情况下,都首先使用相应的接口创建 System.ServiceModel.ChannelFactory 。 然后使用工厂创建通道。 创建信道后,可以使用它来调用适配器上的操作。
创建和打开出站通道
- 使用终结点和绑定为所需通道形状创建和初始化 ChannelFactory 实例。 终结点指定 Oracle 连接 URI,绑定是 OracleDBBinding 的实例。 
- 使用 Credentials 属性为通道工厂提供 Oracle 凭据。 
- 打开通道工厂。 
- 通过在通道工厂上调用 CreateChannel 方法获取通道的实例。 
- 打开通道。 - 可以在代码或配置中指定绑定和终结点地址。 
在代码中指定绑定和终结点地址
下面的代码示例演示如何通过在代码中指定绑定和终结点地址来创建 IRequestChannel 。 创建 IOutputChannel 的代码相同,只是必须为 ChannelFactory 和通道类型指定 IOutputChannel 接口。
// Create binding -- set binding properties before you open the factory.  
OracleDBBinding odbBinding = new OracleDBBinding();  
  
// Create address.  
EndpointAddress odbAddress = new EndpointAddress("oracledb://ADAPTER/");  
  
// Create channel factory from binding and address.  
ChannelFactory<IRequestChannel> factory =   
    new ChannelFactory<IRequestChannel>(odbBinding, odbAddress);  
  
// Specify credentials.   
factory.Credentials.UserName.UserName = "SCOTT";  
factory.Credentials.UserName.Password = "TIGER";  
  
// 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 = "SCOTT";  
factory.Credentials.UserName.Password = "TIGER";  
  
// Open the factory.  
factory.Open();  
  
// Get a channel and open it.  
IRequestChannel channel = factory.CreateChannel();  
channel.Open();  
配置设置
以下代码显示了用于上述示例的配置设置。 客户端终结点的协定必须是“System.ServiceModel.Channels.IRequestChannel”或“System.ServiceModel.Channels.IRequestChannel”,具体取决于要创建的通道形状的类型。
<?xml version="1.0" encoding="utf-8"?>  
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">  
    <system.serviceModel>  
        <bindings>  
            <oracleDBBinding>  
                <binding name="OracleDBBinding" closeTimeout="00:01:00" openTimeout="00:01:00"  
                    receiveTimeout="00:10:00" sendTimeout="00:01:00" metadataPooling="true"  
                    statementCachePurge="false" statementCacheSize="10" pollingInterval="500"  
                    useOracleConnectionPool="true" minPoolSize="1" maxPoolSize="100"  
                    incrPoolSize="5" decrPoolSize="1" connectionLifetime="0" acceptCredentialsInUri="false"  
                    useAmbientTransaction="true" polledDataAvailableStatement="SELECT 1 FROM DUAL"  
                    pollWhileDataFound="false" notifyOnListenerStart="true" notificationPort="-1"  
                    inboundOperationType="Polling" dataFetchSize="65536" longDatatypeColumnSize="0"  
                    skipNilNodes="true" maxOutputAssociativeArrayElements="32"  
                    enableSafeTyping="false" insertBatchSize="1" useSchemaInNameSpace="true"  
                    enableBizTalkCompatibilityMode="false" enablePerformanceCounters="false" />  
            </oracleDBBinding>  
        </bindings>  
        <client>  
            <endpoint address="oracledb://adapter/" binding="oracleDBBinding"  
                bindingConfiguration="OracleDBBinding" contract="System.ServiceModel.Channels.IRequestChannel"  
                name="MyRequestChannel" />  
        </client>  
    </system.serviceModel>  
</configuration>  
创建入站(服务)通道
通过对 OracleDBBinding 实例设置绑定属性,将 Oracle 数据库适配器配置为轮询 Oracle 数据库表和视图。 然后,使用此绑定生成通道侦听器,可以从该侦听器中获取 IInputChannel 通道,以便接收来自适配器的入站操作的消息。
创建并打开 IInputChannel 以接收用于入站操作的消息。
- 创建 OracleDBBinding 实例。 
- 设置入站操作所需的绑定属性。 例如,对于 POLLINGSTMT 操作,至少必须设置 InboundOperationType、PollingStatement 和 PollingInterval 绑定属性,将 Oracle 数据库适配器配置为来轮询 Oracle 数据库。 
- 使用 BindingParameterCollection 类创建绑定参数集合并设置凭据。 
- 通过在 OracleDBBinding 上调用 BuildChannelListener<IInputChannel> 方法创建通道侦听器。 将 Oracle 连接 URI 指定为此方法的参数之一。 有关 Oracle 连接 URI 的详细信息,请参阅 “创建 Oracle 数据库连接 URI”。 
- 打开侦听器。 
- 通过在侦听器上调用 AcceptChannel 方法获取 IInputChannel 通道。 
- 打开通道。 - 以下代码演示如何创建通道侦听器,并使用 POLLINGSTMT 操作从适配器获取 IInputChannel 以处理入站消息。 
注释
Oracle 数据库适配器仅支持单向接收。 因此,必须使用 IInputChannel 从 Oracle 数据库接收入站操作的消息。
// Create a binding: specify the InboundOperationType, PollingInterval (in seconds), the PollingStatement, and  
// the PostPollStatement.  
OracleDBBinding binding = new OracleDBBinding();  
binding.InboundOperationType = InboundOperation.Polling;  
binding.PollingInterval = 30;  
binding.PollingStatement = "SELECT * FROM ACCOUNTACTIVITY FOR UPDATE";  
binding.PostPollStatement = "BEGIN ACCOUNT_PKG.PROCESS_ACTIVITY(); END;";  
  
// Create a binding parameter collection and set the credentials  
ClientCredentials credentials = new ClientCredentials();  
credentials.UserName.UserName = "SCOTT";  
credentials.UserName.Password = "TIGER";  
  
BindingParameterCollection bindingParams = new BindingParameterCollection();  
bindingParams.Add(credentials);  
  
// Get a listener from the binding and open it.  
Uri connectionUri = new Uri("oracleDB://ADAPTER");  
IChannelListener<IInputChannel> listener = binding.BuildChannelListener<IInputChannel>(connectionUri, bindingParams);  
listener.Open();  
  
// Get a channel from the listener and open it.  
channel = listener.AcceptChannel();  
channel.Open();