public void AddBackend(string identifier, InprocClientTransport backend)
        {
            this.Backends.GetOrAdd(identifier, (_) => new BlockingCollection <InprocClientTransport>()).Add(backend);

            backend.ReceiveStream.MessageEnqueued += async(sender, e) =>
            {
                var message = await backend.Receive();

                this.FrontendBuffer.Enqueue(message);
            };
        }
Example #2
0
        public static IApplicationBuilder UseAxon(this IApplicationBuilder app, InprocClientTransport client, Action <AxonKestrelHostingOptions> configure)
        {
            var options = new AxonKestrelHostingOptions();

            configure(options);

            app.UseCors("AllowAll");

            app.Map(options.Route, axonApp =>
            {
                axonApp.MapWhen(context => context.Request.Path.StartsWithSegments("/req") && context.Request.Method == "POST", requestApp =>
                {
                    requestApp.Run(async context =>
                    {
                        if (!context.Request.Query.TryGetValue("tag", out var tag))
                        {
                            throw new Exception("Tag required");
                        }

                        var cancellationSource = new CancellationTokenSource(options.RequestTimeout);

                        byte[] requestData;
                        using (var stream = new MemoryStream())
                        {
                            await context.Request.Body.CopyToAsync(stream);
                            stream.Position = 0;

                            requestData = Convert.FromBase64String(Encoding.UTF8.GetString(stream.ToArray()));
                        }

                        TransportMessage message;
                        using (var stream = new MemoryStream(requestData))
                            using (var reader = new BinaryReader(stream))
                            {
                                message = reader.ReadTransportMessage();
                            }

                        await client.Send(tag, message, cancellationSource.Token);
                        var responseMessage = await client.Receive(tag, cancellationSource.Token);

                        using (var stream = new MemoryStream())
                            using (var writer = new BinaryWriter(stream))
                            {
                                writer.WriteTransportMessage(responseMessage);

                                var responsePayload = Encoding.UTF8.GetBytes(Convert.ToBase64String(stream.ToArray()));
                                context.Response.Body.Write(responsePayload, 0, responsePayload.Length);
                            }
                        //using (var writer = new BinaryWriter(context.Response.Body))
                        //    writer.WriteTransportMessage(responseMessage);
                    });
                });

                axonApp.MapWhen(context => context.Request.Path.StartsWithSegments("/send") && context.Request.Method == "POST", requestApp =>
                {
                    requestApp.Run(async context =>
                    {
                        var cancellationSource = new CancellationTokenSource(options.RequestTimeout);

                        byte[] requestData;
                        using (var stream = new MemoryStream())
                        {
                            await context.Request.Body.CopyToAsync(stream);
                            stream.Position = 0;

                            requestData = Convert.FromBase64String(Encoding.UTF8.GetString(stream.ToArray()));
                        }

                        TransportMessage message;
                        using (var stream = new MemoryStream(requestData))
                            using (var reader = new BinaryReader(stream))
                            {
                                message = reader.ReadTransportMessage();
                            }

                        if (context.Request.Query.TryGetValue("tag", out var tag))
                        {
                            await client.Send(tag, message, cancellationSource.Token);
                        }
                        else
                        {
                            await client.Send(message, cancellationSource.Token);
                        }
                    });
                });

                axonApp.MapWhen(context => context.Request.Path.StartsWithSegments("/receive") && context.Request.Method == "GET", requestApp =>
                {
                    requestApp.Run(async context =>
                    {
                        var cancellationSource = new CancellationTokenSource(options.RequestTimeout);

                        TransportMessage message;
                        if (context.Request.Query.TryGetValue("tag", out var tag))
                        {
                            message = await client.Receive(tag, cancellationSource.Token);
                        }
                        else
                        {
                            message = await client.Receive(cancellationSource.Token);
                        }

                        using (var stream = new MemoryStream())
                            using (var writer = new BinaryWriter(stream))
                            {
                                writer.WriteTransportMessage(message);

                                var responsePayload = Convert.FromBase64String(Convert.ToBase64String(stream.ToArray()));
                                context.Response.Body.Write(responsePayload, 0, responsePayload.Length);
                            }
                        //using (var writer = new BinaryWriter(context.Response.Body))
                        //    writer.WriteTransportMessage(message);
                    });
                });
            });

            return(app);
        }