/// <summary>
        /// Method to deal with adding a second core into Microsoft's dependency injection system.
        /// </summary>
        /// <param name="services">The dependency injection service.</param>
        /// <param name="url">The url for the second core.</param>
        /// <param name="setupAction">Allow for custom headers to be injected.</param>
        /// <typeparam name="TModel">The type of model that should be used for this core.</typeparam>
        /// <returns>The dependency injection service.</returns>
        public static IServiceCollection AddSolrNet <TModel>(this IServiceCollection services, string url, Action <SolrNetOptions> setupAction)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            if (AddedDI <TModel>(services))
            {
                throw new InvalidOperationException($"SolrNet was already addd for model of type {typeof(TModel).Name}");
            }

            services = BuildSolrNet(services, url, setupAction);
            //Set custom http client setting given by user at intiliation for specific solr core
            var autoSolrConnection = new AutoSolrConnection(url);

            if (setupAction != null)
            {
                var solrOption = new SolrNetOptions(autoSolrConnection.HttpClient);
                setupAction(solrOption);
            }
            var connection = new BasicInjectionConnection <TModel>(autoSolrConnection);

            services.AddTransient(typeof(ISolrInjectedConnection <TModel>), (service) => connection);
            return(services);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Method is mean to add in the basic solr net DI to deal with multiple cores.
        /// </summary>
        /// <param name="services">The dependency injection service.</param>
        /// <param name="url">The url to be built from.</param>
        /// <param name="setupAction">The setup action that should be used for injection purposes.</param>
        /// <returns></returns>
        private static IServiceCollection BuildSolrNet(IServiceCollection services, string url, Action <SolrNetOptions> setupAction)
        {
            if (AddedGeneralDI(services))
            {
                return(services);
            }

            services.AddSingleton <IReadOnlyMappingManager, AttributesMappingManager>();
            services.AddTransient <ISolrDocumentPropertyVisitor, DefaultDocumentVisitor>();
            services.AddTransient <ISolrFieldParser, DefaultFieldParser>();
            services.AddTransient(typeof(ISolrDocumentActivator <>), typeof(SolrDocumentActivator <>));
            services.AddTransient(typeof(ISolrDocumentResponseParser <>), typeof(SolrDocumentResponseParser <>));

            services.AddTransient <ISolrDocumentResponseParser <Dictionary <string, object> >, SolrDictionaryDocumentResponseParser>();
            services.AddTransient <ISolrFieldSerializer, DefaultFieldSerializer>();
            services.AddTransient <ISolrQuerySerializer, DefaultQuerySerializer>();
            services.AddTransient <ISolrFacetQuerySerializer, DefaultFacetQuerySerializer>();
            services.AddTransient(typeof(ISolrAbstractResponseParser <>), typeof(DefaultResponseParser <>));
            services.AddTransient <ISolrHeaderResponseParser, HeaderResponseParser <string> >();
            services.AddTransient <ISolrExtractResponseParser, ExtractResponseParser>();
            foreach (var p in new[] {
                typeof(MappedPropertiesIsInSolrSchemaRule),
                typeof(RequiredFieldsAreMappedRule),
                typeof(UniqueKeyMatchesMappingRule),
                typeof(MultivaluedMappedToCollectionRule),
            })
            {
                services.AddTransient(typeof(IValidationRule), p);
            }
            services.AddTransient(typeof(ISolrMoreLikeThisHandlerQueryResultsParser <>), typeof(SolrMoreLikeThisHandlerQueryResultsParser <>));
            services.AddTransient(typeof(ISolrDocumentSerializer <>), typeof(SolrDocumentSerializer <>));
            services.AddTransient <ISolrDocumentSerializer <Dictionary <string, object> >, SolrDictionarySerializer>();

            services.AddTransient <ISolrSchemaParser, SolrSchemaParser>();
            services.AddTransient <ISolrDIHStatusParser, SolrDIHStatusParser>();
            services.AddTransient <IMappingValidator, MappingValidator>();

            var connection = new AutoSolrConnection(url);

            //Bind single type to a single url, prevent breaking existing functionality
            services.AddSingleton <ISolrConnection>(connection);

            services.AddTransient(typeof(ISolrInjectedConnection <>), typeof(BasicInjectionConnection <>));
            services.AddTransient(typeof(ISolrQueryExecuter <>), typeof(SolrInjectionQueryExecuter <>));
            services.AddTransient(typeof(ISolrBasicOperations <>), typeof(SolrInjectionBasicServer <>));
            services.AddTransient(typeof(ISolrBasicReadOnlyOperations <>), typeof(SolrInjectionBasicServer <>));
            services.AddScoped(typeof(ISolrOperations <>), typeof(SolrInjectionServer <>));
            services.AddTransient(typeof(ISolrReadOnlyOperations <>), typeof(SolrInjectionServer <>));


            if (setupAction != null)
            {
                var options = new SolrNetOptions(connection.HttpClient);
                //allow for custom headers to be injected.
                setupAction(options);
            }

            return(services);
        }
        public async Task GetAsyncWithCancelledToken()
        {
            var p = new Dictionary <string, string>();

            p["q"]    = "*";
            p["rows"] = "1";
            var conn        = new AutoSolrConnection(solrURL);
            var tokenSource = new CancellationTokenSource(1);

            await Assert.ThrowsAsync <TaskCanceledException>(() => conn.GetAsStreamAsync("/select/", p, tokenSource.Token));
        }
