共用方式為


RemotingConfiguration.RegisterWellKnownServiceType 方法

定義

在服務端註冊 Type 物件為已知類型(單一呼叫或單例)。

多載

名稱 Description
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,則在方法呼叫完成後即被銷毀。 每個被呼叫的方法都會建立一個新的物件實例。 和 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的通道,並呼叫 newActivator.GetObject 方法來啟動該物件,從而取得該物件的代理。 要啟用一個已知物件, new必須先在用戶端使用該 RegisterWellKnownClientType 方法註冊該已知物件類型。 呼叫該 RegisterWellKnownClientType 方法會讓遠端基礎設施知道遠端物件的位置,進而讓 new 關鍵字建立該物件。 反之,若你使用 Activator.GetObject 該方法來啟用已知物件,必須以物件的 URL 作為參數,因此用戶端無需事先註冊。

當呼叫抵達伺服器時,.NET Framework 會從訊息中擷取 URI,檢查遠端資料表以找到與 URI 相符的物件參考,然後必要時實例化該物件,並將方法呼叫轉發給物件。 若物件註冊為 SingleCall,則在方法呼叫完成後即被銷毀。 每個被呼叫的方法都會建立一個新的物件實例。 和 new 之間的唯一差別Activator.GetObject是,前者允許你指定 URL 作為參數,後者則是從設定中取得 URL。

遠端物件本身並未被註冊過程實例化。 這只會發生在用戶端嘗試呼叫物件的方法或從用戶端啟動物件時。

另請參閱

適用於