Beispiel #1
0
 /// <summary>
 /// Creates a proxy to the actor object that implements an actor interface.
 /// </summary>
 /// <param name="actorId">The actor ID of the proxy actor object. Methods called on this proxy will result in requests
 /// being sent to the actor with this ID.</param>
 /// <param name="actorInterfaceType">
 /// The actor interface type implemented by the remote actor object.
 /// The returned proxy object will implement this interface.
 /// </param>
 /// <param name="actorType">Type of actor implementation.</param>
 /// <param name="options">The optional <see cref="ActorProxyOptions" /> to use when creating the actor proxy.</param>
 /// <returns>Proxy to the actor object.</returns>
 public static object Create(ActorId actorId, Type actorInterfaceType, string actorType, ActorProxyOptions options = null)
 {
     if (!typeof(IActor).IsAssignableFrom(actorInterfaceType))
     {
         throw new ArgumentException("The interface must implement IActor.", nameof(actorInterfaceType));
     }
     return(DefaultProxyFactory.CreateActorProxy(actorId, actorInterfaceType, actorType, options));
 }
        public void DefaultDynamicLazyFieldInterceptorUnWrapsTIEExceptions()
        {
            var pf           = new DefaultProxyFactory();
            var propertyInfo = typeof(MyClass).GetProperty("Id");

            pf.PostInstantiate("MyClass", typeof(MyClass), new HashSet <System.Type>(), propertyInfo.GetGetMethod(), propertyInfo.GetSetMethod(), null);
            var myClassProxied = (MyClass)pf.GetFieldInterceptionProxy(new MyClass());

            Assert.Throws <FormatException>(() => myClassProxied.ThrowError(), "test");
        }
Beispiel #3
0
        public void LazyFieldInterceptorIsBinarySerializable()
        {
            var pf           = new DefaultProxyFactory();
            var propertyInfo = typeof(MyClass).GetProperty("Id");

            pf.PostInstantiate("MyClass", typeof(MyClass), new HashSet <System.Type>(), propertyInfo.GetGetMethod(), propertyInfo.GetSetMethod(), null);
            var fieldInterceptionProxy = (IFieldInterceptorAccessor)pf.GetFieldInterceptionProxy(new MyClass());

            fieldInterceptionProxy.FieldInterceptor = new DefaultFieldInterceptor(null, null, null, "MyClass", typeof(MyClass));

            fieldInterceptionProxy.Should().Be.BinarySerializable();
        }
Beispiel #4
0
 /// <summary>
 /// Creates a proxy to the actor object that implements an actor interface.
 /// </summary>
 /// <typeparam name="TActorInterface">
 /// The actor interface implemented by the remote actor object.
 /// The returned proxy object will implement this interface.
 /// </typeparam>
 /// <param name="actorId">The actor ID of the proxy actor object. Methods called on this proxy will result in requests
 /// being sent to the actor with this ID.</param>
 /// <param name="actorType">Type of actor implementation.</param>
 /// <param name="options">The optional <see cref="ActorProxyOptions" /> to use when creating the actor proxy.</param>
 /// <returns>Proxy to the actor object.</returns>
 public static TActorInterface Create <TActorInterface>(ActorId actorId, string actorType, ActorProxyOptions options = null)
     where TActorInterface : IActor
 {
     return(DefaultProxyFactory.CreateActorProxy <TActorInterface>(actorId, actorType, options));
 }
Beispiel #5
0
 /// <summary>
 /// Creates an Actor Proxy for making calls without Remoting.
 /// </summary>
 /// <param name="actorId">Actor Id.</param>
 /// <param name="actorType">Type of actor.</param>
 /// <param name="options">The optional <see cref="ActorProxyOptions" /> to use when creating the actor proxy.</param>
 /// <returns>Actor proxy to interact with remote actor object.</returns>
 public static ActorProxy Create(ActorId actorId, string actorType, ActorProxyOptions options = null)
 {
     return(DefaultProxyFactory.Create(actorId, actorType, options));
 }
