RemotingConfiguration.RegisterWellKnownServiceType 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将服务端上的对象 Type 注册为已知类型(“单个调用”(singlecall) 或 singleton)。
重载
RegisterWellKnownServiceType(WellKnownServiceTypeEntry) |
将在服务端提供的 Type 中记录的对象 WellKnownServiceTypeEntry 注册为已知类型。 |
RegisterWellKnownServiceType(Type, String, WellKnownObjectMode) |
通过使用给定的参数初始化 Type 的新实例,将服务端上的对象 WellKnownServiceTypeEntry 注册为已知类型。 |
RegisterWellKnownServiceType(WellKnownServiceTypeEntry)
将在服务端提供的 Type 中记录的对象 WellKnownServiceTypeEntry 注册为已知类型。
public:
static void RegisterWellKnownServiceType(System::Runtime::Remoting::WellKnownServiceTypeEntry ^ entry);
public static void RegisterWellKnownServiceType (System.Runtime.Remoting.WellKnownServiceTypeEntry entry);
static member RegisterWellKnownServiceType : System.Runtime.Remoting.WellKnownServiceTypeEntry -> unit
Public Shared Sub RegisterWellKnownServiceType (entry As WellKnownServiceTypeEntry)
参数
已知类型的配置设置。
例外
在调用堆栈上部,至少有一个调用方没有配置远程处理类型和通道的权限。
示例
#using <system.dll>
#using <system.runtime.remoting.dll>
using namespace System;
using namespace System::Runtime::Remoting;
using namespace System::Runtime::Remoting::Channels;
using namespace System::Runtime::Remoting::Channels::Http;
using namespace System::Runtime::Remoting::Messaging;
using namespace System::Runtime::Serialization;
public ref class RemoteObject: public MarshalByRefObject
{
public:
void Method1( LocalObject^ param )
{
Console::WriteLine( "Invoked: Method1( {0})", param );
}
};
int main()
{
ChannelServices::RegisterChannel( gcnew HttpChannel( 8090 ) );
WellKnownServiceTypeEntry^ wkste = gcnew WellKnownServiceTypeEntry( RemoteObject::typeid,"RemoteObject",WellKnownObjectMode::Singleton );
RemotingConfiguration::RegisterWellKnownServiceType( wkste );
RemoteObject^ RObj = dynamic_cast<RemoteObject^>(Activator::GetObject( RemoteObject::typeid, "http://localhost:8090/RemoteObject" ));
LocalObject^ LObj = gcnew LocalObject;
RObj->Method1( LObj );
Console::WriteLine( "Press Return to exit..." );
Console::ReadLine();
}
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Serialization;
using System.Security.Permissions;
public class ObjRefExample {
public static void Main() {
ChannelServices.RegisterChannel(new HttpChannel(8090));
WellKnownServiceTypeEntry wkste =
new WellKnownServiceTypeEntry(typeof(RemoteObject),
"RemoteObject",
WellKnownObjectMode.Singleton);
RemotingConfiguration.RegisterWellKnownServiceType( wkste );
RemoteObject RObj =
(RemoteObject)Activator.GetObject(typeof(RemoteObject),
"http://localhost:8090/RemoteObject");
LocalObject LObj = new LocalObject();
RObj.Method1( LObj );
Console.WriteLine("Press Return to exit...");
Console.ReadLine();
}
}
public class RemoteObject : MarshalByRefObject {
public void Method1(LocalObject param) {
Console.WriteLine("Invoked: Method1({0})", param);
}
}
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Http
Imports System.Runtime.Remoting.Messaging
Imports System.Runtime.Serialization
Imports System.Security.Permissions
' code that drives the example
Public Class ObjRefExample
<PermissionSet(SecurityAction.LinkDemand)> _
Public Overloads Shared Sub Main()
ChannelServices.RegisterChannel(New HttpChannel(8090))
RemotingConfiguration.RegisterWellKnownServiceType(New WellKnownServiceTypeEntry(GetType(RemoteObject), "RemoteObject", WellKnownObjectMode.Singleton))
Dim RObj As RemoteObject = CType(Activator.GetObject(GetType(RemoteObject), "http://localhost:8090/RemoteObject"), RemoteObject)
Dim LObj As New LocalObject()
RObj.Method1(LObj)
Console.WriteLine("Press Return to exit...")
Console.ReadLine()
End Sub
End Class
' a simple remote object
<PermissionSet(SecurityAction.Demand, Name:="FullTrust")> _
Public Class RemoteObject
Inherits MarshalByRefObject
Public Sub Method1(ByVal param As LocalObject)
Console.WriteLine("Invoked: Method1({0})", param)
End Sub
End Class
注解
任何知道已注册已知对象的 URI 的客户端都可以通过使用 注册它喜欢 ChannelServices的通道,并通过调用 new
或 Activator.GetObject 方法激活对象来获取对象的代理。 若要使用 new
激活已知对象,必须先使用 RegisterWellKnownClientType 方法在客户端上注册已知对象类型。
RegisterWellKnownClientType调用 方法会为远程处理基础结构提供远程对象的位置,从而允许 new
关键字创建它。 另一方面,如果使用 Activator.GetObject 方法激活已知对象,则必须为它提供对象的 URL 作为参数,因此无需在客户端上事先注册。
当调用到达服务器时,.NET Framework 会从消息中提取 URI,检查远程处理表以查找与 URI 匹配的 对象的引用,然后根据需要实例化对象,并将方法调用转发到 对象。 如果 对象注册为 SingleCall,则会在方法调用完成后销毁它。 将为每个调用的方法创建 对象的新实例。 和 new
之间的Activator.GetObject唯一区别是,前者允许将 URL 指定为参数,后者从配置中获取 URL。
注册过程不会实例化远程对象本身。 仅当客户端尝试对对象调用方法或从客户端激活对象时,才会发生这种情况。
有关已知对象的详细说明,请参阅 服务器激活。
另请参阅
适用于
RegisterWellKnownServiceType(Type, String, WellKnownObjectMode)
通过使用给定的参数初始化 Type 的新实例,将服务端上的对象 WellKnownServiceTypeEntry 注册为已知类型。
public:
static void RegisterWellKnownServiceType(Type ^ type, System::String ^ objectUri, System::Runtime::Remoting::WellKnownObjectMode mode);
public static void RegisterWellKnownServiceType (Type type, string objectUri, System.Runtime.Remoting.WellKnownObjectMode mode);
static member RegisterWellKnownServiceType : Type * string * System.Runtime.Remoting.WellKnownObjectMode -> unit
Public Shared Sub RegisterWellKnownServiceType (type As Type, objectUri As String, mode As WellKnownObjectMode)
参数
- objectUri
- String
对象 URI。
- mode
- WellKnownObjectMode
正在被注册的已知对象类型的激活方式。 (请参阅WellKnownObjectMode.)
例外
在调用堆栈上部,至少有一个调用方没有配置远程处理类型和通道的权限。
示例
下面的代码示例演示将服务器上的对象类型注册为已知对象类型。 有关与呈现的服务器代码对应的客户端代码,请参阅 方法的示例 RegisterWellKnownClientType 。
#using <system.dll>
#using <system.runtime.remoting.dll>
#using "service.dll"
using namespace System;
using namespace System::Runtime::Remoting;
using namespace System::Runtime::Remoting::Channels;
using namespace System::Runtime::Remoting::Channels::Tcp;
int main()
{
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
public class ServerClass {
public static void Main() {
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
Public Class ServerClass
Public Shared Sub Main()
ChannelServices::RegisterChannel( gcnew TcpChannel( 8082 ) );
RemotingConfiguration::ApplicationName = "HelloServiceApplication";
RemotingConfiguration::RegisterWellKnownServiceType( HelloService::typeid,
"MyUri",
WellKnownObjectMode::SingleCall );
ChannelServices.RegisterChannel(new TcpChannel(8082));
RemotingConfiguration.ApplicationName = "HelloServiceApplication";
RemotingConfiguration.RegisterWellKnownServiceType( typeof(HelloService),
"MyUri",
WellKnownObjectMode.SingleCall
);
ChannelServices.RegisterChannel(New TcpChannel(8082))
RemotingConfiguration.ApplicationName = "HelloServiceApplication"
RemotingConfiguration.RegisterWellKnownServiceType(GetType(HelloService), "MyUri", WellKnownObjectMode.SingleCall)
Console::WriteLine( "Press enter to stop this process." );
Console::ReadLine();
return 0;
}
Console.WriteLine("Press enter to stop this process.");
Console.ReadLine();
}
}
Console.WriteLine("Press enter to stop this process.")
Console.ReadLine()
End Sub
End Class
下面的代码示例演示在上面的示例代码中注册的服务对象。
#using <system.dll>
using namespace System;
public ref class HelloService: public MarshalByRefObject
{
private:
static int n_instances;
public:
HelloService()
{
n_instances++;
Console::WriteLine( "" );
Console::WriteLine( "HelloService activated - instance # {0}.", n_instances );
}
~HelloService()
{
Console::WriteLine( "HelloService instance {0} destroyed.", n_instances );
n_instances--;
}
String^ HelloMethod( String^ name )
{
Console::WriteLine( "HelloMethod called on HelloService instance {0}.", n_instances );
return String::Format( "Hi there {0}.", name );
}
};
using System;
public class HelloService : MarshalByRefObject {
static int n_instances;
public HelloService() {
n_instances++;
Console.WriteLine("");
Console.WriteLine("HelloService activated - instance # {0}.", n_instances);
}
~HelloService() {
Console.WriteLine("HelloService instance {0} destroyed.", n_instances);
n_instances--;
}
public String HelloMethod(String name) {
Console.WriteLine("HelloMethod called on HelloService instance {0}.", n_instances);
return "Hi there " + name + ".";
}
}
Public Class HelloService
Inherits MarshalByRefObject
Private Shared n_instances As Integer
Public Sub New()
n_instances += 1
Console.WriteLine("")
Console.WriteLine("HelloService activated - instance # {0}.", n_instances)
End Sub
Protected Overrides Sub Finalize()
Console.WriteLine("HelloService instance {0} destroyed.", n_instances)
n_instances -= 1
MyBase.Finalize()
End Sub
Public Function HelloMethod(name As [String]) As [String]
Console.WriteLine("HelloMethod called on HelloService instance {0}.", n_instances)
Return "Hi there " + name + "."
End Function 'HelloMethod
End Class
注解
任何知道已注册已知对象的 URI 的客户端都可以通过使用 注册它喜欢 ChannelServices的通道,并通过调用 new
或 Activator.GetObject 方法激活对象来获取对象的代理。 若要使用 new
激活已知对象,必须先使用 RegisterWellKnownClientType 方法在客户端上注册已知对象类型。
RegisterWellKnownClientType调用 方法会为远程处理基础结构提供远程对象的位置,从而允许 new
关键字创建它。 另一方面,如果使用 Activator.GetObject 方法激活已知对象,则必须为它提供对象的 URL 作为参数,因此无需在客户端上事先注册。
当调用到达服务器时,.NET Framework 会从消息中提取 URI,检查远程处理表以查找与 URI 匹配的 对象的引用,然后根据需要实例化对象,并将方法调用转发到 对象。 如果 对象注册为 SingleCall,则会在方法调用完成后销毁它。 将为每个调用的方法创建 对象的新实例。 和 new
之间的Activator.GetObject唯一区别是,前者允许将 URL 指定为参数,后者从配置中获取 URL。
注册过程不会实例化远程对象本身。 仅当客户端尝试对对象调用方法或从客户端激活对象时,才会发生这种情况。
有关已知对象的详细说明,请参阅 服务器激活。