RemotingConfiguration.RegisterWellKnownServiceType 方法

定义

将服务端上的对象 Type 注册为已知类型(“单个调用”(singlecall) 或 singleton)。Registers an object Type on the service end as a well-known type (single call or singleton).

重载

RegisterWellKnownServiceType(WellKnownServiceTypeEntry)

将在服务端提供的 Type 中记录的对象 WellKnownServiceTypeEntry 注册为已知类型。Registers an object Type recorded in the provided WellKnownServiceTypeEntry on the service end as a well-known type.

RegisterWellKnownServiceType(Type, String, WellKnownObjectMode)

通过使用给定的参数初始化 Type 的新实例,将服务端上的对象 WellKnownServiceTypeEntry 注册为已知类型。Registers an object Type on the service end as a well-known type, using the given parameters to initialize a new instance of WellKnownServiceTypeEntry.

RegisterWellKnownServiceType(WellKnownServiceTypeEntry)

将在服务端提供的 Type 中记录的对象 WellKnownServiceTypeEntry 注册为已知类型。Registers an object Type recorded in the provided WellKnownServiceTypeEntry on the service end as a well-known type.

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)

参数

entry
WellKnownServiceTypeEntry

已知类型的配置设置。Configuration settings for the well-known type.

异常

在调用堆栈上部,至少有一个调用方没有配置远程处理类型和通道的权限。At least one of the callers higher in the callstack does not have permission to configure remoting types and channels.

示例

#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 {

   [PermissionSet(SecurityAction.LinkDemand)]
   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();
   }
}

[PermissionSet(SecurityAction.Demand, Name="FullTrust")] 
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的通道, 并通过调用newActivator.GetObject方法激活该对象来获取该对象的代理。Any client that knows the URI of a registered well-known object can obtain a proxy for the object by registering the channel it prefers with ChannelServices, and activating the object by calling new or the Activator.GetObject method. 若要使用new激活已知对象, 必须先RegisterWellKnownClientType使用方法在客户端上注册众所周知的对象类型。To activate a well-known object with new, you must first register the well-known object type on the client using the RegisterWellKnownClientType method. 调用方法为远程处理基础结构提供了远程对象的位置, 这new允许关键字创建该对象。 RegisterWellKnownClientTypeCalling the RegisterWellKnownClientType method gives the remoting infrastructure the location of the remote object, which allows the new keyword to create it. 另一方面, 如果使用Activator.GetObject方法来激活已知对象, 则必须将该对象的 URL 作为参数提供, 因此不需要在客户端上进行事先注册。If, on the other hand, you use the Activator.GetObject method to activate the well-known object, you must supply it with the object's URL as an argument, so no prior registration on the client end is necessary.

当调用到达服务器时, .NET Framework 从消息中提取 URI, 检查远程处理表以找到与 URI 匹配的对象的引用, 然后根据需要实例化对象 (如有必要), 将方法调用转发给对象。When the call arrives at the server, the .NET Framework extracts the URI from the message, examines the remoting tables to locate the reference for the object that matches the URI, and then instantiates the object if necessary, forwarding the method call to the object. 如果将对象注册为SingleCall, 则在方法调用完成后将其销毁。If the object is registered as SingleCall, it is destroyed after the method call is completed. 将为每个调用的方法创建对象的新实例。A new instance of the object is created for each method called. Activator.GetObjectnew之间唯一的区别在于前者允许您指定一个 url 作为参数, 后者从配置中获取 url。The only difference between Activator.GetObject and new is that the former allows you to specify a URL as a parameter, and the latter obtains the URL from the configuration.

注册过程不会实例化远程对象本身。The remote object itself is not instantiated by the registration process. 仅当客户端尝试调用对象上的方法或从客户端激活对象时, 才会发生这种情况。This only happens when a client attempts to call a method on the object or activates the object from the client side.

有关已知对象的详细说明, 请参阅服务器激活For a detailed description of well-known objects, see Server Activation.

安全性

