Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for ASP.NET Core Dependency Injection #904

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions ExampleAspNetCoreProject/Controllers/EmailController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Threading.Tasks;
using ExampleAspNetCoreProject.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using SendGrid;
using SendGrid.Helpers.Mail;

namespace ExampleAspNetCoreProject.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class EmailController : ControllerBase
{
private readonly ILogger<EmailController> logger;
private readonly ISendGridClient client;

public EmailController(ILogger<EmailController> logger, ISendGridClient client)
{
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
this.client = client ?? throw new ArgumentNullException(nameof(client));
}

[HttpPost("send")]
public async Task<IActionResult> SendEmail([FromBody] SendEmailModel model)
{
if (!ModelState.IsValid)
return BadRequest(ModelState);

var message = new SendGridMessage
{
HtmlContent = model.Content,
Subject = model.Subject,
From = new EmailAddress(model.From)
};

message.AddTo(model.Recipient);

logger.LogInformation($"Sending email to {model.Recipient} with subject {model.Subject}.");

var response = await client.SendEmailAsync(message);

logger.LogInformation($"SendGrid responded with status code: {response.StatusCode}");

return StatusCode((int) response.StatusCode);
}
}
}
18 changes: 18 additions & 0 deletions ExampleAspNetCoreProject/ExampleAspNetCoreProject.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
<UserSecretsId>adf5331b-3d39-4f3d-94f2-e5808d7e3a7a</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
</ItemGroup>

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

</Project>
21 changes: 21 additions & 0 deletions ExampleAspNetCoreProject/Models/SendEmailModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.ComponentModel.DataAnnotations;

namespace ExampleAspNetCoreProject.Models
{
public class SendEmailModel
{
[Required]
[EmailAddress]
public string Recipient { get; set; }

[Required]
[EmailAddress]
public string From { get; set; }

[Required]
public string Subject { get; set; }

[Required]
public string Content { get; set; }
}
}
17 changes: 17 additions & 0 deletions ExampleAspNetCoreProject/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;

namespace ExampleAspNetCoreProject
{
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
}
28 changes: 28 additions & 0 deletions ExampleAspNetCoreProject/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:52446",
"sslPort": 44352
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"ExampleAspNetCoreProject": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
44 changes: 44 additions & 0 deletions ExampleAspNetCoreProject/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Net.Http;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SendGrid;

