Skip to content

Commit cadb619

Browse files
authored
Integrate SendGrid with HttpClientFactory (#922)
* Integrate SendGrid with HttpClientFactory - Introduce new SendGrid.Extensions.DependencyInjection package for HttpClientFactory integrations * Update SendGrid.Tests - Update NuGet packages - Cleanup Properties * Add Solution Items * Renamed `Example.cs` to `Program.cs` - `Program.cs` is the convention of C# console application entry point * Build .NET Framework libraries on Linux without Mono - https://andrewlock.net/using-reference-assemblies-to-build-net-framework-libararies-on-linux-without-mono/ * Simplify Examples - Upgrade to SDK style csproj - Code cleanup * Simplify Makefile * Update README.md - Add `SendGrid.Extensions.DependencyInjection` usage - Fixes typo - Fixes Markdown syntax warnings - Simplify examples * Use .NET Core CLI for *.nupkg deployment - https://docs.microsoft.com/dotnet/core/tools/dotnet-nuget-push
1 parent 4b45af0 commit cadb619

File tree

21 files changed

+506
-482
lines changed

21 files changed

+506
-482
lines changed

.travis.yml

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,11 @@ dotnet: 2.1.803
33
solution: SendGrid.sln
44
os: linux
55
dist: bionic
6-
mono: latest
6+
mono: none
77

88
services:
99
- docker
1010

11-
before_install:
12-
- mkdir -p .nuget
13-
- wget -O .nuget/nuget.exe https://dist.nuget.org/win-x86-commandline/latest/nuget.exe
14-
15-
env:
16-
- FrameworkPathOverride=/usr/lib/mono/4.5/
17-
1811
script:
1912
- make test-docker
2013
- make release
@@ -25,7 +18,7 @@ after_success:
2518
deploy:
2619
skip_cleanup: true
2720
provider: script
28-
script: nuget push ./src/SendGrid/bin/Release/SendGrid.*.nupkg -ApiKey $NUGET_API_KEY -Source https://api.nuget.org/v3/index.json
21+
script: dotnet nuget push **/*.nupkg -k $NUGET_API_KEY -s https://api.nuget.org/v3/index.json
2922
on:
3023
branch: master
3124
tags: true
Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp1.0</TargetFramework>
5-
<AssemblyName>ExampleCoreProject</AssemblyName>
4+
<TargetFramework>netcoreapp2.1</TargetFramework>
65
<OutputType>Exe</OutputType>
7-
<PackageId>ExampleCoreProject</PackageId>
8-
<RuntimeFrameworkVersion>1.0.4</RuntimeFrameworkVersion>
9-
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
10-
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
11-
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
6+
<IsPackable>false</IsPackable>
127
</PropertyGroup>
138

149
<ItemGroup>
15-
<ProjectReference Include="..\src\SendGrid\SendGrid.csproj" />
10+
<None Update="appsettings.json">
11+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
12+
</None>
13+
<None Update="appsettings.*.json">
14+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
15+
</None>
1616
</ItemGroup>
1717

18-
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp1.0' ">
19-
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
20-
<PackageReference Include="System.Net.Http" Version="4.3.2" />
18+
<ItemGroup>
19+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.4" />
20+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.4" />
21+
<PackageReference Include="Microsoft.Extensions.Http" Version="3.1.4" />
22+
</ItemGroup>
23+
24+
<ItemGroup>
25+
<ProjectReference Include="..\src\SendGrid.Extensions.DependencyInjection\SendGrid.Extensions.DependencyInjection.csproj" />
2126
</ItemGroup>
2227

2328
</Project>

ExampleCoreProject/Program.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Microsoft.Extensions.Configuration;
4+
using Microsoft.Extensions.DependencyInjection;
5+
using SendGrid;
6+
using SendGrid.Extensions.DependencyInjection;
7+
using SendGrid.Helpers.Mail;
8+
9+
namespace Example
10+
{
11+
internal class Program
12+
{
13+
private static IConfiguration Configuration { get; set; }
14+
15+
private static async Task Main()
16+
{
17+
var env = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? "Production";
18+
Configuration = new ConfigurationBuilder()
19+
.AddJsonFile("appsettings.json", optional: true)
20+
.AddJsonFile($"appsettings.{env}.json", optional: true)
21+
.Build();
22+
var services = ConfigureServices(new ServiceCollection()).BuildServiceProvider();
23+
var client = services.GetRequiredService<ISendGridClient>();
24+
var from = new EmailAddress(Configuration.GetValue("SendGrid:From", "[email protected]"), "Example User");
25+
var to = new EmailAddress(Configuration.GetValue("SendGrid:To", "[email protected]"), "Example User");
26+
var msg = new SendGridMessage
27+
{
28+
From = from,
29+
Subject = "Sending with Twilio SendGrid is Fun"
30+
};
31+
msg.AddContent(MimeType.Text, "and easy to do anywhere, even with C#");
32+
msg.AddTo(to);
33+
if (Configuration.GetValue("SendGrid:SandboxMode", false))
34+
{
35+
msg.MailSettings = new MailSettings
36+
{
37+
SandboxMode = new SandboxMode
38+
{
39+
Enable = true
40+
}
41+
};
42+
}
43+
Console.WriteLine($"Sending email with payload: \n{msg.Serialize()}");
44+
var response = await client.SendEmailAsync(msg).ConfigureAwait(false);
45+
46+
Console.WriteLine($"Response: {response.StatusCode}");
47+
Console.WriteLine(response.Headers);
48+
}
49+
50+
private static IServiceCollection ConfigureServices(IServiceCollection services)
51+
{
52+
services.AddSendGrid(options => { options.ApiKey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY") ?? Configuration["SendGrid:ApiKey"]; });
53+
54+
return services;
55+
}
56+
}
57+
}

ExampleCoreProject/Properties/AssemblyInfo.cs

Lines changed: 0 additions & 18 deletions
This file was deleted.

ExampleNet45Project/App.config

Lines changed: 0 additions & 6 deletions
This file was deleted.

ExampleNet45Project/Example.cs

Lines changed: 0 additions & 172 deletions
This file was deleted.

0 commit comments

Comments
 (0)