Exemplo n.º 4
0
        public void SimpleGet()
        {
            var p = new Dictionary <string, string>();

            p["q"]    = "*";
            p["rows"] = "1";
            var conn     = new AutoSolrConnection(solrURL);
            var response = conn.Get("/select/", p);
            var xdoc     = XDocument.Parse(response);

            Assert.Equal("0", xdoc.Root.Element("lst").Elements("int").First(el => (string)el.Attribute("name") == "status").Value);
        }
Exemplo n.º 5
0
        public async Task GetAsyncAutoPost()
        {
            var p = new Dictionary <string, string>();

            p["q"]    = "*";
            p["rows"] = "1";
            p["test"] = string.Join("", Enumerable.Range(0, 9000).Select(a => Guid.NewGuid().ToString()));
            var conn     = new AutoSolrConnection(solrURL);
            var response = await conn.GetAsync("/select/", p);

            var xdoc = XDocument.Parse(response);

            Assert.Equal("0", xdoc.Root.Element("lst").Elements("int").First(el => (string)el.Attribute("name") == "status").Value);
        }
        public async Task SimpleGetAsync()
        {
            var p = new Dictionary <string, string>();

            p["q"]    = "*";
            p["rows"] = "1";
            var conn     = new AutoSolrConnection(solrURL);
            var response = await conn.GetAsync("/select/", p);

            var xdoc = XDocument.Parse(response);

            Assert.Equal("0", xdoc.Root.Element("lst").Elements("int").First(el => (string)el.Attribute("name") == "status").Value);
            Assert.True(int.Parse((string)xdoc.Root.Element("result").Attribute("numFound")) > 1);
            Assert.Single(xdoc.Root.Element("result").Elements("doc"));
        }
