Ejemplo n.º 1
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();
                    }
                }
            });
        }