RemotingConfiguration.RegisterWellKnownServiceType 方法

定义

在服务端将对象 Type 注册为已知类型(单个调用或单一实例)。

重载

名称 说明
RegisterWellKnownServiceType(WellKnownServiceTypeEntry)

将服务端上提供的WellKnownServiceTypeEntry记录的对象Type注册为已知类型。

RegisterWellKnownServiceType(Type, String, WellKnownObjectMode)

Type使用给定的参数将服务端的对象注册为已知类型,以初始化新实例WellKnownServiceTypeEntry

RegisterWellKnownServiceType(WellKnownServiceTypeEntry)

将服务端上提供的WellKnownServiceTypeEntry记录的对象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

已知类型的配置设置。

例外

调用堆栈中至少有一个更高的调用方无权配置远程处理类型和通道。

示例

#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的通道,并通过调用 newActivator.GetObject 方法激活对象来获取对象的代理。 若要使用 new 激活已知对象,必须先使用 RegisterWellKnownClientType 该方法在客户端上注册已知对象类型。 RegisterWellKnownClientType调用该方法可让远程处理基础结构定位远程对象,从而允许new关键字创建远程对象。 另一方面, Activator.GetObject 如果使用该方法激活已知对象,则必须使用对象的 URL 作为参数提供该对象,因此不需要在客户端端进行事先注册。

当调用到达服务器时,.NET Framework 从消息中提取 URI,检查远程处理表以查找与 URI 匹配的对象的引用,然后在必要时实例化对象,并将方法调用转发到对象。 如果对象注册为 SingleCall,则会在方法调用完成后销毁该对象。 将为调用的每个方法创建对象的新实例。 前者唯一的区别Activator.GetObjectnew在于,前者允许将 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)

参数

type
Type

Type对象 。

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的通道,并通过调用 newActivator.GetObject 方法激活对象来获取对象的代理。 若要使用 new 激活已知对象,必须先使用 RegisterWellKnownClientType 该方法在客户端上注册已知对象类型。 RegisterWellKnownClientType调用该方法可让远程处理基础结构定位远程对象,从而允许new关键字创建远程对象。 另一方面, Activator.GetObject 如果使用该方法激活已知对象,则必须使用对象的 URL 作为参数提供该对象,因此不需要在客户端端进行事先注册。

当调用到达服务器时,.NET Framework 从消息中提取 URI,检查远程处理表以查找与 URI 匹配的对象的引用,然后在必要时实例化对象,并将方法调用转发到对象。 如果对象注册为 SingleCall,则会在方法调用完成后销毁该对象。 将为调用的每个方法创建对象的新实例。 前者唯一的区别Activator.GetObjectnew在于,前者允许将 URL 指定为参数,后者从配置中获取 URL。

远程对象本身不会由注册过程实例化。 仅当客户端尝试对对象调用方法或从客户端激活对象时,才会发生这种情况。

另请参阅

适用于