Exemplo n.º 1
0
        private static void AddTestServices(
            IServiceCollection services,
            string applicationWebSiteName,
            string applicationPath,
            Action <IServiceCollection> configureServices)
        {
            applicationPath = applicationPath ?? WebsitesDirectoryPath;

            // Get current IApplicationEnvironment; likely added by the host.
            var provider            = services.BuildServiceProvider();
            var originalEnvironment = provider.GetRequiredService <IApplicationEnvironment>();

            // When an application executes in a regular context, the application base path points to the root
            // directory where the application is located, for example MvcSample.Web. However, when executing
            // an application as part of a test, the ApplicationBasePath of the IApplicationEnvironment points
            // to the root folder of the test project.
            // To compensate for this, we need to calculate the original path and override the application
            // environment value so that components like the view engine work properly in the context of the
            // test.
            var applicationBasePath = CalculateApplicationBasePath(
                originalEnvironment,
                applicationWebSiteName,
                applicationPath);
            var environment = new TestApplicationEnvironment(
                originalEnvironment,
                applicationWebSiteName,
                applicationBasePath);

            services.AddInstance <IApplicationEnvironment>(environment);
            var hostingEnvironment = new HostingEnvironment();

            hostingEnvironment.Initialize(applicationBasePath, config: null);
            services.AddInstance <IHostingEnvironment>(hostingEnvironment);

            // Injecting a custom assembly provider. Overrides AddMvc() because that uses TryAdd().
            var assemblyProvider = CreateAssemblyProvider(applicationWebSiteName);

            services.AddInstance(assemblyProvider);

            // Avoid using pooled memory, we don't have a guarantee that our services will get disposed.
            services.AddInstance <IHttpResponseStreamWriterFactory>(new TestHttpResponseStreamWriterFactory());

            if (configureServices != null)
            {
                configureServices(services);
            }
        }
Exemplo n.º 2
0
        private static void AddTestServices(
            IServiceCollection services,
            string applicationWebSiteName,
            string applicationPath,
            Action<IServiceCollection> configureServices)
        {
            applicationPath = applicationPath ?? WebsitesDirectoryPath;

            // Get current IApplicationEnvironment; likely added by the host.
            var provider = services.BuildServiceProvider();
            var originalEnvironment = provider.GetRequiredService<IApplicationEnvironment>();

            // When an application executes in a regular context, the application base path points to the root
            // directory where the application is located, for example MvcSample.Web. However, when executing
            // an application as part of a test, the ApplicationBasePath of the IApplicationEnvironment points
            // to the root folder of the test project.
            // To compensate for this, we need to calculate the original path and override the application
            // environment value so that components like the view engine work properly in the context of the
            // test.
            var applicationBasePath = CalculateApplicationBasePath(
                originalEnvironment,
                applicationWebSiteName,
                applicationPath);
            var environment = new TestApplicationEnvironment(
                originalEnvironment,
                applicationWebSiteName,
                applicationBasePath);
            services.AddInstance<IApplicationEnvironment>(environment);
            var hostingEnvironment = new HostingEnvironment();
            hostingEnvironment.Initialize(applicationBasePath, config: null);
            services.AddInstance<IHostingEnvironment>(hostingEnvironment);

            // Injecting a custom assembly provider. Overrides AddMvc() because that uses TryAdd().
            var assemblyProvider = CreateAssemblyProvider(applicationWebSiteName);
            services.AddInstance(assemblyProvider);

            // Avoid using pooled memory, we don't have a guarantee that our services will get disposed.
            services.AddInstance<IHttpResponseStreamWriterFactory>(new TestHttpResponseStreamWriterFactory());

            if (configureServices != null)
            {
                configureServices(services);
            }
        }