namespace ExampleAspNetCoreProject
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services)
{
// Configure the SendGrid client based on the values specified in the application settings.
// We then register the Client and its options with the DI container, passing a singleton HttpClient instance
// to be used for making requests to the API. This allows us to inject the client into our controllers.
services.Configure<SendGridClientOptions>(Configuration.GetSection("SendGrid"));

services.AddSingleton(new HttpClient());
services.AddSingleton<ISendGridClient, SendGridClient>();

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseHttpsRedirection();
app.UseMvc();
}
}
}
9 changes: 9 additions & 0 deletions ExampleAspNetCoreProject/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
11 changes: 11 additions & 0 deletions ExampleAspNetCoreProject/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"SendGrid": {
"ApiKey": ""
}
}
3 changes: 3 additions & 0 deletions ExampleNet45Project/ExampleNet45.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<PackageReference Include="Microsoft.Extensions.Options">
<Version>1.1.2</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
<Compile Include="Example.cs" />
Expand Down
114 changes: 62 additions & 52 deletions SendGrid.sln
Original file line number Diff line number Diff line change
@@ -1,52 +1,62 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26430.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{71C09624-020B-410E-A8FE-1FD216A1FE31}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{D06BDAE9-BE83-448F-8AD4-3044BB187C11}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Example Projects", "Example Projects", "{D3201F71-A289-4136-AC7E-E5204ACB9183}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SendGrid", "src\SendGrid\SendGrid.csproj", "{377C20E4-2297-488F-933B-FB635C56D8FC}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SendGrid.Tests", "tests\SendGrid.Tests\SendGrid.Tests.csproj", "{D89ADAEA-2BE8-49AC-B5BC-6EABBB2AE4E3}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExampleCoreProject", "ExampleCoreProject\ExampleCoreProject.csproj", "{4BD07A97-8AD2-4134-848E-6A74EB992050}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExampleNet45", "ExampleNet45Project\ExampleNet45.csproj", "{3B3F2699-F720-4498-8044-262EFE110A22}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{377C20E4-2297-488F-933B-FB635C56D8FC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{377C20E4-2297-488F-933B-FB635C56D8FC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{377C20E4-2297-488F-933B-FB635C56D8FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{377C20E4-2297-488F-933B-FB635C56D8FC}.Release|Any CPU.Build.0 = Release|Any CPU
{D89ADAEA-2BE8-49AC-B5BC-6EABBB2AE4E3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D89ADAEA-2BE8-49AC-B5BC-6EABBB2AE4E3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D89ADAEA-2BE8-49AC-B5BC-6EABBB2AE4E3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D89ADAEA-2BE8-49AC-B5BC-6EABBB2AE4E3}.Release|Any CPU.Build.0 = Release|Any CPU
{4BD07A97-8AD2-4134-848E-6A74EB992050}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4BD07A97-8AD2-4134-848E-6A74EB992050}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4BD07A97-8AD2-4134-848E-6A74EB992050}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4BD07A97-8AD2-4134-848E-6A74EB992050}.Release|Any CPU.Build.0 = Release|Any CPU
{3B3F2699-F720-4498-8044-262EFE110A22}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3B3F2699-F720-4498-8044-262EFE110A22}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3B3F2699-F720-4498-8044-262EFE110A22}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3B3F2699-F720-4498-8044-262EFE110A22}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{377C20E4-2297-488F-933B-FB635C56D8FC} = {71C09624-020B-410E-A8FE-1FD216A1FE31}
{D89ADAEA-2BE8-49AC-B5BC-6EABBB2AE4E3} = {D06BDAE9-BE83-448F-8AD4-3044BB187C11}
{4BD07A97-8AD2-4134-848E-6A74EB992050} = {D3201F71-A289-4136-AC7E-E5204ACB9183}
{3B3F2699-F720-4498-8044-262EFE110A22} = {D3201F71-A289-4136-AC7E-E5204ACB9183}
EndGlobalSection
EndGlobal

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29025.244
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{71C09624-020B-410E-A8FE-1FD216A1FE31}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{D06BDAE9-BE83-448F-8AD4-3044BB187C11}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Example Projects", "Example Projects", "{D3201F71-A289-4136-AC7E-E5204ACB9183}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SendGrid", "src\SendGrid\SendGrid.csproj", "{377C20E4-2297-488F-933B-FB635C56D8FC}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SendGrid.Tests", "tests\SendGrid.Tests\SendGrid.Tests.csproj", "{D89ADAEA-2BE8-49AC-B5BC-6EABBB2AE4E3}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExampleCoreProject", "ExampleCoreProject\ExampleCoreProject.csproj", "{4BD07A97-8AD2-4134-848E-6A74EB992050}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExampleNet45", "ExampleNet45Project\ExampleNet45.csproj", "{3B3F2699-F720-4498-8044-262EFE110A22}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExampleAspNetCoreProject", "ExampleAspNetCoreProject\ExampleAspNetCoreProject.csproj", "{7C3D92E0-86DE-46A6-BE16-1E798DF728C8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{377C20E4-2297-488F-933B-FB635C56D8FC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{377C20E4-2297-488F-933B-FB635C56D8FC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{377C20E4-2297-488F-933B-FB635C56D8FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{377C20E4-2297-488F-933B-FB635C56D8FC}.Release|Any CPU.Build.0 = Release|Any CPU
{D89ADAEA-2BE8-49AC-B5BC-6EABBB2AE4E3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D89ADAEA-2BE8-49AC-B5BC-6EABBB2AE4E3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D89ADAEA-2BE8-49AC-B5BC-6EABBB2AE4E3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D89ADAEA-2BE8-49AC-B5BC-6EABBB2AE4E3}.Release|Any CPU.Build.0 = Release|Any CPU
{4BD07A97-8AD2-4134-848E-6A74EB992050}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4BD07A97-8AD2-4134-848E-6A74EB992050}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4BD07A97-8AD2-4134-848E-6A74EB992050}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4BD07A97-8AD2-4134-848E-6A74EB992050}.Release|Any CPU.Build.0 = Release|Any CPU
{3B3F2699-F720-4498-8044-262EFE110A22}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3B3F2699-F720-4498-8044-262EFE110A22}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3B3F2699-F720-4498-8044-262EFE110A22}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3B3F2699-F720-4498-8044-262EFE110A22}.Release|Any CPU.Build.0 = Release|Any CPU
{7C3D92E0-86DE-46A6-BE16-1E798DF728C8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7C3D92E0-86DE-46A6-BE16-1E798DF728C8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7C3D92E0-86DE-46A6-BE16-1E798DF728C8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7C3D92E0-86DE-46A6-BE16-1E798DF728C8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{377C20E4-2297-488F-933B-FB635C56D8FC} = {71C09624-020B-410E-A8FE-1FD216A1FE31}
{D89ADAEA-2BE8-49AC-B5BC-6EABBB2AE4E3} = {D06BDAE9-BE83-448F-8AD4-3044BB187C11}
{4BD07A97-8AD2-4134-848E-6A74EB992050} = {D3201F71-A289-4136-AC7E-E5204ACB9183}
{3B3F2699-F720-4498-8044-262EFE110A22} = {D3201F71-A289-4136-AC7E-E5204ACB9183}
{7C3D92E0-86DE-46A6-BE16-1E798DF728C8} = {D3201F71-A289-4136-AC7E-E5204ACB9183}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {12F463FA-F03B-48C9-8F64-5BED32F8D5D0}
EndGlobalSection
EndGlobal
Loading