Skip to content

Commit

Permalink
MS Dependency Injection update
Browse files Browse the repository at this point in the history
* Switch to Singleton AutoSolrConnection
* Add override to IServiceCollection.AddSolrNet , allowing to set injection options. Currently only supporting tweaking the HttpClient in use by the AutoSolrConnection.
* Add unit test showing the use of Basic Authentication.

Fixes SolrNet#402
  • Loading branch information
gjunge committed Mar 18, 2018
1 parent a900534 commit fd80276
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ public void ResolveSolrAbstractResponseParser()
public void CannotResolveSolrAbstractResponseParsersViaArray()
{
//MS Dependency injection doesn't support
Assert.Throws<InvalidOperationException>(() =>
Assert.Throws<InvalidOperationException>(() =>
DefaultServiceProvider.GetRequiredService<ISolrAbstractResponseParser<Entity>[]>()
);

}


Expand Down Expand Up @@ -107,6 +107,27 @@ public void ResolveSolrOperations()
}



[Fact]
public void SetBasicAuthenticationHeader()
{
var sc = new ServiceCollection();

//my credentials
var credentials = System.Text.Encoding.ASCII.GetBytes("myUsername:myPassword");
//in base64
var credentialsBase64 = Convert.ToBase64String(credentials);
//use the options to set the Authorization header.
sc.AddSolrNet("http://localhost:8983/solr", options => { options.HttpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", credentialsBase64); });

//test
var provider = sc.BuildServiceProvider();
var connection = provider.GetRequiredService<ISolrConnection>() as AutoSolrConnection;

Assert.NotNull(connection.HttpClient.DefaultRequestHeaders.Authorization);
Assert.Equal(credentialsBase64, connection.HttpClient.DefaultRequestHeaders.Authorization.Parameter);
}

public class Entity { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ public static IServiceCollection AddSolrNet(this IServiceCollection services, st
{
if (services == null) throw new ArgumentNullException(nameof(services));

return services.AddSolrNet(new SolrCore[] { new SolrCore(null, null, url) });
return services.AddSolrNet(new SolrCore[] { new SolrCore(null, null, url) }, null);
}

private static IServiceCollection AddSolrNet(this IServiceCollection services, IEnumerable<SolrCore> cores)
public static IServiceCollection AddSolrNet(this IServiceCollection services, string url, Action<SolrNetOptions> setupAction) => AddSolrNet(services, new SolrCore[] { new SolrCore(null, null, url) }, setupAction);

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

Expand All @@ -34,7 +36,7 @@ private static IServiceCollection AddSolrNet(this IServiceCollection services, I
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>();
Expand Down Expand Up @@ -63,15 +65,22 @@ private static IServiceCollection AddSolrNet(this IServiceCollection services, I
{
throw new NotImplementedException("Microsoft DependencyInjection doesn't support Key/Name based injection. This is a place holder for the future.");
}
else

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)
{
//Bind single type to a single url, prevent breaking existing functionality
services.AddTransient<ISolrConnection>(_ => new SolrConnection(cores.Single().Url));
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<>));
var options = new SolrNetOptions(connection.HttpClient);
//allow for custom headers to be injected.
setupAction(options);
}


Expand Down
17 changes: 17 additions & 0 deletions Microsoft.DependencyInjection.SolrNet/SolrNetOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Net.Http;

namespace SolrNet
{
public class SolrNetOptions
{
public SolrNetOptions(HttpClient httpClient)
{
HttpClient = httpClient;
}

/// <summary>
/// Gets the HttpClient with which SolrNet connects to the Solr server. This is the place to add Default Headers for Basic Authentication for example..
/// </summary>
public HttpClient HttpClient { get; }
}
}

0 comments on commit fd80276

Please sign in to comment.