Exemple #1
0
        public void AddServiceCollection_WithExistingServiceCollectionAndWithNullServiceCollection_ExceptionThrown()
        {
            Action act = () =>
            {
                HttpApplicationExtensions.AddServiceCollection(new HttpApplication(), null);
            };

            act.Should().Throw <ArgumentNullException>()
            .And.ParamName.Should().Be("serviceCollection");
        }
Exemple #2
0
        public void AddServiceCollection_WithExistingCollection()
        {
            // Creates a HttpContext
            var application = new HttpApplicationMock();
            var request     = new HttpRequest("The filename", "http://theurl", "");
            var response    = new HttpResponse(new StringWriter());
            var session     = FormatterServices.GetUninitializedObject(typeof(HttpSessionState));

            HttpContext.Current = new HttpContext(request, response)
            {
                ApplicationInstance = application,
                Items =
                {
                    { "AspSession", session }
                }
            };

            try
            {
                var collection = new ServiceCollection();
                collection.AddSingleton <IService, Service>();
                collection.AddSingleton <IDependentService, DependentService>();

                var returnedCollection = HttpApplicationExtensions.AddServiceCollection(application, collection);

                returnedCollection.Should().BeSameAs(collection);

                // Checks the WebObjectActivator has been defined correctly
                HttpRuntime.WebObjectActivator.Should().BeOfType <ServiceProviderAdapter>();

                // Checks the services
                HttpRuntime.WebObjectActivator.GetService(typeof(IService)).As <Service>().Should().NotBeNull();
                HttpRuntime.WebObjectActivator.GetService(typeof(HttpApplication)).Should().BeSameAs(application);
                HttpRuntime.WebObjectActivator.GetService(typeof(HttpApplicationMock)).Should().BeSameAs(application);
                HttpRuntime.WebObjectActivator.GetService(typeof(HttpRequest)).Should().BeSameAs(request);
                HttpRuntime.WebObjectActivator.GetService(typeof(HttpResponse)).Should().BeSameAs(response);
                HttpRuntime.WebObjectActivator.GetService(typeof(HttpSessionState)).Should().BeSameAs(session);

                // Checks the next provider in the adapter has been defined correctly
                HttpRuntime.WebObjectActivator.GetFieldValue <IServiceProvider>("nextProvider").Should().BeSameAs(this.existingProvider);

                // Checks the adapter has been registered on the hosting environment infrastructure
                typeof(HostingEnvironment).GetStaticValue <object>("_theHostingEnvironment").GetFieldValue <Hashtable>("_registeredObjects").ContainsKey(HttpRuntime.WebObjectActivator);
                typeof(HostingEnvironment).GetStaticValue <object>("_theHostingEnvironment").GetFieldValue <Hashtable>("_registeredObjects").ContainsValue(HttpRuntime.WebObjectActivator);
            }
            finally
            {
                HttpContext.Current = null;
            }
        }