IpcChannel 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供使用 IPC 通訊協定傳輸訊息的通道實作。
public ref class IpcChannel : System::Runtime::Remoting::Channels::IChannelReceiver, System::Runtime::Remoting::Channels::IChannelSender, System::Runtime::Remoting::Channels::ISecurableChannel
public class IpcChannel : System.Runtime.Remoting.Channels.IChannelReceiver, System.Runtime.Remoting.Channels.IChannelSender, System.Runtime.Remoting.Channels.ISecurableChannel
type IpcChannel = class
interface IChannelReceiver
interface IChannelSender
interface IChannel
interface ISecurableChannel
type IpcChannel = class
interface IChannelReceiver
interface IChannel
interface IChannelSender
interface ISecurableChannel
Public Class IpcChannel
Implements IChannelReceiver, IChannelSender, ISecurableChannel
- 繼承
-
IpcChannel
- 實作
範例
下列程式代碼範例示範如何使用 IpcChannel 來設定遠端伺服器及其用戶端。 此範例包含三個部分:
伺服器
用戶端
伺服器和用戶端所使用的遠端物件。
下列程式代碼範例顯示伺服器。
#using <System.dll>
#using <System.Runtime.Remoting.dll>
#using "common.dll"
using namespace System;
using namespace System::Runtime::Remoting::Channels::Ipc;
void main()
{
// Create the server channel.
IpcChannel^ serverChannel = gcnew IpcChannel( L"localhost:9090" );
// Register the server channel.
System::Runtime::Remoting::Channels::ChannelServices::RegisterChannel( serverChannel );
// Show the name of the channel.
Console::WriteLine( L"The name of the channel is {0}.", serverChannel->ChannelName );
// Show the priority of the channel.
Console::WriteLine( L"The priority of the channel is {0}.", serverChannel->ChannelPriority );
// Show the URIs associated with the channel.
System::Runtime::Remoting::Channels::ChannelDataStore^ channelData = (System::Runtime::Remoting::Channels::ChannelDataStore^)serverChannel->ChannelData;
for each (String^ uri in channelData->ChannelUris)
{
Console::WriteLine("The channel URI is {0}.", uri);
}
// Expose an object for remote calls.
System::Runtime::Remoting::RemotingConfiguration::RegisterWellKnownServiceType(
RemoteObject::typeid,L"RemoteObject.rem",
System::Runtime::Remoting::WellKnownObjectMode::Singleton);
// Parse the channel's URI.
array<String^>^ urls = serverChannel->GetUrlsForUri( L"RemoteObject.rem" );
if ( urls->Length > 0 )
{
String^ objectUrl = urls[ 0 ];
String^ objectUri;
String^ channelUri = serverChannel->Parse( objectUrl, objectUri );
Console::WriteLine( L"The object URI is {0}.", objectUri );
Console::WriteLine( L"The channel URI is {0}.", channelUri );
Console::WriteLine( L"The object URL is {0}.", objectUrl );
}
// Wait for the user prompt.
Console::WriteLine( L"Press ENTER to exit the server." );
Console::ReadLine();
Console::WriteLine( L"The server is exiting." );
}
using System;
using System.Runtime.Remoting.Channels.Ipc;
public class Server
{
public static void Main(string[] args)
{
// Create the server channel.
IpcChannel serverChannel =
new IpcChannel("localhost:9090");
// Register the server channel.
System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(
serverChannel);
// Show the name of the channel.
Console.WriteLine("The name of the channel is {0}.",
serverChannel.ChannelName);
// Show the priority of the channel.
Console.WriteLine("The priority of the channel is {0}.",
serverChannel.ChannelPriority);
// Show the URIs associated with the channel.
System.Runtime.Remoting.Channels.ChannelDataStore channelData =
(System.Runtime.Remoting.Channels.ChannelDataStore)
serverChannel.ChannelData;
foreach (string uri in channelData.ChannelUris)
{
Console.WriteLine("The channel URI is {0}.", uri);
}
// Expose an object for remote calls.
System.Runtime.Remoting.RemotingConfiguration.
RegisterWellKnownServiceType(
typeof(RemoteObject), "RemoteObject.rem",
System.Runtime.Remoting.WellKnownObjectMode.Singleton);
// Parse the channel's URI.
string[] urls = serverChannel.GetUrlsForUri("RemoteObject.rem");
if (urls.Length > 0)
{
string objectUrl = urls[0];
string objectUri;
string channelUri = serverChannel.Parse(objectUrl, out objectUri);
Console.WriteLine("The object URI is {0}.", objectUri);
Console.WriteLine("The channel URI is {0}.", channelUri);
Console.WriteLine("The object URL is {0}.", objectUrl);
}
// Wait for the user prompt.
Console.WriteLine("Press ENTER to exit the server.");
Console.ReadLine();
Console.WriteLine("The server is exiting.");
}
}
下列程式代碼範例顯示此伺服器的用戶端。
#using <System.dll>
#using <System.Runtime.Remoting.dll>
#using "common.dll"
using namespace System;
using namespace System::Runtime::Remoting::Channels::Ipc;
void main()
{
// Create the channel.
IpcChannel^ channel = gcnew IpcChannel;
// Register the channel.
System::Runtime::Remoting::Channels::ChannelServices::RegisterChannel(channel);
// Register as client for remote object.
System::Runtime::Remoting::WellKnownClientTypeEntry^ remoteType = gcnew
System::Runtime::Remoting::WellKnownClientTypeEntry(
RemoteObject::typeid,L"ipc://localhost:9090/RemoteObject.rem" );
System::Runtime::Remoting::RemotingConfiguration::RegisterWellKnownClientType(remoteType);
// Create a message sink.
String^ objectUri;
System::Runtime::Remoting::Messaging::IMessageSink^ messageSink = channel->CreateMessageSink(
L"ipc://localhost:9090/RemoteObject.rem", nullptr, objectUri );
Console::WriteLine( L"The URI of the message sink is {0}.", objectUri );
if ( messageSink != nullptr )
{
Console::WriteLine( L"The type of the message sink is {0}.", messageSink->GetType() );
}
// Create an instance of the remote object.
RemoteObject^ service = gcnew RemoteObject;
// Invoke a method on the remote object.
Console::WriteLine( L"The client is invoking the remote object." );
Console::WriteLine( L"The remote object has been called {0} times.", service->GetCount() );
}
using System;
using System.Runtime.Remoting.Channels.Ipc;
public class Client
{
public static void Main(string[] args)
{
// Create the channel.
IpcChannel channel = new IpcChannel();
// Register the channel.
System.Runtime.Remoting.Channels.ChannelServices.
RegisterChannel(channel);
// Register as client for remote object.
System.Runtime.Remoting.WellKnownClientTypeEntry remoteType =
new System.Runtime.Remoting.WellKnownClientTypeEntry(
typeof(RemoteObject),
"ipc://localhost:9090/RemoteObject.rem");
System.Runtime.Remoting.RemotingConfiguration.
RegisterWellKnownClientType(remoteType);
// Create a message sink.
string objectUri;
System.Runtime.Remoting.Messaging.IMessageSink messageSink =
channel.CreateMessageSink(
"ipc://localhost:9090/RemoteObject.rem", null,
out objectUri);
Console.WriteLine("The URI of the message sink is {0}.",
objectUri);
if (messageSink != null)
{
Console.WriteLine("The type of the message sink is {0}.",
messageSink.GetType().ToString());
}
// Create an instance of the remote object.
RemoteObject service = new RemoteObject();
// Invoke a method on the remote object.
Console.WriteLine("The client is invoking the remote object.");
Console.WriteLine("The remote object has been called {0} times.",
service.GetCount());
}
}
下列程式代碼範例顯示伺服器和用戶端所使用的遠端物件。
using namespace System;
// Remote object.
public ref class RemoteObject: public MarshalByRefObject
{
private:
static int callCount = 0;
public:
int GetCount()
{
Console::WriteLine( L"GetCount has been called." );
callCount++;
return (callCount);
}
};
using System;
// Remote object.
public class RemoteObject : MarshalByRefObject
{
private int callCount = 0;
public int GetCount()
{
Console.WriteLine("GetCount has been called.");
callCount++;
return(callCount);
}
}
備註
重要
使用不信任的資料呼叫此類別的方法,會造成安全性上的風險。 呼叫此類別的方法時,請一律使用信任的資料。 如需詳細資訊,請參閱 驗證所有輸入。
the.NET Framework 遠端基礎結構用來傳輸遠端呼叫的通道。 當用戶端呼叫遠端物件時,呼叫會串行化為用戶端通道所傳送並由伺服器通道接收的訊息。 收到訊息之後,就會還原串行化和處理訊息。 任何傳回的值會由伺服器通道傳輸,並由用戶端通道接收。
類別IpcChannel是結合 類別和 IpcServerChannel 類別功能的IpcClientChannel便利類別。
警告
在 自變數中將 exclusiveAddressUse
屬性設定為 false
時,可以針對相同的命名管道註冊數IpcServerChannel個properties
物件。 在這種情況下,要求可以移至任何已註冊的通道。 只有在也使用 ALC 時,才會將此設定視為安全。
建構函式
IpcChannel() |
初始化 IpcChannel 類別的新執行個體,只啟動用戶端通道,不啟動伺服器通道。 |
IpcChannel(IDictionary, IClientChannelSinkProvider, IServerChannelSinkProvider) |
使用指定的組態屬性和接收,初始化 IpcChannel 類別的新執行個體。 |
IpcChannel(IDictionary, IClientChannelSinkProvider, IServerChannelSinkProvider, CommonSecurityDescriptor) |
使用指定的組態屬性和接收,初始化 IpcChannel 類別的新執行個體。 |
IpcChannel(String) |
使用接聽指定 IPC 連接埠的伺服器通道,初始化 IpcChannel 類別的新執行個體。 |
屬性
ChannelData |
取得通道特定資料。 |
ChannelName |
取得目前通道的名稱。 |
ChannelPriority |
取得目前通道的優先權。 |
IsSecured |
取得或設定指出目前通道是否安全的布林值。 |
方法
CreateMessageSink(String, Object, String) |
傳回通道訊息接收,其傳遞訊息給指定 URL 或通道資料物件。 |
Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
GetHashCode() |
做為預設雜湊函式。 (繼承來源 Object) |
GetType() |
取得目前執行個體的 Type。 (繼承來源 Object) |
GetUrlsForUri(String) |
傳回具有指定 URI 之物件的所有 URL 的陣列,這 URI 裝載 (Host) 於目前 IpcChannel 上。 |
MemberwiseClone() |
建立目前 Object 的淺層複製。 (繼承來源 Object) |
Parse(String, String) |
從指定的 URL 擷取通道 URI 和遠端已知物件 URI。 |
StartListening(Object) |
指示目前通道開始接聽要求。 |
StopListening(Object) |
指示目前通道停止接聽要求。 |
ToString() |
傳回代表目前物件的字串。 (繼承來源 Object) |