Skip to content

Commit

Permalink
Add netstandard 2.0 support to Autofac intregration
Browse files Browse the repository at this point in the history
  • Loading branch information
gjunge committed Feb 11, 2018
1 parent 741ca28 commit e0df32a
Show file tree
Hide file tree
Showing 14 changed files with 221 additions and 60 deletions.
55 changes: 55 additions & 0 deletions AutofacContrib.SolrNet.Tests/AutofacConfigFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Autofac;
using Microsoft.Extensions.Configuration;
using SolrNet;
using SolrNet.Impl;
using Xunit;

namespace AutofacContrib.SolrNet.Tests
{
public class AutofacConfigFixture
{

[Fact]
public void RegistersSolrConnectionWithCoresJsonServerUrl()
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetParent("../../../").FullName)
.AddJsonFile("cores.json")
.Build()
.GetSection("solr:servers");

var builder = new ContainerBuilder();
builder.RegisterModule(new SolrNetModule(configuration.Get<List<SolrServer>>()));
var container = builder.Build();
var solrConnection = (SolrConnection)container.ResolveNamed<ISolrConnection>("entitySolrNet.Impl.SolrConnection");

Assert.Equal("http://localhost:8983/solr/collection1", solrConnection.ServerURL);
}


[Fact]
public void CheckParseJsonConfiguration()
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetParent("../../../").FullName)
.AddJsonFile("cores.json")
.Build()
.GetSection("solr:servers");

var servers = configuration.Get<List<SolrServer>>();

Assert.Equal(3, servers.Count);
Assert.Equal("entity", servers.First().Id);
Assert.Equal("http://localhost:8983/solr/collection1", servers.First().Url);
Assert.Equal("AutofacContrib.SolrNet.Tests.Entity, AutofacContrib.SolrNet.Tests", servers.First().DocumentType);

Assert.Equal("entity3", servers.Last().Id);
Assert.Equal("http://localhost:8983/solr/core1", servers.Last().Url);
Assert.Equal("AutofacContrib.SolrNet.Tests.Entity2, AutofacContrib.SolrNet.Tests", servers.Last().DocumentType);

}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net46</TargetFramework>
<TargetFramework>net462</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AutofacContrib.SolrNet\AutofacContrib.SolrNet.csproj">
Expand All @@ -24,4 +26,9 @@
<ItemGroup>
<Reference Include="System.Configuration" />
</ItemGroup>
<ItemGroup>
<None Update="cores.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
3 changes: 2 additions & 1 deletion AutofacContrib.SolrNet.Tests/AutofacMulticoreFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ public void ResolveSolrOperations_fromConfigSection()
}
}

public class Entity { }
public class Entity1 { }
public class Entity2 { }
}
}
21 changes: 21 additions & 0 deletions AutofacContrib.SolrNet.Tests/cores.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"solr": {
"servers": [
{
"id": "entity",
"url": "http://localhost:8983/solr/collection1",
"documentType": "AutofacContrib.SolrNet.Tests.Entity, AutofacContrib.SolrNet.Tests"
},
{
"id": "entity2",
"url": "http://localhost:8983/solr/core0",
"documentType": "AutofacContrib.SolrNet.Tests.Entity2, AutofacContrib.SolrNet.Tests"
},
{
"id": "entity3",
"url": "http://localhost:8983/solr/core1",
"documentType": "AutofacContrib.SolrNet.Tests.Entity2, AutofacContrib.SolrNet.Tests"
}
]
}
}
11 changes: 8 additions & 3 deletions AutofacContrib.SolrNet/AutofacContrib.SolrNet.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net46</TargetFrameworks>
<TargetFrameworks>net46;netstandard2.0</TargetFrameworks>
<Version>1.0.3</Version>
<RepositoryUrl>https://github.com/solrnet/solrnet</RepositoryUrl>
<PackageLicenseUrl>https://raw.githubusercontent.com/SolrNet/SolrNet/master/license.txt</PackageLicenseUrl>
Expand All @@ -18,9 +18,14 @@
<ProjectReference Include="..\SolrNet\SolrNet.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Autofac" Version="4.6.1" />
<PackageReference Include="Autofac" Version="4.6.2" />
</ItemGroup>
<ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net46'">
<Reference Include="System.Configuration" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<Compile Remove="Config\SolrConfigurationSection.cs" />
<Compile Remove="Config\SolrServerElement.cs" />
<Compile Remove="Config\SolrServers.cs" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion AutofacContrib.SolrNet/Config/SolrConfigurationSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ public SolrServers SolrServers
get { return (SolrServers)base[""]; }
}
}
}
}
5 changes: 3 additions & 2 deletions AutofacContrib.SolrNet/Config/SolrServerElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ namespace AutofacContrib.SolrNet.Config {
/// <summary>
/// Solr instance or core config
/// </summary>
public class SolrServerElement : ConfigurationElement {
public class SolrServerElement : ConfigurationElement, ISolrServer
{
private const string ID = "id";
private const string URL = "url";
private const string DOCUMENT_TYPE = "documentType";
Expand Down Expand Up @@ -40,4 +41,4 @@ public override string ToString() {
return string.Format("Id: {0} Url: {1} DocType: {2}", Id, Url, DocumentType);
}
}
}
}
15 changes: 12 additions & 3 deletions AutofacContrib.SolrNet/Config/SolrServers.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System.Configuration;
using System.Collections.Generic;
using System.Configuration;

namespace AutofacContrib.SolrNet.Config {
/// <summary>
/// Solr cores / instances configuration
/// </summary>
public class SolrServers : ConfigurationElementCollection {
public class SolrServers : ConfigurationElementCollection, IEnumerable<ISolrServer> {
/// <summary>
/// Adds a new core / instance to the config
/// </summary>
Expand All @@ -22,6 +23,14 @@ protected override object GetElementKey(ConfigurationElement element) {
return solrServerElement.Url + solrServerElement.DocumentType;
}

IEnumerator<ISolrServer> IEnumerable<ISolrServer>.GetEnumerator()
{
foreach (SolrServerElement server in this)
{
yield return server;
}
}

public override ConfigurationElementCollectionType CollectionType {
get { return ConfigurationElementCollectionType.BasicMap; }
}
Expand All @@ -30,4 +39,4 @@ protected override string ElementName {
get { return "server"; }
}
}
}
}
9 changes: 9 additions & 0 deletions AutofacContrib.SolrNet/ISolrServer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace AutofacContrib.SolrNet
{
public interface ISolrServer
{
string Id { get; set; }
string Url { get; set; }
string DocumentType { get; set; }
}
}
14 changes: 14 additions & 0 deletions AutofacContrib.SolrNet/RegistrationException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AutofacContrib.SolrNet
{
public class RegistrationException : ApplicationException
{
public RegistrationException(string message, Exception innerException) : base(message, innerException) { }
public RegistrationException(string message) : base(message) {}
}
}
Loading

0 comments on commit e0df32a

Please sign in to comment.