遠端處理範例:動態發行
.NET 遠端處理只支援具有伺服器啟動之可遠端處理型別的預設建構函式。若要在使用某個特定建構函式建立物件之後發行該物件,並對該特定執行個體的發行具有完整控制,則可用程式設計方式發行您的執行個體。
警告
.NET 遠端處理預設不會執行驗證或加密。因此,建議在與用戶端或伺服器進行遠端互動之前,先採取所有必要步驟以驗證這些用戶端或伺服器的識別。因為 .NET 遠端處理應用程式需要 FullTrust 權限才能執行,所以如果將伺服器的存取權授與未授權用戶端,則該用戶端可如受到完全信任般地執行程式碼。請一律驗證您的端點並加密通訊資料流,方法是在網際網路資訊服務 (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。
若要在階段中停止發行可遠端處理物件,請在 server 命令提示字元中按 ENTER 鍵,並重新執行 client 以觀察在不同階段擲回的不同例外狀況。這個應用程式可在單一電腦上或跨網路執行。若要透過網路執行這個應用程式,則必須將用戶端組態中的 "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"), "http://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 方法