public void PipelineBuilder_BuildFromConfiguration_AssemblyServices_MultiAvailable()
        {
            var element = new ElementOptions()
            {
                BuilderName = "RequiredService"
            };

            // Create configuration object.
            PipelineOptions opts = new PipelineOptions();

            opts.Elements = new List <ElementOptions>
            {
                element
            };

            var service       = new RequiredServiceElementBuilder.EmptyService();
            var httpClient    = new Mock <HttpClient>();
            var updateService = new DataUpdateService(
                new Mock <ILogger <DataUpdateService> >().Object,
                httpClient.Object);
            var services = new FiftyOneServiceProvider();

            services.AddService(service);
            services.AddService(updateService);

            // Pass the configuration to the builder to create the pipeline.
            var pipeline = new PipelineBuilder(_loggerFactory, services)
                           .BuildFromConfiguration(opts);

            Assert.IsNotNull(pipeline.GetElement <RequiredServiceElement>().LoggerFactory);
            Assert.IsNotNull(pipeline.GetElement <RequiredServiceElement>().Service);
            Assert.AreEqual(service, pipeline.GetElement <RequiredServiceElement>().Service);
            Assert.IsNotNull(pipeline.GetElement <RequiredServiceElement>().UpdateService);
            Assert.AreEqual(updateService, pipeline.GetElement <RequiredServiceElement>().UpdateService);
        }
예제 #2
0
        /// <summary>
        /// Private constructor used to construct the only instance which will
        /// exist of this class.
        /// </summary>
        private WebPipeline()
        {
            IConfiguration config = new ConfigurationBuilder()
                .AddPipelineConfig()
                .Build();
            _options = new PipelineWebIntegrationOptions();
            config.Bind("PipelineOptions", _options);
            
            if (_options == null ||
                _options.Elements == null)
            {
                throw new PipelineConfigurationException(
                   Messages.ExceptionNoConfiguration);
            }

            // Add the sequence element.
            var sequenceConfig = _options.Elements.Where(e =>
                e.BuilderName.IndexOf(nameof(SequenceElement),
                    StringComparison.OrdinalIgnoreCase) >= 0);
            if (sequenceConfig.Any() == false)
            {
                // The sequence element is not included so add it.
                // Make sure it's added as the first element.
                _options.Elements.Insert(0, new ElementOptions()
                {
                    BuilderName = nameof(SequenceElement)
                });
            }

            if (ClientSideEvidenceEnabled)
            {
                // Client-side evidence is enabled so make sure the 
                // JsonBuilderElement and JavaScriptBundlerElement has been 
                // included.
                var jsonConfig = _options.Elements.Where(e =>
                    e.BuilderName.StartsWith(nameof(JsonBuilderElement),
                        StringComparison.OrdinalIgnoreCase));
                var javascriptConfig = _options.Elements.Where(e =>
                    e.BuilderName.StartsWith(nameof(JavaScriptBuilderElement),
                        StringComparison.OrdinalIgnoreCase));

                var jsIndex = javascriptConfig.Any() ?
                    _options.Elements.IndexOf(javascriptConfig.First()) : -1;

                if (jsonConfig.Any() == false)
                {
                    // The json builder is not included so add it.
                    var newElementOptions = new ElementOptions()
                    {
                        BuilderName = nameof(JsonBuilderElement)
                    };
                    if (jsIndex > -1)
                    {
                        // There is already a javascript builder element
                        // so insert the json builder before it.
                        _options.Elements.Insert(jsIndex, newElementOptions);
                    }
                    else
                    {
                        _options.Elements.Add(newElementOptions);
                    }
                }

                if (jsIndex == -1)
                {
                    // The javascript builder is not included so add it.
                    _options.Elements.Add(new ElementOptions()
                    {
                        BuilderName = nameof(JavaScriptBuilderElement),
                        BuildParameters = new Dictionary<string, object>()
                        {
                            { "EndPoint", "/51dpipeline/json" }
                        }
                    });
                }
                else
                {
                    // There is already a JavaScript builder config so check if 
                    // the endpoint is specified. If not, add it.
                    if (javascriptConfig.Single().BuildParameters.ContainsKey("EndPoint") == false)
                    {
                        javascriptConfig.Single().BuildParameters.Add("EndPoint", "/51dpipeline/json");
                    }
                }
            }

            // Add the set headers
            var setHeadersConfig = _options.Elements.Where(e =>
                e.BuilderName.IndexOf(nameof(SetHeadersElement),
                    StringComparison.OrdinalIgnoreCase) >= 0);
            if (setHeadersConfig.Any() == false)
            {
                // The set headers element is not included, so add it.
                // Make sure it's added as the last element.
                _options.Elements.Add(new ElementOptions()
                {
                    BuilderName = nameof(SetHeadersElement)
                });
            }

            // Set up common services.
            var loggerFactory = new LoggerFactory();
            var updateService = new DataUpdateService(
                loggerFactory.CreateLogger<DataUpdateService>(),
                new System.Net.Http.HttpClient());
            var services = new FiftyOneServiceProvider();
            // Add data update and missing property services.
            services.AddService(updateService);
            services.AddService(MissingPropertyService.Instance);

            Pipeline = new PipelineBuilder(
                loggerFactory,
                services)
                .BuildFromConfiguration(_options);
        }