Beispiel #1
0
        public async Task Actor_UsesCustomActivator()
        {
            var activator = new TestActivator();
            var actorType = typeof(MyActor);

            var options = new ActorRuntimeOptions();

            options.Actors.RegisterActor <MyActor>(options =>
            {
                options.Activator = activator;
            });
            var runtime = new ActorRuntime(options, loggerFactory, activatorFactory, proxyFactory);

            Assert.Contains(actorType.Name, runtime.RegisteredActors.Select(a => a.Type.ActorTypeName), StringComparer.InvariantCulture);

            var output = new MemoryStream();
            await runtime.DispatchWithoutRemotingAsync(actorType.Name, "abc", nameof(MyActor.MyMethod), new MemoryStream(), output);

            var text = Encoding.UTF8.GetString(output.ToArray());

            Assert.Equal("\"hi\"", text);

            await runtime.DeactivateAsync(actorType.Name, "abc");

            Assert.Equal(1, activator.CreateCallCount);
            Assert.Equal(1, activator.DeleteCallCount);
        }
Beispiel #2
0
        public void NoActivateMessageFromRuntime()
        {
            var actorType = typeof(MyActor);

            ActorRuntime.Instance.RegisterActor <MyActor>();

            var output = new MemoryStream();

            ActorRuntime.DispatchWithoutRemotingAsync("MyActor", "abc", "MyMethod", new MemoryStream(), output).GetAwaiter().GetResult();
            string s = Encoding.UTF8.GetString(output.ToArray());

            Assert.Equal("\"hi\"", s);
            Assert.Contains(actorType.Name, ActorRuntime.Instance.RegisteredActorTypes, StringComparer.InvariantCulture);
            Console.WriteLine("done");
        }
        public async Task NoActivateMessageFromRuntime()
        {
            var actorType = typeof(MyActor);

            options.RegisterActor <MyActor>();
            var runtime = new ActorRuntime(options, loggerFactory);

            var output = new MemoryStream();
            await runtime.DispatchWithoutRemotingAsync("MyActor", "abc", "MyMethod", new MemoryStream(), output);

            string s = Encoding.UTF8.GetString(output.ToArray());

            Assert.Equal("\"hi\"", s);
            Assert.Contains(actorType.Name, runtime.RegisteredActorTypes, StringComparer.InvariantCulture);
            Console.WriteLine("done");
        }
Beispiel #4
0
        public async Task NoActivateMessageFromRuntime()
        {
            var actorType = typeof(MyActor);

            var options = new ActorRuntimeOptions();

            options.Actors.RegisterActor <MyActor>();
            var runtime = new ActorRuntime(options, loggerFactory, activatorFactory);

            Assert.Contains(actorType.Name, runtime.RegisteredActors.Select(a => a.Type.ActorTypeName), StringComparer.InvariantCulture);

            var output = new MemoryStream();
            await runtime.DispatchWithoutRemotingAsync(actorType.Name, "abc", nameof(MyActor.MyMethod), new MemoryStream(), output);

            var text = Encoding.UTF8.GetString(output.ToArray());

            Assert.Equal("\"hi\"", text);
            Assert.Contains(actorType.Name, runtime.RegisteredActors.Select(a => a.Type.ActorTypeName), StringComparer.InvariantCulture);
        }
Beispiel #5
0
        public static void AddActorMethodRoute(this IRouteBuilder routeBuilder)
        {
            routeBuilder.MapPut("actors/{actorTypeName}/{actorId}/method/{methodName}", async context =>
            {
                var routeValues   = context.Request.RouteValues;
                var actorTypeName = (string)routeValues["actorTypeName"];
                var actorId       = (string)routeValues["actorId"];
                var methodName    = (string)routeValues["methodName"];

                // If Header is present, call is made using Remoting, use Remoting dispatcher.
                if (context.Request.Headers.ContainsKey(Constants.RequestHeaderName))
                {
                    var daprActorheader = context.Request.Headers[Constants.RequestHeaderName];
                    var(header, body)   = await ActorRuntime.DispatchWithRemotingAsync(actorTypeName, actorId, methodName, daprActorheader, context.Request.Body);

                    // Item 1 is header , Item 2 is body
                    if (header != string.Empty)
                    {
                        // exception case
                        context.Response.Headers.Add(Constants.ErrorResponseHeaderName, header); // add error header
                    }

                    await context.Response.Body.WriteAsync(body, 0, body.Length); // add response message body
                }
                else
                {
                    // write exception info in response.
                    try
                    {
                        await ActorRuntime.DispatchWithoutRemotingAsync(actorTypeName, actorId, methodName, context.Request.Body, context.Response.Body);
                    }
                    catch (Exception e)
                    {
                        context.Response.Headers.Add("Connection: close", default(string));
                        context.Response.ContentType = "application/json";
                        context.Response.StatusCode  = StatusCodes.Status500InternalServerError;
                        await context.Response.WriteAsync(e.ToString());
                        await context.Response.CompleteAsync();
                    }
                }
            });
        }