Exemplo n.º 7
0
        public async Task GetAsyncAutoPostWithStream()
        {
            //No mocking yet of HTTPClient, so checked with Fiddler if indeed POST-ed
            var p = new Dictionary <string, string>();

            p["q"]    = "*";
            p["rows"] = "1";
            p["test"] = string.Join("", Enumerable.Range(0, 9000).Select(a => Guid.NewGuid().ToString()));
            var       conn = new AutoSolrConnection(solrURL);
            XDocument xdoc;

            using (var response = await conn.GetAsStreamAsync("/select/", p, CancellationToken.None))
            {
                xdoc = XDocument.Load(response);
            }
            Assert.Equal("0", xdoc.Root.Element("lst").Elements("int").First(el => (string)el.Attribute("name") == "status").Value);
        }
        public async Task GetStreamAsyncAutoPost()
        {
            var p = new Dictionary <string, string>();

            p["q"]    = "*";
            p["rows"] = "1";
            p["test"] = string.Join("", Enumerable.Range(0, 9000).Select(a => "a"));
            var       conn = new AutoSolrConnection(solrURL);
            XDocument xdoc;

            using (var response = await conn.GetAsStreamAsync("/select/", p, default(CancellationToken)))
                xdoc = XDocument.Load(response);

            Assert.Equal("0", xdoc.Root.Element("lst").Elements("int").First(el => (string)el.Attribute("name") == "status").Value);
            Assert.True(int.Parse((string)xdoc.Root.Element("result").Attribute("numFound")) > 1);
            Assert.Single(xdoc.Root.Element("result").Elements("doc"));
        }
        private static ISolrConnection CreateAutoSolrConnection(IServiceProvider serviceProvider,
                                                                Func <IServiceProvider, string> urlRetriever, Action <SolrNetOptions> setupAction)
        {
            var solrUrl = urlRetriever(serviceProvider);

            if (string.IsNullOrWhiteSpace(solrUrl))
            {
                throw new ArgumentNullException(nameof(solrUrl));
            }

            var connection = new AutoSolrConnection(solrUrl);

            if (setupAction == null)
            {
                return(connection);
            }

            // Allow for custom headers to be injected.
            var options = new SolrNetOptions(connection.HttpClient);

            setupAction(options);
            return(connection);
        }