SecurityPermission
用于配置远程处理基础结构。for configuration of the remoting infrastructure. 要求值: Demand;权限值:RemotingConfigurationDemand value: Demand; Permission value: RemotingConfiguration

另请参阅

RegisterWellKnownServiceType(Type, String, WellKnownObjectMode)

通过使用给定的参数初始化 Type 的新实例,将服务端上的对象 WellKnownServiceTypeEntry 注册为已知类型。Registers an object Type on the service end as a well-known type, using the given parameters to initialize a new instance of 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

参数

type
Type

对象 TypeThe object Type.

objectUri
String

对象 URI。The object URI.

mode
WellKnownObjectMode

正在被注册的已知对象类型的激活方式。The activation mode of the well-known object type being registered. (参见 WellKnownObjectMode。)(See WellKnownObjectMode.)

异常

在调用堆栈上部,至少有一个调用方没有配置远程处理类型和通道的权限。At least one of the callers higher in the callstack does not have permission to configure remoting types and channels.

示例

下面的代码示例演示如何在服务器上将对象类型注册为众所周知的对象类型。The following code example demonstrates registration of an object type on the server as a well-known object type. 有关对应于所提供的服务器代码的客户端代码, 请参阅RegisterWellKnownClientType方法的示例。For the client code that corresponds to the presented server code, see the example for the RegisterWellKnownClientType method.

#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

下面的代码示例演示了在上面的示例代码中注册的服务对象。The following code example shows the service object registered in the sample code above.

#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的通道, 并通过调用newActivator.GetObject方法激活该对象来获取该对象的代理。Any client that knows the URI of a registered well-known object can obtain a proxy for the object by registering the channel it prefers with ChannelServices, and activating the object by calling new or the Activator.GetObject method. 若要使用new激活已知对象, 必须先RegisterWellKnownClientType使用方法在客户端上注册众所周知的对象类型。To activate a well-known object with new, you must first register the well-known object type on the client using the RegisterWellKnownClientType method. 调用方法为远程处理基础结构提供了远程对象的位置, 这new允许关键字创建该对象。 RegisterWellKnownClientTypeCalling the RegisterWellKnownClientType method gives the remoting infrastructure the location of the remote object, which allows the new keyword to create it. 另一方面, 如果使用Activator.GetObject方法来激活已知对象, 则必须将该对象的 URL 作为参数提供, 因此不需要在客户端上进行事先注册。If, on the other hand, you use the Activator.GetObject method to activate the well-known object, you must supply it with the object's URL as an argument, so no prior registration on the client end is necessary.

当调用到达服务器时, .NET Framework 从消息中提取 URI, 检查远程处理表以找到与 URI 匹配的对象的引用, 然后根据需要实例化对象 (如有必要), 将方法调用转发给对象。When the call arrives at the server, the .NET Framework extracts the URI from the message, examines the remoting tables to locate the reference for the object that matches the URI, and then instantiates the object if necessary, forwarding the method call to the object. 如果将对象注册为SingleCall, 则在方法调用完成后将其销毁。If the object is registered as SingleCall, it is destroyed after the method call is completed. 将为每个调用的方法创建对象的新实例。A new instance of the object is created for each method called. Activator.GetObjectnew之间唯一的区别在于前者允许您指定一个 url 作为参数, 后者从配置中获取 url。The only difference between Activator.GetObject and new is that the former allows you to specify a URL as a parameter, and the latter obtains the URL from the configuration.

注册过程不会实例化远程对象本身。The remote object itself is not instantiated by the registration process. 仅当客户端尝试调用对象上的方法或从客户端激活对象时, 才会发生这种情况。This only happens when a client attempts to call a method on the object or activates the object from the client side.

有关已知对象的详细说明, 请参阅服务器激活For a detailed description of well-known objects, see Server Activation.

安全性

SecurityPermission
用于配置远程处理基础结构。for configuration of the remoting infrastructure. 要求值: Demand;权限值:RemotingConfigurationDemand value: Demand; Permission value: RemotingConfiguration

另请参阅

适用于