您可以使用 IRequestChannel 或 IOutputChannel 圖形來叫用 Oracle 資料庫配接器的作業,以將訊息傳送至配接器。 基本模式是使用系結 (OracleDBBinding) 和從連線 URI 建立的端點,為所需的通道圖形建立通道處理站。 接著,您可以建立 Message 實例,代表符合目標作業之訊息架構的 SOAP 訊息。 然後,您可以使用從通道處理站建立的通道,將此 訊息 傳送至 Oracle 資料庫配接器。 如果您使用 IRequestChannel,您會收到回應。 如果在 Oracle 資料庫上執行作業時發生問題,Oracle 資料庫配接器會擲回 Microsoft.ServiceModel.Channels.Common.TargetSystemException。
如需如何在 WCF 中使用
本主題中的各節提供資訊,協助您使用WCF通道模型在 Oracle 資料庫配接器上叫用作業。
建立和取用輸出作業的訊息
若要在 Oracle Database 配接器上叫用作業,您可以使用 IRequestChannel 或 IOutputChannel 來傳送目標作業的要求訊息。 如果您使用 IRequestChannel ,配接器會傳回回應消息中作業的結果。
如需要求和回應消息架構以及每個作業之訊息動作的詳細資訊,請參閱 BizTalk Adapter for Oracle Database 的訊息和訊息架構。
如何建立要求訊息並取用回應訊息,判斷節點串流或節點值串流是由配接器執行。 接著,這將決定支援的作業是否執行 LOB 資料的端到端串流。
建立要求訊息
您可以使用下列兩種方式之一建立要求訊息:
若要建立可用於節點值串流的訊息,您必須在實作節點值串流的 XmlBodyWriter 中傳遞訊息本文。
若要建立可用於節點串流的訊息,您可以在 XmlReader 中傳遞訊息本文。
您通常會使用節點值串流來支援要求訊息中 Oracle LOB 資料的端對端串流。 唯一支援這項功能的作業是UpdateLOB。
取用回應消息
您可以使用下列兩種方式之一來取用回應訊息:
若要使用節點值串流來取用訊息,您必須在回應訊息上呼叫 WriteBodyContents 方法,並將實作節點值串流的 XmlDictionaryWriter 傳遞給它。
若要使用節點串流來取用訊息,您可以在回應訊息上呼叫 GetReaderAtBodyContents 來取得 XmlReader。
您通常會使用節點值串流來支持回應消息中 Oracle LOB 資料的端對端串流。 有許多作業支援這項功能。
LOB 資料和訊息串流支援
如需 Oracle 資料庫配接器如何支援 LOB 數據串流的詳細資訊,請參閱 在 Oracle Database 配接器中串流大型對象數據類型。
如需在程式代碼中實作節點值串流以支援LOB數據的端對端串流的詳細資訊,請參閱 使用WCF通道模型串流Oracle Database LOB資料類型。
WCF 通道模型中針對輸出作業的交易支援。
配接器會執行您在 Oracle 資料庫專用交易內叫用的每個作業。 您可以藉由設定 TransactionIsolationLevel 系結屬性來控制這些交易的隔離等級。
關於本主題中使用的範例
本主題中的範例會使用 SCOTT。ACCOUNTACTIVITY 數據表。 SDK 範例附有生成這些檔案的腳本。 如需 SDK 範例的詳細資訊,請參閱 SDK 中的範例。
如何使用通道執行作業?
若要使用 IRequestChannel 叫用作業,請執行下列步驟。
如何用 IRequestChannel 的實例叫用作業
建置通道處理站 (ChannelFactory<IRequestChannel>)。 若要這樣做,您必須指定系結 (OracleDBBinding) 和端點位址。 您可以在程式代碼中以命令方式指定系結和端點位址,或在組態中以宣告方式指定。 如需如何在組態中指定系結和端點位址的詳細資訊,請參閱 使用 Oracle Database 建立通道。
// Create a binding OracleDBBinding binding = new OracleDBBinding(); // Create an endpoint address by using the connection URI EndpointAddress address = new EndpointAddress("oracledb://ADAPTER"); // Create the channel factory ChannelFactory<IRequestChannel> factory = new ChannelFactory<IRequestChannel>(binding, address);使用 ClientCredentials 屬性設定通道處理站的使用者名稱密碼認證。
factory.Credentials.UserName.UserName = "SCOTT"; factory.Credentials.UserName.Password = "TIGER";開啟通道處理站。
factory.Open();從工廠取得通道並開啟。
IRequestChannel channel = factory.CreateChannel(); channel.Open();建立目標作業的 訊息 實例。 請確定已指定目標作業的訊息動作。 在此範例中,訊息本文會透過透過檔案建立 XmlReader 來傳遞。 目標操作是 SCOTT/EMP 數據表上的選擇操作。
XmlReader readerIn = XmlReader.Create("SelectAllActivity.xml"); Message messageIn = Message.CreateMessage(MessageVersion.Default, "http://Microsoft.LobServices.OracleDB/2007/03/SCOTT/Table/ACCOUNTACTIVITY/Select", readerIn);叫用通道上的 Request 方法,將訊息傳送至 Oracle 資料庫配接器並接收回復。 如果 Oracle 資料庫遇到例外狀況,配接器會擲回 TargetSystemException。 (非 Oracle 例外狀況可能還有其他例外狀況。您可以從 TargetSystemException 的 InnerException.Message 屬性取得 Oracle 錯誤的描述。
try { Message messageOut = channel.Request(messageIn); } catch (Exception ex) { // handle exception }處理回應。 在此範例中,回應訊息上會呼叫 GetReaderAtBodyContents 以取得訊息本文。
XmlReader readerOut = messageOut.GetReaderAtBodyContents();當您完成處理回應消息時,請關閉讀取器和訊息。
readerOut.Close(); messageOut.Close();當您使用完通道和通道工廠後,請關閉它們。 關閉工廠將會關閉與其建立的所有通道。
channel.Close() factory.Close();您遵循相同的步驟來使用 IOutputChannel 形狀來傳送訊息,但有以下例外:
您會在步驟 1 中建立 ChannelFactory<IOutputChannel> 。
您在步驟 6 的通道上呼叫 Send 方法。
channel.Send(messageIn);。IOutputChannel 沒有傳回的回應消息。
範例
下列範例示範如何使用 IRequestChannel 通道叫用 Select 作業。 Select 回應消息會使用 XmlReader 進行處理,然後將返回的記錄數量寫入主控台。
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using Microsoft.ServiceModel.Channels;
using Microsoft.Adapters.OracleDB;
using System.Xml;
using System.IO;
using System.Runtime.Serialization;
namespace RequestChanneSample
{
class Program
{
static void Main(string[] args)
{
// The Select operation request message
const string selectRequestString =
"\<Select xmlns=\"http://Microsoft.LobServices.OracleDB/2007/03/SCOTT/Table/ACCOUNTACTIVITY\"\>" +
"<COLUMN_NAMES>*</COLUMN_NAMES>" +
"<FILTER>ACCOUNT = 100002</FILTER>" +
"</Select>";
try
{
// Create binding -- specify 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 the factory.
factory.Open();
// Get a channel.
IRequestChannel channel = factory.CreateChannel();
// Open the channel.
channel.Open();
// Create the request message from the string
StringReader strReader = new StringReader(selectRequestString);
XmlReader readerIn = XmlReader.Create(strReader);
Message requestMessage = Message.CreateMessage(MessageVersion.Default,
"http://Microsoft.LobServices.OracleDB/2007/03/SCOTT/Table/ACCOUNTACTIVITY/Select",
readerIn);
Send the message and get a respone
Message responseMessage = channel.Request(requestMessage);
// Get an XmlReader from the message
XmlReader readerOut = (XmlReader) responseMessage.GetReaderAtBodyContents();
// Count the number of records returned and write to the console.
readerOut.MoveToContent();
int numberOfRecordsReturned = 0;
while (readerOut.Read())
{
if (readerOut.NodeType == XmlNodeType.Element && readerOut.Name == "ACCOUNTACTIVITYRECORDSELECT")
numberOfRecordsReturned++;
}
Console.WriteLine("{0} records returned.", numberOfRecordsReturned);
// Close the output reader and message
readerOut.Close();
responseMessage.Close();
//Close channel
channel.Close();
//Close the factory
factory.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}