Ejemplo n.º 1
0
        public void Configuration(IAppBuilder app)
        {
            // register assemblies
            AutofacAssemblyStore.LoadAssembly("Brandscreen.Framework");
            AutofacAssemblyStore.LoadAssembly("Brandscreen.Core");
            AutofacAssemblyStore.LoadAssembly(Assembly.GetExecutingAssembly());

            // create and activate host
            _host = HostStarter.CreateHost();
            _host.Activate(); // Note: HostEventsHandler will be called

            // ensure http configuration
            InitializeHttp(_host);

            // setting logger factory so it can do logging via NLog
            app.SetLoggerFactory(_host.LifetimeScope.Resolve <ILoggerFactory>());

            // handling begin and end request events
            app.Use(async(context, next) =>
            {
                _host.BeginRequest(); // begin request

                try
                {
                    await next();
                }
                catch (OperationCanceledException ex)
                {
                    var logger = app.CreateLogger(GetType());
                    logger.WriteWarning(ex.Message);
                }
                catch (Exception ex)
                {
                    var logger = app.CreateLogger(GetType());
                    logger.WriteError("owin error", ex);
                    throw;
                }
                finally
                {
                    _host.EndRequest(); // end request
                }
            });

            // handling application end event
            var properties = new AppProperties(app.Properties);

            properties.OnAppDisposing.Register(() => _host.Terminate()); // end app

            // setup autofac middleware
            app.UseAutofacMiddleware(_host.LifetimeScope); // IOwinContext is registered by autofac when calling this

            // setup
            ConfigureMvc(app, _host);
            ConfigureApi(app, _host);
        }
Ejemplo n.º 2
0
        public void SetUp()
        {
            // init container host for testing, web api will use different one
            AutofacAssemblyStore.LoadAssembly("Brandscreen.Framework");
            AutofacAssemblyStore.LoadAssembly("Brandscreen.Core");
            AutofacAssemblyStore.LoadAssembly("Brandscreen.WebApi");
            Host = HostStarter.CreateHost();
            AutofacAssemblyStore.ClearAssemblies(); // clear loaded assemblies so that it won't affect web api Startup

            // passes the test scope to web app, so it will use to activate RepositoryTestModule
            // the scope will be set again on the starting of each test case in IocSupportedTests
            // the scope is used for resolving same instance of objects as the ones in current running test case for each http request made through the test case
            // e.g. the database context must be the same both in the current running test case and web api current request context,
            // then it is able to rollback all temporary data within the test scope.
            Startup.SetCurrentTestScope(Host.LifetimeScope);

            // init web app
            var ipAddress = LocalIpAddress();

            Url = $"http://{ipAddress?.ToString() ?? "localhost"}:{Port}/";
            Console.WriteLine("starting web app at {0}.", Url);
            _webApp = WebApp.Start <Startup>(Url);
            Console.WriteLine("started web app.");
        }