Exemplo n.º 10
0
        private static Container AddSolrNet(this Container container, IEnumerable <SolrCore> cores, Action <SolrNetOptions> setupAction)
        {
            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }

            container.Register <IReadOnlyMappingManager>(() => new MemoizingMappingManager(new AttributesMappingManager()), Lifestyle.Singleton);
            container.Register <ISolrDocumentPropertyVisitor, DefaultDocumentVisitor>();
            container.Register <ISolrFieldParser, DefaultFieldParser>();
            container.Register(typeof(ISolrDocumentActivator <>), typeof(SolrDocumentActivator <>));
            container.RegisterConditional(typeof(ISolrDocumentResponseParser <>), typeof(SolrDocumentResponseParser <>), c => !c.Handled);

            container.Register <ISolrDocumentResponseParser <Dictionary <string, object> >, SolrDictionaryDocumentResponseParser>();
            container.Register <ISolrFieldSerializer, DefaultFieldSerializer>();
            container.Register <ISolrQuerySerializer, DefaultQuerySerializer>();
            container.Register <ISolrFacetQuerySerializer, DefaultFacetQuerySerializer>();
            container.Register(typeof(ISolrAbstractResponseParser <>), typeof(DefaultResponseParser <>));
            container.Collection.Register(typeof(ISolrAbstractResponseParser <>), new[] { typeof(DefaultResponseParser <>) });
            container.Register <ISolrHeaderResponseParser, HeaderResponseParser <string> >();
            container.Register <ISolrExtractResponseParser, ExtractResponseParser>();
            var p = new[] {
                typeof(MappedPropertiesIsInSolrSchemaRule),
                typeof(RequiredFieldsAreMappedRule),
                typeof(UniqueKeyMatchesMappingRule),
                typeof(MultivaluedMappedToCollectionRule),
            };

            container.Collection.Register(typeof(IValidationRule), p);
            container.Register(typeof(ISolrMoreLikeThisHandlerQueryResultsParser <>), typeof(SolrMoreLikeThisHandlerQueryResultsParser <>));
            container.RegisterConditional(typeof(ISolrDocumentSerializer <>), typeof(SolrDocumentSerializer <>), c => !c.Handled);
            container.Register <ISolrDocumentSerializer <Dictionary <string, object> >, SolrDictionarySerializer>();

            container.Register <ISolrSchemaParser, SolrSchemaParser>();
            container.Register <ISolrDIHStatusParser, SolrDIHStatusParser>();
            container.Register <IMappingValidator, MappingValidator>();

            if (!cores.Any())
            {
                return(container);
            }

            if (cores.Count() > 1)
            {
                throw new NotImplementedException("Need to add multicore support");
            }

            var connection = new AutoSolrConnection(cores.Single().Url);

            //Bind single type to a single url, prevent breaking existing functionality
            container.Register <ISolrConnection>(() => connection, Lifestyle.Singleton);
            container.Register(typeof(ISolrQueryExecuter <>), typeof(SolrQueryExecuter <>));
            container.Register(typeof(ISolrBasicOperations <>), typeof(SolrBasicServer <>));
            container.Register(typeof(ISolrBasicReadOnlyOperations <>), typeof(SolrBasicServer <>));
            container.Register(typeof(ISolrOperations <>), typeof(SolrServer <>));
            container.Register(typeof(ISolrReadOnlyOperations <>), typeof(SolrServer <>));

            if (setupAction != null)
            {
                var options = new SolrNetOptions(connection.HttpClient);
                //allow for custom headers to be injected.
                setupAction(options);
            }

            return(container);
        }
        private static IServiceCollection AddSolrNet(this IServiceCollection services, IEnumerable <SolrCore> cores, Action <SolrNetOptions> setupAction)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            services.AddSingleton <IReadOnlyMappingManager, AttributesMappingManager>();
            services.AddTransient <ISolrDocumentPropertyVisitor, DefaultDocumentVisitor>();
            services.AddTransient <ISolrFieldParser, DefaultFieldParser>();
            services.AddTransient(typeof(ISolrDocumentActivator <>), typeof(SolrDocumentActivator <>));
            services.AddTransient(typeof(ISolrDocumentResponseParser <>), typeof(SolrDocumentResponseParser <>));

            services.AddTransient <ISolrDocumentResponseParser <Dictionary <string, object> >, SolrDictionaryDocumentResponseParser>();
            services.AddTransient <ISolrFieldSerializer, DefaultFieldSerializer>();
            services.AddTransient <ISolrQuerySerializer, DefaultQuerySerializer>();
            services.AddTransient <ISolrFacetQuerySerializer, DefaultFacetQuerySerializer>();
            services.AddTransient(typeof(ISolrAbstractResponseParser <>), typeof(DefaultResponseParser <>));
            services.AddTransient <ISolrHeaderResponseParser, HeaderResponseParser <string> >();
            services.AddTransient <ISolrExtractResponseParser, ExtractResponseParser>();
            foreach (var p in new[] {
                typeof(MappedPropertiesIsInSolrSchemaRule),
                typeof(RequiredFieldsAreMappedRule),
                typeof(UniqueKeyMatchesMappingRule),
                typeof(MultivaluedMappedToCollectionRule),
            })
            {
                services.AddTransient(typeof(IValidationRule), p);
            }
            services.AddTransient(typeof(ISolrMoreLikeThisHandlerQueryResultsParser <>), typeof(SolrMoreLikeThisHandlerQueryResultsParser <>));
            services.AddTransient(typeof(ISolrDocumentSerializer <>), typeof(SolrDocumentSerializer <>));
            services.AddTransient <ISolrDocumentSerializer <Dictionary <string, object> >, SolrDictionarySerializer>();

            services.AddTransient <ISolrSchemaParser, SolrSchemaParser>();
            services.AddTransient <ISolrDIHStatusParser, SolrDIHStatusParser>();
            services.AddTransient <IMappingValidator, MappingValidator>();

            if (!cores.Any())
            {
                return(services);
            }

            if (cores.Count() > 1)
            {
                throw new NotImplementedException("Microsoft DependencyInjection doesn't support Key/Name based injection. This is a place holder for the future.");
            }

            var connection = new AutoSolrConnection(cores.Single().Url);

            //Bind single type to a single url, prevent breaking existing functionality
            services.AddSingleton <ISolrConnection>(connection);
            services.AddTransient(typeof(ISolrQueryExecuter <>), typeof(SolrQueryExecuter <>));
            services.AddTransient(typeof(ISolrBasicOperations <>), typeof(SolrBasicServer <>));
            services.AddTransient(typeof(ISolrBasicReadOnlyOperations <>), typeof(SolrBasicServer <>));
            services.AddScoped(typeof(ISolrOperations <>), typeof(SolrServer <>));
            services.AddTransient(typeof(ISolrReadOnlyOperations <>), typeof(SolrServer <>));


            if (setupAction != null)
            {
                var options = new SolrNetOptions(connection.HttpClient);
                //allow for custom headers to be injected.
                setupAction(options);
            }


            return(services);
        }