Exemple #1
0
        /// <summary>
        /// Starts running a new RestBus host.
        /// </summary>
        /// <param name="app">The Application builder</param>
        /// <param name="subscriber">The RestBus subscriber</param>
        /// <param name="skipRestBusServerCheck">Set to true to run the host even if the application server is the RestBus.AspNet server</param>
        public static void RunRestBusHost(this IApplicationBuilder app, IRestBusSubscriber subscriber, bool skipRestBusServerCheck)
        {
            if (app == null) throw new ArgumentNullException("app");
            if (subscriber == null) throw new ArgumentNullException("subscriber");

            if (!skipRestBusServerCheck && 
                app.ApplicationServices.GetRequiredService<IHostingEnvironment>().Configuration[Server.Server.ConfigServerArgumentName] == Server.Server.ConfigServerAssembly)
            {
                //The application is running RestBusServer, so exit
                return;
            }

            var appFunc = app.Build();

            var _loggerFactory = app.ApplicationServices.GetRequiredService<ILoggerFactory>();
            var diagnosticSource = app.ApplicationServices.GetRequiredService<DiagnosticSource>();
            var httpContextFactory = app.ApplicationServices.GetRequiredService<IHttpContextFactory>();

            //TODO: Work on counting instances (all hosts + server)  and adding the count to the logger name e.g RestBus.AspNet (2), consider including the typename as well.
            var application = new HostingApplication(appFunc, _loggerFactory.CreateLogger(Server.Server.ConfigServerAssembly), diagnosticSource, httpContextFactory);

            var host = new RestBusHost<HostingApplication.Context>(subscriber, application);

            //Register host for disposal
            var appLifeTime = app.ApplicationServices.GetRequiredService<IApplicationLifetime>();

            appLifeTime.ApplicationStopping.Register(() => host.Dispose()); //TODO: Make ApplicationStopping event stop dequeueing items (StopPollingQueue)
            appLifeTime.ApplicationStopped.Register(() => host.Dispose());

            //Start host
            host.Start();
        }
Exemple #2
0
        /// <summary>
        /// Configures the RestBus server to use a specified subscriber
        /// </summary>
        /// <param name="app">The Application builder</param>
        /// <param name="subscriber">The RestBus subscriber</param>
        public static void ConfigureRestBusServer( this IApplicationBuilder app, IRestBusSubscriber subscriber )
        {
            if (app == null) throw new ArgumentNullException("app");
            if (subscriber == null) throw new ArgumentNullException("subscriber");

            var feature = app.ServerFeatures.Get<IServerInformation>();
            if (feature == null) return; //Application isn't running RestBus server so return

            var serverInfo = feature as ServerInformation;
            if(serverInfo != null)
            {
                serverInfo.Subscriber = subscriber;
            }
        }
Exemple #3
0
        /// <summary>
        /// Starts running a new RestBus host.
        /// </summary>
        /// <param name="app">The Application builder</param>
        /// <param name="subscriber">The RestBus subscriber</param>
        /// <param name="skipRestBusServerCheck">Set to true to run the host even if the application server is the RestBus.AspNet server</param>
        public static void RunRestBusHost(this IApplicationBuilder app, IRestBusSubscriber subscriber, bool skipRestBusServerCheck)
        {
            if (app == null)
            {
                throw new ArgumentNullException("app");
            }
            if (subscriber == null)
            {
                throw new ArgumentNullException("subscriber");
            }

            if (!skipRestBusServerCheck &&
                app.ApplicationServices.GetRequiredService <IHostingEnvironment>().Configuration[Server.Server.ConfigServerArgumentName] == Server.Server.ConfigServerAssembly)
            {
                //The application is running RestBusServer, so exit
                return;
            }

            var appFunc = app.Build();

            var _loggerFactory     = app.ApplicationServices.GetRequiredService <ILoggerFactory>();
            var diagnosticSource   = app.ApplicationServices.GetRequiredService <DiagnosticSource>();
            var httpContextFactory = app.ApplicationServices.GetRequiredService <IHttpContextFactory>();

            //TODO: Work on counting instances (all hosts + server)  and adding the count to the logger name e.g RestBus.AspNet (2), consider including the typename as well.
            var application = new HostingApplication(appFunc, _loggerFactory.CreateLogger(Server.Server.ConfigServerAssembly), diagnosticSource, httpContextFactory);

            var host = new RestBusHost <HostingApplication.Context>(subscriber, application);

            //Register host for disposal
            var appLifeTime = app.ApplicationServices.GetRequiredService <IApplicationLifetime>();

            appLifeTime.ApplicationStopping.Register(() => host.Dispose()); //TODO: Make ApplicationStopping event stop dequeueing items (StopPollingQueue)
            appLifeTime.ApplicationStopped.Register(() => host.Dispose());

            //Start host
            host.Start();
        }
Exemple #4
0
        /// <summary>
        /// Configures the RestBus server to use a specified subscriber
        /// </summary>
        /// <param name="app">The Application builder</param>
        /// <param name="subscriber">The RestBus subscriber</param>
        public static void ConfigureRestBusServer(this IApplicationBuilder app, IRestBusSubscriber subscriber)
        {
            if (app == null)
            {
                throw new ArgumentNullException("app");
            }
            if (subscriber == null)
            {
                throw new ArgumentNullException("subscriber");
            }

            var feature = app.ServerFeatures.Get <IServerInformation>();

            if (feature == null)
            {
                return;                  //Application isn't running RestBus server so return
            }
            var serverInfo = feature as ServerInformation;

            if (serverInfo != null)
            {
                serverInfo.Subscriber = subscriber;
            }
        }
Exemple #5
0
 public RestBusHost(IRestBusSubscriber subscriber, HttpConfiguration config)
 {
     this.subscriber = subscriber;
     this.config = config;
     this.requestHandler = new RequestHandler(config);
 }
Exemple #6
0
 public RestBusHost(IRestBusSubscriber subscriber, HttpConfiguration config)
 {
     this.subscriber     = subscriber;
     this.config         = config;
     this.requestHandler = new RequestHandler(config);
 }
Exemple #7
0
        private static HttpResponsePacket CreateResponsePacketFromMessage(HttpResponseMessage responseMsg, IRestBusSubscriber subscriber)
        {
            var responsePkt = responseMsg.ToHttpResponsePacket();

            //Add/Update Server header
            responsePkt.Headers["Server"] = HTTP_RESPONSE_SERVER_HEADER;

            return(responsePkt);
        }
Exemple #8
0
        //TODO: See if it's possible to prevent middleware from being added after RunRestBusHost() is called, subsequent calls to RunRestBusHost must succeed.

        /// <summary>
        /// Starts running a new RestBus host.
        /// </summary>
        /// <param name="app">The Application builder</param>
        /// <param name="subscriber">The RestBus subscriber</param>
        /// <remarks>The RestBus host will not be started if the application is running a RestBus server.</remarks>
        public static void RunRestBusHost(this IApplicationBuilder app, IRestBusSubscriber subscriber)
        {
            RunRestBusHost(app, subscriber, false);
        }
Exemple #9
0
        private static HttpResponsePacket CreateResponsePacketFromMessage(HttpResponseMessage responseMsg, IRestBusSubscriber subscriber)
        {
            var responsePkt = responseMsg.ToHttpResponsePacket();

            //Add/Update Server header
            responsePkt.Headers["Server"] = HTTP_RESPONSE_SERVER_HEADER;

            return responsePkt;
        }
Exemple #10
0
        private static HttpResponsePacket CreateResponsePacketFromWrapper(ResponseWrapper wrapper, IRestBusSubscriber subscriber)
        {
            HttpResponsePacket response = new HttpResponsePacket();

            string trimmedKey;

            foreach (string key in wrapper.Headers.AllKeys)
            {
                foreach (string value in wrapper.Headers.GetValues(key))
                {
                    trimmedKey = key.Trim();
                    if (trimmedKey != String.Empty)
                    {
                        if (response.Headers.ContainsKey(trimmedKey))
                        {
                            ((List <string>)response.Headers[trimmedKey]).Add(value);
                        }
                        else
                        {
                            response.Headers.Add(trimmedKey, new List <string> {
                                value
                            });
                        }
                    }
                }
            }

            //TODO: Investigate if servicestack V3 produces a Server header, if so add it here and in CreateResponseFromException

            response.Content           = (wrapper.OutputStream as System.IO.MemoryStream).ToArray();
            response.StatusCode        = wrapper.StatusCode;
            response.StatusDescription = wrapper.StatusDescription;
            response.Version           = HTTP_RESPONSE_VERSION;

            return(response);
        }
Exemple #11
0
        //TODO: See if it's possible to prevent middleware from being added after RunRestBusHost() is called, subsequent calls to RunRestBusHost must succeed.

        /// <summary>
        /// Starts running a new RestBus host.
        /// </summary>
        /// <param name="app">The Application builder</param>
        /// <param name="subscriber">The RestBus subscriber</param>
        /// <remarks>The RestBus host will not be started if the application is running a RestBus server.</remarks>
        public static void RunRestBusHost(this IApplicationBuilder app, IRestBusSubscriber subscriber)
        {
            RunRestBusHost(app, subscriber, false);
        }
Exemple #12
0
        private static HttpResponsePacket CreateResponsePacketFromMessage(HttpResponseMessage responseMsg, IRestBusSubscriber subscriber)
        {
            //TODO: Confirm that commas in response headers are merged properly into packet header
            //It seems like header folding behavior in ToHttpResponsePacket() is wrong
            //AddHeader("One, Two")
            //AddHeader(["Three", "Four"])
            //Should not return 
            //"One, Two"
            //"Three, Four" in the output
            //It should be:
            //"One, Two"
            //"Three"
            //"Four"
            var responsePkt = responseMsg.ToHttpResponsePacket();

            //Add/Update Subscriber-Id header
            responsePkt.Headers[Common.Shared.SUBSCRIBER_ID_HEADER] = new string[] { subscriber == null ? String.Empty : subscriber.Id ?? String.Empty };

            return responsePkt;
        }
Exemple #13
0
 public RestBusHost(IRestBusSubscriber subscriber)
 {
     this.subscriber = subscriber;
 }
Exemple #14
0
        private static HttpResponsePacket CreateResponsePacketFromWrapper(ResponseWrapper wrapper, IRestBusSubscriber subscriber)
        {
            HttpResponsePacket response = new HttpResponsePacket();

            //TODO: Note that when implementing this in WebAPI/MVC the "responsewrapper" will most likely split headers into groups seperated by commas

            string trimmedKey;

            foreach (string key in wrapper.Headers.AllKeys)
            {
                foreach (string value in wrapper.Headers.GetValues(key))
                {
                    trimmedKey = key.Trim();
                    if (trimmedKey != String.Empty)
                    {
                        if (response.Headers.ContainsKey(trimmedKey))
                        {
                            ((List <string>)response.Headers[trimmedKey]).Add(value);
                        }
                        else
                        {
                            response.Headers.Add(trimmedKey, new List <string> {
                                value
                            });
                        }
                    }
                }
            }

            //Add/Update Subscriber-Id header
            response.Headers[Common.Shared.SUBSCRIBER_ID_HEADER] = new string[] { subscriber == null ? String.Empty : subscriber.Id ?? String.Empty };

            response.Content           = (wrapper.OutputStream as System.IO.MemoryStream).ToArray();
            response.StatusCode        = wrapper.StatusCode;
            response.StatusDescription = wrapper.StatusDescription;
            response.Version           = HTTP_RESPONSE_VERSION;

            return(response);
        }
Exemple #15
0
        private HttpResponsePacket CreateResponsePacketFromMessage(HttpResponseMessage responseMsg, IRestBusSubscriber subscriber)
        {
            //TODO: Confirm that commas in response headers are merged iproperly into packet header
            var responsePkt = new HttpResponsePacket(responseMsg);

            //Add/Update Subscriber-Id header
            responsePkt.Headers[Common.Shared.SUBSCRIBER_ID_HEADER] = new string[] { subscriber == null ? String.Empty : subscriber.Id ?? String.Empty };

            return(responsePkt);
        }
Exemple #16
0
        private HttpResponsePacket CreateResponsePacketFromMessage(HttpResponseMessage responseMsg, IRestBusSubscriber subscriber)
        {
            //TODO: Confirm that commas in response headers are merged iproperly into packet header
            var responsePkt = new HttpResponsePacket(responseMsg);

            //Add/Update Subscriber-Id header
            responsePkt.Headers[Common.Shared.SUBSCRIBER_ID_HEADER] = new string[] { subscriber == null ? String.Empty : subscriber.Id ?? String.Empty };

            return responsePkt;
        }
Exemple #17
0
 public RestBusHost(IRestBusSubscriber subscriber, IApplicationBuilder appBuilder)
 {
     this.appFunc = appBuilder.Build();
     this.subscriber = subscriber;
     //this.requestHandler = new RequestHandler(config);
 }
Exemple #18
0
 /// <summary>
 /// Initializes a new instance of <see cref="RestBusHost{TContext}"/>
 /// </summary>
 /// <param name="subscriber">The RestBus Subscriber</param>
 /// <param name="application">The HttpApplication</param>
 internal RestBusHost(IRestBusSubscriber subscriber, IHttpApplication <TContext> application)
 {
     this.subscriber  = subscriber;
     this.application = application;
 }