Created
November 22, 2010 08:26
-
-
Save noqisofon/709675 to your computer and use it in GitHub Desktop.
MSDN の RemotingConfiguration.RegisterWellKnownServiceType メソッド の使用例改変。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Security.Permissions; | |
using System.Runtime.Remoting; | |
using System.Runtime.Remoting.Channels; | |
using System.Runtime.Remoting.Channels.Http; | |
using System.Runtime.Remoting.Messaging; | |
using System.Runtime.Serialization; | |
[assembly: SecurityPermission( SecurityAction.RequestMinimum, Execution = true )] | |
namespace demo.remoting.objref { | |
/// <summary> | |
/// | |
/// </summary> | |
class EntryPoint { | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="args"></param> | |
[PermissionSet( SecurityAction.LinkDemand )] | |
public void run(string[] args) { | |
int port = 8090; | |
string obj_uri = "RemoteObject"; | |
try { | |
ChannelServices.RegisterChannel( new HttpChannel( port ), true ); | |
WellKnownServiceTypeEntry service_type_entry = new WellKnownServiceTypeEntry( typeof( RemoteObject ), | |
obj_uri, | |
WellKnownObjectMode.Singleton | |
); | |
RemotingConfiguration.RegisterWellKnownServiceType( service_type_entry ); | |
RemoteObject remote_obj = (RemoteObject)Activator.GetObject( typeof( RemoteObject ), | |
string.Format( "http://localhost:{0}/{1}", | |
port, | |
obj_uri | |
) | |
); | |
LocalObject local_obj = new LocalObject(); | |
remote_obj.doSomething( local_obj ); | |
} catch ( RemotingException re ) { | |
Console.WriteLine( re.Message ); | |
Console.WriteLine( re.StackTrace ); | |
} | |
Console.Write( "press return to exit..." ); | |
Console.ReadLine(); | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="args"></param> | |
static void Main(string[] args) { | |
EntryPoint progn = new EntryPoint(); | |
progn.run( args ); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.Remoting; | |
using System.Security.Permissions; | |
namespace demo.remoting.objref { | |
/// <summary> | |
/// a class that uses MyObjRef | |
/// </summary> | |
[PermissionSet( SecurityAction.Demand, Name = "FullTrust" )] | |
public class LocalObject : MarshalByRefObject { | |
/// <summary> | |
/// overriding CreateObjRef will allow us to return a custom ObjRef | |
/// </summary> | |
/// <param name="t"></param> | |
/// <returns></returns> | |
public override ObjRef CreateObjRef(Type t) { | |
return new MyObjRef( this, t ); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.Remoting; | |
using System.Runtime.Remoting.Channels; | |
using System.Runtime.Remoting.Messaging; | |
using System.Runtime.Serialization; | |
using System.Security.Permissions; | |
namespace demo.remoting.objref { | |
/// <summary> | |
/// a custom ObjRef class that outputs its status | |
/// </summary> | |
[Serializable] | |
[PermissionSet( SecurityAction.Demand, Name = "FullTrust" )] | |
public class MyObjRef : ObjRef { | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="mbr_object"></param> | |
/// <param name="type"></param> | |
public MyObjRef(MarshalByRefObject mbr_object, Type type) | |
: base( mbr_object, type ) { | |
Console.WriteLine( "Created MyObjRef." ); | |
objectReferenceDump(); | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="info"></param> | |
/// <param name="context"></param> | |
public MyObjRef(SerializationInfo info, StreamingContext context) | |
: base( info, context ) { | |
Console.WriteLine( "Deserialized MyObjRef." ); | |
} | |
/// <summary> | |
/// only instantiate using marshaling or deserialization | |
/// </summary> | |
private MyObjRef() { } | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="info"></param> | |
/// <param name="context"></param> | |
public override void GetObjectData(SerializationInfo info, StreamingContext context) { | |
// After calling the base method, change the type from ObjRef to MyObjRef | |
base.GetObjectData( info, context ); | |
info.SetType( GetType() ); | |
Console.WriteLine( "Serialized MyObjRef." ); | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="context"></param> | |
/// <returns></returns> | |
public override Object GetRealObject(StreamingContext context) { | |
if ( IsFromThisAppDomain() || IsFromThisProcess() ) { | |
Console.WriteLine( "Returning actual object referenced by MyObjRef." ); | |
return base.GetRealObject( context ); | |
} else { | |
Console.WriteLine( "Returning proxy to remote object." ); | |
return RemotingServices.Unmarshal( this ); | |
} | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
public void objectReferenceDump() { | |
Console.WriteLine( " --- Reporting MyObjRef Info --- " ); | |
Console.WriteLine( "Reference to {0}.", base.TypeInfo.TypeName ); | |
Console.WriteLine( "URI is {0}.", base.URI ); | |
Console.WriteLine( "\nWriting EnvoyInfo: " ); | |
if ( base.EnvoyInfo != null ) { | |
IMessageSink envoy_sinks = EnvoyInfo.EnvoySinks; | |
while ( envoy_sinks != null ) { | |
Console.WriteLine( "\tSink: " + envoy_sinks.ToString() ); | |
envoy_sinks = envoy_sinks.NextSink; | |
} | |
} else | |
Console.WriteLine( "\t {no sinks}" ); | |
Console.WriteLine( "\nWriting ChannelInfo: " ); | |
for ( int i = 0; i < ChannelInfo.ChannelData.Length; i++ ) | |
Console.WriteLine( "\tChannel: {0}", ChannelInfo.ChannelData[i] ); | |
Console.WriteLine( " ----------------------------- " ); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Security.Permissions; | |
namespace demo.remoting.objref { | |
/// <summary> | |
/// | |
/// </summary> | |
[PermissionSet( SecurityAction.Demand, Name = "FullTrust" )] | |
public class RemoteObject : MarshalByRefObject { | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="param"></param> | |
public void doSomething(LocalObject param) { | |
Console.WriteLine( "Invoked: doSomething({0})", param ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment