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

Commit

Permalink
Merge branch 'master' of https://github.com/segmentio/Analytics.NET i…
Browse files Browse the repository at this point in the history
…nto issue#154-Catch-PostAsync-Network-exceptions-and-Retry

# Conflicts:
#	Test.NetStandard20/Test.NetStandard20.csproj
  • Loading branch information
juliette-ruchel committed Feb 11, 2021
2 parents a5b224e + a9670f1 commit daeb441
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 1 deletion.
18 changes: 17 additions & 1 deletion Analytics/Analytics.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
<PackageReleaseNotes>Check out the changelog at: https://raw.githubusercontent.com/segmentio/Analytics.NET/master/CHANGELOG.md</PackageReleaseNotes>
<PackageTags>analytics, Segment</PackageTags>
<PackageIcon>packageIcon.png</PackageIcon>
<PackageLicenseFile>README.md</PackageLicenseFile>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RepositoryUrl>https://github.com/segmentio/Analytics.NET.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)'=='net35'">
Expand Down Expand Up @@ -82,4 +84,18 @@
</None>
</ItemGroup>


<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions">
<Version>2.0.0</Version>
</PackageReference>
</ItemGroup>


<ItemGroup Condition="'$(TargetFramework)' == 'net461'">
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions">
<Version>2.0.0</Version>
</PackageReference>
</ItemGroup>

</Project>
24 changes: 24 additions & 0 deletions Analytics/Extensions/ServiceCollectionExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#if NETSTANDARD2_0 || NET461
using Microsoft.Extensions.DependencyInjection;
#endif

namespace Segment.Extensions
{
public static class ServiceCollectionExtension
{
#if NETSTANDARD2_0 || NET461
public static void AddAnalytics(this IServiceCollection services, string writeKey, Config config = null)
{
Config configuration;

if(config == null)
configuration = new Config();
else
configuration = config;

var client = new Client(writeKey, configuration);
services.AddSingleton<IAnalyticsClient>(client);
}
#endif
}
}
51 changes: 51 additions & 0 deletions Test.NetStandard20/Extensions/ServiceCollectionExtensionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Text;
using Segment.Extensions;


namespace Segment.Test.Extensions
{
[TestFixture]
public class ServiceCollectionExtensionTests
{

[Test]
public void TestInicializationOfClientWithServicesCollection()
{
var writeKey = "writeKey";
var services = new ServiceCollection();
services.AddAnalytics(writeKey);

var provider = services.BuildServiceProvider();
var analyticsInstance = provider.GetRequiredService(typeof(IAnalyticsClient));

Assert.IsNotNull(analyticsInstance);

var client = analyticsInstance as Client;
Assert.AreEqual("writeKey", client.WriteKey);
}

[Test]
public void TestInicializationOfClientWithServicesCollectionAndConfig()
{
var writeKey = "writeKey";
var threadCount = 10;
var config = new Config { Threads = threadCount };

var services = new ServiceCollection();
services.AddAnalytics(writeKey, config);

var provider = services.BuildServiceProvider();
var analyticsInstance = provider.GetRequiredService(typeof(IAnalyticsClient));

Assert.IsNotNull(analyticsInstance);

var client = analyticsInstance as Client;
Assert.AreEqual("writeKey", client.WriteKey);
Assert.AreEqual(threadCount, client.Config.Threads);
}
}
}
1 change: 1 addition & 0 deletions Test.NetStandard20/Test.NetStandard20.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.0.0" />
<PackageReference Include="EmbedIO" Version="3.4.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0-preview-20170628-02" />
<PackageReference Include="Moq" Version="4.13.1" />
Expand Down
51 changes: 51 additions & 0 deletions Test/Extensions/ServiceCollectionExtensionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Text;
using Segment.Extensions;


namespace Segment.Test.Extensions
{
[TestFixture]
public class ServiceCollectionExtensionTests
{

[Test]
public void TestInicializationOfClientWithServicesCollection()
{
var writeKey = "writeKey";
var services = new ServiceCollection();
services.AddAnalytics(writeKey);

var provider = services.BuildServiceProvider();
var analyticsInstance = provider.GetRequiredService(typeof(IAnalyticsClient));

Assert.IsNotNull(analyticsInstance);

var client = analyticsInstance as Client;
Assert.AreEqual("writeKey", client.WriteKey);
}

[Test]
public void TestInicializationOfClientWithServicesCollectionAndConfig()
{
var writeKey = "writeKey";
var threadCount = 10;
var config = new Config { Threads = threadCount };

var services = new ServiceCollection();
services.AddAnalytics(writeKey, config);

var provider = services.BuildServiceProvider();
var analyticsInstance = provider.GetRequiredService(typeof(IAnalyticsClient));

Assert.IsNotNull(analyticsInstance);

var client = analyticsInstance as Client;
Assert.AreEqual("writeKey", client.WriteKey);
Assert.AreEqual(threadCount, client.Config.Threads);
}
}
}
1 change: 1 addition & 0 deletions Test/Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0-preview-20170628-02" />
<PackageReference Include="Moq" Version="4.13.1" />
<PackageReference Include="NUnit" Version="3.8.1" />
Expand Down

0 comments on commit daeb441

Please sign in to comment.