Skip to content
This repository was archived by the owner on Apr 24, 2024. It is now read-only.

Commit daeb441

Browse files
author
juliette-ruchel
committed
Merge branch 'master' of https://github.com/segmentio/Analytics.NET into issue#154-Catch-PostAsync-Network-exceptions-and-Retry
# Conflicts: # Test.NetStandard20/Test.NetStandard20.csproj
2 parents a5b224e + a9670f1 commit daeb441

File tree

6 files changed

+145
-1
lines changed

6 files changed

+145
-1
lines changed

Analytics/Analytics.csproj

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
<PackageReleaseNotes>Check out the changelog at: https://raw.githubusercontent.com/segmentio/Analytics.NET/master/CHANGELOG.md</PackageReleaseNotes>
1515
<PackageTags>analytics, Segment</PackageTags>
1616
<PackageIcon>packageIcon.png</PackageIcon>
17-
<PackageLicenseFile>README.md</PackageLicenseFile>
17+
<PackageLicenseExpression>MIT</PackageLicenseExpression>
18+
<RepositoryUrl>https://github.com/segmentio/Analytics.NET.git</RepositoryUrl>
19+
<RepositoryType>git</RepositoryType>
1820
</PropertyGroup>
1921

2022
<PropertyGroup Condition="'$(TargetFramework)'=='net35'">
@@ -82,4 +84,18 @@
8284
</None>
8385
</ItemGroup>
8486

87+
88+
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
89+
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions">
90+
<Version>2.0.0</Version>
91+
</PackageReference>
92+
</ItemGroup>
93+
94+
95+
<ItemGroup Condition="'$(TargetFramework)' == 'net461'">
96+
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions">
97+
<Version>2.0.0</Version>
98+
</PackageReference>
99+
</ItemGroup>
100+
85101
</Project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#if NETSTANDARD2_0 || NET461
2+
using Microsoft.Extensions.DependencyInjection;
3+
#endif
4+
5+
namespace Segment.Extensions
6+
{
7+
public static class ServiceCollectionExtension
8+
{
9+
#if NETSTANDARD2_0 || NET461
10+
public static void AddAnalytics(this IServiceCollection services, string writeKey, Config config = null)
11+
{
12+
Config configuration;
13+
14+
if(config == null)
15+
configuration = new Config();
16+
else
17+
configuration = config;
18+
19+
var client = new Client(writeKey, configuration);
20+
services.AddSingleton<IAnalyticsClient>(client);
21+
}
22+
#endif
23+
}
24+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using NUnit.Framework;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Text;
6+
using Segment.Extensions;
7+
8+
9+
namespace Segment.Test.Extensions
10+
{
11+
[TestFixture]
12+
public class ServiceCollectionExtensionTests
13+
{
14+
15+
[Test]
16+
public void TestInicializationOfClientWithServicesCollection()
17+
{
18+
var writeKey = "writeKey";
19+
var services = new ServiceCollection();
20+
services.AddAnalytics(writeKey);
21+
22+
var provider = services.BuildServiceProvider();
23+
var analyticsInstance = provider.GetRequiredService(typeof(IAnalyticsClient));
24+
25+
Assert.IsNotNull(analyticsInstance);
26+
27+
var client = analyticsInstance as Client;
28+
Assert.AreEqual("writeKey", client.WriteKey);
29+
}
30+
31+
[Test]
32+
public void TestInicializationOfClientWithServicesCollectionAndConfig()
33+
{
34+
var writeKey = "writeKey";
35+
var threadCount = 10;
36+
var config = new Config { Threads = threadCount };
37+
38+
var services = new ServiceCollection();
39+
services.AddAnalytics(writeKey, config);
40+
41+
var provider = services.BuildServiceProvider();
42+
var analyticsInstance = provider.GetRequiredService(typeof(IAnalyticsClient));
43+
44+
Assert.IsNotNull(analyticsInstance);
45+
46+
var client = analyticsInstance as Client;
47+
Assert.AreEqual("writeKey", client.WriteKey);
48+
Assert.AreEqual(threadCount, client.Config.Threads);
49+
}
50+
}
51+
}

Test.NetStandard20/Test.NetStandard20.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.0.0" />
1213
<PackageReference Include="EmbedIO" Version="3.4.3" />
1314
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0-preview-20170628-02" />
1415
<PackageReference Include="Moq" Version="4.13.1" />
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using NUnit.Framework;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Text;
6+
using Segment.Extensions;
7+
8+
9+
namespace Segment.Test.Extensions
10+
{
11+
[TestFixture]
12+
public class ServiceCollectionExtensionTests
13+
{
14+
15+
[Test]
16+
public void TestInicializationOfClientWithServicesCollection()
17+
{
18+
var writeKey = "writeKey";
19+
var services = new ServiceCollection();
20+
services.AddAnalytics(writeKey);
21+
22+
var provider = services.BuildServiceProvider();
23+
var analyticsInstance = provider.GetRequiredService(typeof(IAnalyticsClient));
24+
25+
Assert.IsNotNull(analyticsInstance);
26+
27+
var client = analyticsInstance as Client;
28+
Assert.AreEqual("writeKey", client.WriteKey);
29+
}
30+
31+
[Test]
32+
public void TestInicializationOfClientWithServicesCollectionAndConfig()
33+
{
34+
var writeKey = "writeKey";
35+
var threadCount = 10;
36+
var config = new Config { Threads = threadCount };
37+
38+
var services = new ServiceCollection();
39+
services.AddAnalytics(writeKey, config);
40+
41+
var provider = services.BuildServiceProvider();
42+
var analyticsInstance = provider.GetRequiredService(typeof(IAnalyticsClient));
43+
44+
Assert.IsNotNull(analyticsInstance);
45+
46+
var client = analyticsInstance as Client;
47+
Assert.AreEqual("writeKey", client.WriteKey);
48+
Assert.AreEqual(threadCount, client.Config.Threads);
49+
}
50+
}
51+
}

Test/Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.0.0" />
1213
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0-preview-20170628-02" />
1314
<PackageReference Include="Moq" Version="4.13.1" />
1415
<PackageReference Include="NUnit" Version="3.8.1" />

0 commit comments

Comments
 (0)