共用方式為


RemotingConfiguration.RegisterActivatedServiceType 方法

定義

在服務端註冊 Type 一個物件,作為可由用戶端啟動的物件。

多載

名稱 Description
RegisterActivatedServiceType(ActivatedServiceTypeEntry)

註冊服務端所提供的 ActivatedServiceTypeEntry 物件類型,作為可由用戶端請求時啟用的物件類型。

RegisterActivatedServiceType(Type)

在服務端註冊指定的物件型別,作為可於用戶端請求時啟用的型別。

RegisterActivatedServiceType(ActivatedServiceTypeEntry)

註冊服務端所提供的 ActivatedServiceTypeEntry 物件類型,作為可由用戶端請求時啟用的物件類型。

public:
 static void RegisterActivatedServiceType(System::Runtime::Remoting::ActivatedServiceTypeEntry ^ entry);
public static void RegisterActivatedServiceType(System.Runtime.Remoting.ActivatedServiceTypeEntry entry);
static member RegisterActivatedServiceType : System.Runtime.Remoting.ActivatedServiceTypeEntry -> unit
Public Shared Sub RegisterActivatedServiceType (entry As ActivatedServiceTypeEntry)

參數

entry
ActivatedServiceTypeEntry

用戶端啟用類型的設定。

例外狀況

呼叫堆疊中至少有一個呼叫者沒有權限設定遠端存取類型和通道。

備註

要在伺服器端建立由客戶端啟動的物件實例,你必須知道它 Type ,並且必須透過此 RegisterActivatedServiceType 方法在伺服器端註冊。 要取得用戶端啟用物件新實例的代理,用戶端必須先註冊一個通道, ChannelServices 然後透過呼叫 newActivator.CreateInstance啟動該物件。

要啟用由客戶端啟用的物件類型,並使用 new 關鍵字,您必須先在用戶端使用該 RegisterActivatedClientType 方法註冊該物件類型。 呼叫此 RegisterActivatedClientType 方法會讓遠端基礎設施知道遠端應用程式的位置,並 new 讓使用者嘗試建立該應用程式。 反之,若你使用此 CreateInstance 方法建立客戶端啟用物件的新實例,必須提供遠端應用程式的 URL 作為參數,因此客戶端無需事先註冊。 若要提供 CreateInstance 該方法並提供你想建立物件的伺服器 URL,你必須將該 URL 封裝在該類別的 UrlAttribute 實例中。

另請參閱

適用於

RegisterActivatedServiceType(Type)

在服務端註冊指定的物件型別,作為可於用戶端請求時啟用的型別。

public:
 static void RegisterActivatedServiceType(Type ^ type);
public static void RegisterActivatedServiceType(Type type);
static member RegisterActivatedServiceType : Type -> unit
Public Shared Sub RegisterActivatedServiceType (type As Type)

參數

type
Type

Type該物件需註冊。

例外狀況

呼叫堆疊中至少有一個呼叫者沒有權限設定遠端存取類型和通道。

範例

以下程式碼範例示範在伺服器上註冊物件類型,作為客戶端可啟用的類型。 對於對應於所呈現伺服器程式碼的客戶端程式碼,請參考範例該 RegisterActivatedClientType 方法。

#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()
{
   ChannelServices::RegisterChannel( gcnew TcpChannel( 8082 ) );
   RemotingConfiguration::RegisterActivatedServiceType( HelloServiceClass::typeid );
   Console::WriteLine( "Press enter to stop this process." );
   Console::ReadLine();
   return 0;
}
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

public class ServerClass {

    public static void Main()  {

        ChannelServices.RegisterChannel(new TcpChannel(8082));

        RemotingConfiguration.RegisterActivatedServiceType(typeof(HelloServiceClass));

        Console.WriteLine("Press enter to stop this process.");
        Console.ReadLine();
    }
}
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp


Public Class ServerClass
      
   Public Shared Sub Main()
      
      ChannelServices.RegisterChannel(New TcpChannel(8082))     
      RemotingConfiguration.RegisterActivatedServiceType(GetType(HelloServiceClass))
      
      Console.WriteLine("Press enter to stop this process.")
      Console.ReadLine()

   End Sub

End Class

以下程式碼範例展示了上述範例程式碼中註冊的服務物件。

#using <system.dll>

using namespace System;

public ref class HelloServiceClass: public MarshalByRefObject
{
private:
   static int n_instance;

public:
   HelloServiceClass()
   {
      n_instance++;
      Console::WriteLine(  "{0} has been created.  Instance # = {1}", this->GetType()->Name, n_instance );
   }

   ~HelloServiceClass()
   {
      Console::WriteLine( "Destroyed instance {0} of HelloServiceClass.", n_instance );
      n_instance--;
   }

   String^ HelloMethod( String^ name )
   {
      // Reports that the method was called.
      Console::WriteLine();
      Console::WriteLine( "Called HelloMethod on instance {0} with the '{1}' parameter.", n_instance, name );

      // Calculates and returns the result to the client.
      return String::Format( "Hi there {0}", name );
   }
};
using System;

public class HelloServiceClass : MarshalByRefObject {

    static int n_instance;

    public HelloServiceClass() {
        n_instance++;
        Console.WriteLine(this.GetType().Name + " has been created.  Instance # = {0}", n_instance);
    }

    ~HelloServiceClass() {
        Console.WriteLine("Destroyed instance {0} of HelloServiceClass.", n_instance);
        n_instance --;
    }

    public String HelloMethod(String name) {

        // Reports that the method was called.
        Console.WriteLine();
        Console.WriteLine("Called HelloMethod on instance {0} with the '{1}' parameter.",
                             n_instance, name);

        // Calculates and returns the result to the client.
        return "Hi there " + name + ".";
    }
}
Public Class HelloServiceClass
   Inherits MarshalByRefObject
   
   Private Shared n_instance As Integer

      
   Public Sub New()
      n_instance += 1
      Console.WriteLine(Me.GetType().Name + " has been created.  Instance # = {0}", n_instance)
   End Sub
      
   
   Protected Overrides Sub Finalize()
      Console.WriteLine("Destroyed instance {0} of HelloServiceClass.", n_instance)
      n_instance -= 1
      MyBase.Finalize()
   End Sub
   
   
   
   Public Function HelloMethod(name As [String]) As [String]
      
      ' Reports that the method was called.
      Console.WriteLine()
      Console.WriteLine("Called HelloMethod on instance {0} with the '{1}' parameter.", n_instance, name)
      
      ' Calculates and returns the result to the client.
      Return "Hi there " + name + "."

   End Function 'HelloMethod

End Class

備註

要在伺服器端建立由客戶端啟動的物件實例,你必須知道它 Type ,並且必須透過此 RegisterActivatedServiceType 方法在伺服器端註冊。 要取得用戶端啟用物件新實例的代理,用戶端必須先註冊一個通道, ChannelServices 然後透過呼叫 newActivator.CreateInstance啟動該物件。

要啟用由客戶端啟用的物件類型,並使用 new 關鍵字,您必須先在用戶端使用該 RegisterActivatedClientType 方法註冊該物件類型。 呼叫此 RegisterActivatedClientType 方法會讓遠端基礎設施知道遠端應用程式的位置,並 new 讓使用者嘗試建立該應用程式。 反之,若你使用此 CreateInstance 方法建立客戶端啟用物件的新實例,必須提供遠端應用程式的 URL 作為參數,因此客戶端無需事先註冊。 若要提供 CreateInstance 該方法並提供你想建立物件的伺服器 URL,你必須將該 URL 封裝在該類別的 UrlAttribute 實例中。

另請參閱

適用於