.NET 遠端處理只支援具有伺服器啟動可遠端處理型別的預設建構函式。如果您希望發行集件,在利用某些特定建構函式建立好物件,並能夠完全控制特定執行個體的發行集之後,您就可以程式設計的方式來發行執行個體。
**注意 **.NET 遠端處理依預設不會執行驗證或加密。因此,建議您最好採取所有必要步驟,在與用戶端或伺服器遠端互動前,先確定其識別。由於 .NET 遠端處理應用程式需要 FullTrust 使用權限才能執行,因此如果讓未授權用戶端存取您的伺服器,該用戶端便可以執行程式碼,如同它完全受信任一般。永遠驗證您的端點並加密通訊資料流,方法是在 Internet Information Services (IIS) 中裝載遠端型別,或是建置自訂通道接收配對來完成這個工作。
編譯和執行這個範例
在命令提示字元中輸入下列命令:
vbc -t:library remote.vb
vbc -r:System.Runtime.Remoting.dll -r:remote.dll server.vb
vbc -r:System.Runtime.Remoting.dll -r:remote.dll client.vb
開啟指向相同目錄的兩個命令提示。在其中一個中,輸入 Server。在另一個中,輸入 Client。
若要停止正發行的可遠端處理物件,請在伺服器命令提示中按下 ENTER,並返回用戶端以觀察針對不同階段所擲回的不同例外狀況。這個應用程式可以在單台電腦或網路上執行。如果您希望在網路上執行這個應用程式,必須將用戶端組態中的**「localhost」**取代成遠端電腦的名稱。
remote.vb
Imports System
Public Class ServiceClass
Inherits MarshalByRefObject
Private m_starttime As DateTime
Public Sub New()
Console.WriteLine("ServiceClass created without constructor. Instance hash is " & Me.GetHashCode().ToString())
m_starttime = DateTime.Now
End Sub
Overrides Protected Sub Finalize()
Console.WriteLine("I'm being collected after " & (New TimeSpan(DateTime.Now.Ticks - m_starttime.Ticks)).ToString() & " seconds.")
MyBase.Finalize()
End Sub
Public Function GetServerTime() As DateTime
Console.WriteLine("Time requested by a client.")
Return DateTime.Now
End Function
Public ReadOnly Property InstanceHash() As Integer
Get
Return Me.GetHashCode()
End Get
End Property
End Class
Server.vb
Imports System
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Http
Public Class ServerProcess
<MTAThread()> _
Public Shared Sub Main()
Dim channel As New HttpChannel(8080)
ChannelServices.RegisterChannel(channel)
Dim object1 As New ServiceClass()
' Creates the single instance of ServiceClass. All clients
' will use this instance.
Dim ref1 As ObjRef = RemotingServices.Marshal(object1, "object1uri")
Console.WriteLine("ObjRef.URI: " & ref1.URI)
Console.WriteLine("Running. Press Enter to end publication.")
Console.ReadLine()
' This unregisters the object from publication, but leaves
' the channel listening.
RemotingServices.Disconnect(object1)
Console.WriteLine()
Console.WriteLine("Disconnected the object. Client now receives a RemotingException.")
Console.WriteLine("Press Enter to unregister the channel.")
Console.ReadLine()
' At this point, the ServerClass object still exists. The server
' could republish it.
' This unregisters the channel, but leaves the application
' domain running.
ChannelServices.UnregisterChannel(channel)
Console.WriteLine("Unregistered the channel. Client now receives a WebException.")
' The ServerClass object still exists. The server could
' reregister the channel and republish the object.
Console.WriteLine("The host application domain is still running. Press Enter to stop the process.")
Console.ReadLine()
' The ServiceClass object's Finalize method writes a message to
' the console. A single object will almost always succeed in
' running its Finalize method before the Console is finalized;
' in a larger application, you could ensure that all objects
' finalize before the application ends by calling the garbage
' collector and waiting.
GC.Collect()
GC.WaitForPendingFinalizers()
End Sub
End Class
Client.vb
Imports System
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
Imports System.Runtime.Remoting.Channels.Http
Public Class ClientProcess
<MTAThread()> _
Public Shared Sub Main()
Dim channel As New HttpChannel(0)
ChannelServices.RegisterChannel(channel)
' Registers the remote class. (This could be done with a
' configuration file instead of a direct call.)
RemotingConfiguration.RegisterWellKnownClientType(Type.GetType("ServiceClass, remote"), "https://localhost:8080/object1uri")
' Instead of creating a new object, this obtains a reference
' to the server's single instance of the ServiceClass object.
Dim object1 As ServiceClass = New ServiceClass()
Try
Console.WriteLine("ServerTime: " & object1.GetServerTime())
Catch ex As Exception
Console.WriteLine("Exception of type: " & ex.GetType.ToString & " occurred.")
Console.WriteLine("Details: " & ex.Message)
End Try
End Sub ' Main
End Class ' ClientProcess
請參閱
遠端處理範例 | RemotingServices.Marshal 方法 | RemotingServices.Disconnect 方法 | ChannelServices.UnregisterChannel 方法