RemotingConfiguration.RegisterWellKnownServiceType 方法

定義

在服務端上登錄物件 Type 為已知型別 (單一呼叫或單一物件)。

多載

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)

參數

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用的通道來取得物件的 Proxy,並藉由呼叫 newActivator.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)

參數

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用的通道來取得物件的 Proxy,並藉由呼叫 newActivator.GetObject 方法來啟用物件。 若要使用 new啟用已知的物件,您必須先使用 RegisterWellKnownClientType 方法在用戶端上註冊已知的物件類型。 RegisterWellKnownClientType呼叫 方法會提供遠端基礎結構遠程物件的位置,以允許new關鍵詞加以建立。 如果另一方面,您可以使用 Activator.GetObject 方法來啟動已知的物件,您必須提供物件的URL做為自變數,因此不需要在用戶端端進行先前的註冊。

當呼叫抵達伺服器時,.NET Framework 會從訊息擷取 URI、檢查遠端數據表以找出符合 URI 之對象的參考,然後視需要具現化物件,並將方法呼叫轉送至物件。 如果對象註冊為 SingleCall,則會在方法呼叫完成之後終結。 系統會為每個呼叫的方法建立 物件的新實例。 和 new 之間的Activator.GetObject唯一差異在於前者可讓您將URL指定為參數,而後者則會從組態取得URL。

遠端物件本身不會由註冊程式具現化。 只有在用戶端嘗試在 物件上呼叫 方法,或從客戶端啟動 物件時,才會發生這種情況。

如需已知對象的詳細說明,請參閱 伺服器啟用

另請參閱

適用於