Skip to content

Commit

Permalink
Integrate SendGrid with HttpClientFactory (#922)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
akunzai authored May 28, 2020
1 parent 4b45af0 commit cadb619
Show file tree
Hide file tree
Showing 21 changed files with 506 additions and 482 deletions.
11 changes: 2 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,11 @@ dotnet: 2.1.803
solution: SendGrid.sln
os: linux
dist: bionic
mono: latest
mono: none

services:
- docker

before_install:
- mkdir -p .nuget
- wget -O .nuget/nuget.exe https://dist.nuget.org/win-x86-commandline/latest/nuget.exe

env:
- FrameworkPathOverride=/usr/lib/mono/4.5/

script:
- make test-docker
- make release
Expand All @@ -25,7 +18,7 @@ after_success:
deploy:
skip_cleanup: true
provider: script
script: nuget push ./src/SendGrid/bin/Release/SendGrid.*.nupkg -ApiKey $NUGET_API_KEY -Source https://api.nuget.org/v3/index.json
script: dotnet nuget push **/*.nupkg -k $NUGET_API_KEY -s https://api.nuget.org/v3/index.json
on:
branch: master
tags: true
Expand Down
27 changes: 16 additions & 11 deletions ExampleCoreProject/ExampleCoreProject.csproj
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp1.0</TargetFramework>
<AssemblyName>ExampleCoreProject</AssemblyName>
<TargetFramework>netcoreapp2.1</TargetFramework>
<OutputType>Exe</OutputType>
<PackageId>ExampleCoreProject</PackageId>
<RuntimeFrameworkVersion>1.0.4</RuntimeFrameworkVersion>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\src\SendGrid\SendGrid.csproj" />
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="appsettings.*.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp1.0' ">
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
<PackageReference Include="System.Net.Http" Version="4.3.2" />
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Http" Version="3.1.4" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\src\SendGrid.Extensions.DependencyInjection\SendGrid.Extensions.DependencyInjection.csproj" />
</ItemGroup>

</Project>
57 changes: 57 additions & 0 deletions ExampleCoreProject/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SendGrid;
using SendGrid.Extensions.DependencyInjection;
using SendGrid.Helpers.Mail;

namespace Example
{
internal class Program
{
private static IConfiguration Configuration { get; set; }

private static async Task Main()
{
var env = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? "Production";
Configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true)
.AddJsonFile($"appsettings.{env}.json", optional: true)
.Build();
var services = ConfigureServices(new ServiceCollection()).BuildServiceProvider();
var client = services.GetRequiredService<ISendGridClient>();
var from = new EmailAddress(Configuration.GetValue("SendGrid:From", "[email protected]"), "Example User");
var to = new EmailAddress(Configuration.GetValue("SendGrid:To", "[email protected]"), "Example User");
var msg = new SendGridMessage
{
From = from,
Subject = "Sending with Twilio SendGrid is Fun"
};
msg.AddContent(MimeType.Text, "and easy to do anywhere, even with C#");
msg.AddTo(to);
if (Configuration.GetValue("SendGrid:SandboxMode", false))
{
msg.MailSettings = new MailSettings
{
SandboxMode = new SandboxMode
{
Enable = true
}
};
}
Console.WriteLine($"Sending email with payload: \n{msg.Serialize()}");
var response = await client.SendEmailAsync(msg).ConfigureAwait(false);

Console.WriteLine($"Response: {response.StatusCode}");
Console.WriteLine(response.Headers);
}

private static IServiceCollection ConfigureServices(IServiceCollection services)
{
services.AddSendGrid(options => { options.ApiKey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY") ?? Configuration["SendGrid:ApiKey"]; });

return services;
}
}
}
18 changes: 0 additions & 18 deletions ExampleCoreProject/Properties/AssemblyInfo.cs

This file was deleted.

6 changes: 0 additions & 6 deletions ExampleNet45Project/App.config

This file was deleted.

172 changes: 0 additions & 172 deletions ExampleNet45Project/Example.cs

This file was deleted.

Loading

0 comments on commit cadb619

Please sign in to comment.