public static StartupServer Create(Uri baseAddress = null) { var appEnv = CallContextServiceLocator.Locator.ServiceProvider.GetRequiredService<IApplicationEnvironment>(); var config = new ConfigurationBuilder(); var configDict = new Dictionary<string, string>(); configDict["Hosting:Environment"] = "Test"; config.AddInMemoryCollection(configDict.AsEnumerable()); var conf = config.Build(); var env = new HostingEnvironment(); env.Initialize($"{appEnv.ApplicationBasePath}/../../src/Accounting", conf); var startup = new Accounting.Startup(appEnv, env); var setup = new SetupObject(); Action<IApplicationBuilder> buildApp = (app) => { app.Use(async (context, next) => { var req = context.Request; if (req.Path == testSetupPathString && setup.Setup != null) { await setup.Setup(context); } else { await next(); } }); startup.Configure(app); }; Action<IServiceCollection> buildServices = (services) => { startup.ConfigureServices(services); services.Remove(services.Where(sp => sp.ServiceType == typeof(IConfigureOptions<AntiforgeryOptions>)).First()); services.TryAddEnumerable(ServiceDescriptor.Transient<IConfigureOptions<AntiforgeryOptions>, TestAntiforgeryOptionsSetup>()); services.AddSingleton<IAntiforgeryTokenSerializer, TestAntiforgeryTokenSerializer>(); services.AddSingleton<IAntiforgeryTokenGenerator, TestAntiforgeryTokenGenerator>(); }; StartupServer server = new StartupServer(CreateBuilder(conf, app => buildApp(app), services => buildServices(services))); server.setupObject = setup; server.BaseAddress = baseAddress; return server; }
protected TestServer CreateServer() { EnsurePath(Path.Combine(TestProjectsPath, TemplateName, "wwwroot")); // Get current IApplicationEnvironment; likely added by the host. var provider = CallContextServiceLocator.Locator.ServiceProvider; 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, TemplateName, TestProjectsPath); var environment = new TestApplicationEnvironment( originalEnvironment, applicationBasePath, TemplateName); var hostingEnvironment = new HostingEnvironment(); hostingEnvironment.Initialize(applicationBasePath, environmentName: null); try { CallContextServiceLocator.Locator.ServiceProvider = new WrappingServiceProvider(provider, environment, hostingEnvironment); var assemblyProvider = CreateAssemblyProvider(TemplateName); var builder = TestServer.CreateBuilder() .UseStartup(TemplateName) .UseServices(services => { services.AddInstance<IHostingEnvironment>(hostingEnvironment); services.AddInstance(assemblyProvider); }); return new TestServer(builder); } finally { CallContextServiceLocator.Locator.ServiceProvider = provider; } }
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); } }
public void Services_can_be_configured() { //var appBaseDir = AppDomain.CurrentDomain.BaseDirectory; //Console.WriteLine($"AppDomain.BaseDirectory {appBaseDir}"); var dir = Directory.GetCurrentDirectory(); Console.WriteLine($"Directory.GetCurrentDirectory {dir}"); IHostingEnvironment env = new HostingEnvironment(); Startup startup = new Startup(env); var cfg = startup.Configuration; env.Initialize(dir, cfg); var cs = cfg.GetSection("ConnectionStrings:FineWork"); Assert.NotNull(cs); ServiceCollection services = new ServiceCollection(); startup.ConfigureServices(services); var sp = services.BuildServiceProvider(); var sessionProvider = sp.GetRequiredService<ISessionProvider<AefSession>>(); Assert.NotNull(sessionProvider); }
public static Action<IServiceCollection> InitializeServices(Type startup, Action<IServiceCollection> next) { var applicationServices = CallContextServiceLocator.Locator.ServiceProvider; var libraryManager = applicationServices.GetRequiredService<ILibraryManager>(); var applicationName = startup.GetTypeInfo().Assembly.GetName().Name; var library = libraryManager.GetLibrary(applicationName); var applicationRoot = Path.GetDirectoryName(library.Path); var applicationEnvironment = applicationServices.GetRequiredService<IApplicationEnvironment>(); var configureServices = startup.GetMethod("ConfigureServices"); return (services) => { services.AddInstance<IApplicationEnvironment>(new TestApplicationEnvironment(applicationEnvironment, applicationName, applicationRoot)); var hostingEnvironment = new HostingEnvironment(); hostingEnvironment.Initialize(applicationRoot, "Production"); services.AddInstance<IHostingEnvironment>(new HostingEnvironment()); next(services); }; }
private Func<IServiceCollection, IServiceProvider> InitializeServices( Assembly startupAssembly, Func<IServiceCollection, IServiceProvider> buildServices) { var applicationServices = CallContextServiceLocator.Locator.ServiceProvider; var libraryManager = applicationServices.GetRequiredService<ILibraryManager>(); // When an application executes in a regular context, the application base path points to the root // directory where the application is located, for example .../samples/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, we need to calculate the correct project path and override the application // environment value so that components like the view engine work properly in the context of the test. var applicationName = startupAssembly.GetName().Name; var library = libraryManager.GetLibrary(applicationName); var applicationRoot = Path.GetDirectoryName(library.Path); var applicationEnvironment = applicationServices.GetRequiredService<IApplicationEnvironment>(); return (services) => { services.AddInstance<IApplicationEnvironment>( new TestApplicationEnvironment(applicationEnvironment, applicationName, applicationRoot)); var hostingEnvironment = new HostingEnvironment(); hostingEnvironment.Initialize(applicationRoot, null); services.AddInstance<IHostingEnvironment>(hostingEnvironment); // Inject a custom assembly provider. Overrides AddMvc() because that uses TryAdd(). var assemblyProvider = new StaticAssemblyProvider(); assemblyProvider.CandidateAssemblies.Add(startupAssembly); services.AddInstance<IAssemblyProvider>(assemblyProvider); AddAdditionalServices(services); return buildServices(services); }; }
public Func<IServiceCollection, IServiceProvider> InitializeServices(Assembly startupAssembly, Func<IServiceCollection, IServiceProvider> buildServices) { var applicationName = startupAssembly.GetName().Name; var applicationRoot = GetApplicationRoot(applicationName); #if DNX451 AppDomain.CurrentDomain.SetData("APP_CONTEXT_BASE_DIRECTORY", applicationRoot); #endif var applicationEnvironment = PlatformServices.Default.Application; var hostingEnvironment = new HostingEnvironment(); hostingEnvironment.Initialize(applicationRoot, null); var assemblyProvider = new StaticAssemblyProvider(); assemblyProvider.CandidateAssemblies.Add(startupAssembly); return services => { services.AddInstance<IApplicationEnvironment>(new TestApplicationEnvironment(applicationEnvironment, applicationName, applicationRoot)); services.AddInstance<IHostingEnvironment>(hostingEnvironment); // Inject a custom assembly provider. Overrides AddMvc() because that uses TryAdd(). services.AddInstance<IAssemblyProvider>(assemblyProvider); return buildServices(services); }; }