Beispiel #6
0
        private static async Task test(bool hostInstance, params object[] arguments)
        {
            var proxyGenerator = new DefaultProxyFactory();

            using (var cancel = new CancellationTokenSource(Debugger.IsAttached ? TimeSpan.FromDays(1) : TimeSpan.FromMinutes(1)))
                using (var localChannel = new MockChannel())
                    using (var remoteChannel = new MockChannel())
                    {
                        var writeTask = new TaskCompletionSource <(long id, Stream data)>();
                        cancel.Token.Register(() => writeTask.TrySetCanceled());
                        localChannel.MockConnection.Writes  += (id, data) => writeTask.TrySetResult((id, data));
                        remoteChannel.MockConnection.Writes += (id, data) => writeTask.TrySetResult((id, data));

                        //make a local start on another thread
                        var localStart = localChannel.LocalStart(cancel, proxyGenerator,
                                                                 typeof(DummyClass).GetConstructors()
                                                                 .FirstOrDefault(ctor => ctor.GetParameters().Length == arguments.Length) ??
                                                                 typeof(DummyClass).GetConstructors()
                                                                 .First(ctor => ctor.GetParameters().Length != arguments.Length),
                                                                 hostInstance,
                                                                 arguments);
                        if (localStart.IsCompleted)
                        {
                            //this usually means an error happened so lets throw it now
                            await localStart.ConfigureAwait(false);
                        }

                        //wait for local start to start the waiting step for a response
                        await writeTask.Task.ConfigureAwait(false);

                        //read the write request from the local channel
                        var writeRequest = writeTask.Task.Result;
                        Assert.AreEqual(localChannel.Id, writeRequest.id);
                        Assert.IsNotNull(writeRequest.data);
                        writeTask = new TaskCompletionSource <(long id, Stream data)>();

                        //send the request to the remote channel
                        await remoteChannel.Buffer.Fill(writeRequest.data, (int)writeRequest.data.Length, cancel.Token).ConfigureAwait(false);

                        var remoteStart = await remoteChannel.RemoteStart(cancel, proxyGenerator).ConfigureAwait(false);

                        await writeTask.Task.ConfigureAwait(false);

                        //read the write request from the remote channel
                        writeRequest = writeTask.Task.Result;
                        Assert.AreEqual(remoteChannel.Id, writeRequest.id);
                        Assert.IsNotNull(writeRequest.data);

                        //send the request to the local channel
                        await localChannel.Buffer.Fill(writeRequest.data, (int)writeRequest.data.Length, cancel.Token).ConfigureAwait(false);

                        //wait for all start tasks to finish
                        await localStart.ConfigureAwait(false);

                        //validate local instance
                        var localInstance = localStart.Result as DummyClass;
                        Assert.IsNotNull(localInstance);

                        Assert.AreEqual(localInstance.Arg1, arguments.Skip(0).FirstOrDefault());
                        Assert.AreEqual(localInstance.Arg2, arguments.Skip(1).FirstOrDefault());
                        Assert.AreEqual(localInstance.Arg3, arguments.Skip(2).FirstOrDefault());

                        //validate remote instance
                        var remoteInstance = remoteStart.instance as DummyClass;
                        Assert.IsNotNull(remoteInstance);

                        Assert.AreEqual(remoteInstance.Arg1, arguments.Skip(0).FirstOrDefault());
                        Assert.AreEqual(remoteInstance.Arg2, arguments.Skip(1).FirstOrDefault());
                        Assert.AreEqual(remoteInstance.Arg3, arguments.Skip(2).FirstOrDefault());

                        //validate proxy was generated correctly
                        if (hostInstance)
                        {
                            Assert.IsFalse(remoteStart.isHost);

                            Assert.AreEqual(typeof(DummyClass), localInstance.GetType());
                            Assert.AreNotEqual(typeof(DummyClass), remoteInstance.GetType());
                        }
                        else
                        {
                            Assert.IsTrue(remoteStart.isHost);

                            Assert.AreNotEqual(typeof(DummyClass), localInstance.GetType());
                            Assert.AreEqual(typeof(DummyClass), remoteInstance.GetType());
                        }
                    }
        }