远程处理示例:动态发布
对于服务器激活的可远程处理的类型,.NET 远程处理仅支持默认构造函数。要在使用某个特定构造函数创建对象后发布该对象并且完全控制该特定实例的发布,可以通过编程方式发布该实例。
警告
.NET 远程处理在默认情况下不进行身份验证或加密。因此,建议您在与客户端或服务器进行远程交互之前,采取任何必要的措施以确认它们的身份。因为 .NET 远程处理应用程序需要 FullTrust 权限才能执行,所以,未经授权的客户端如果被授予访问您服务器的权限,该客户端就可以像完全受信任的客户端一样执行代码。应始终验证终结点的身份并将通信流加密,通过在 Internet 信息服务 (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 方法