Skip to content

Commit

Permalink
Merge pull request #35 from EdiWang/dev/net8-isolated
Browse files Browse the repository at this point in the history
Dev/net8 isolated
  • Loading branch information
EdiWang authored Apr 2, 2024
2 parents bccda4d + 0080b5c commit 0514e36
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 112 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/azfunc.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Moonglade.Function.Email Build and Deploy
name: Moonglade.Email Build and Deploy

on:
push:
Expand All @@ -14,7 +14,7 @@ on:
env:
AZURE_FUNCTIONAPP_NAME: moonglade-email # set this to your application's name
AZURE_FUNCTIONAPP_PACKAGE_PATH: '.' # set this to the path to your web app project, defaults to the repository root
DOTNET_VERSION: '6.0.x' # set this to the dotnet version to use
DOTNET_VERSION: '8.0.x' # set this to the dotnet version to use

jobs:
build-and-deploy:
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM mcr.microsoft.com/azure-functions/dotnet:4 AS base
WORKDIR /home/site/wwwroot
EXPOSE 80

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src

# Auto copy to prevent 996
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This Function sets HTML template and send email notifications to blog administra

Tools | Alternative
--- | ---
[.NET 6.0 SDK](http://dot.net) | N/A
[.NET 8.0 SDK](http://dot.net) | N/A
[Visual Studio 2022](https://visualstudio.microsoft.com/) with Azure Development payload| [Visual Studio Code](https://code.visualstudio.com/)
[Azure Key Vault](https://azure.microsoft.com/en-us/services/key-vault/) | N/A
[Azure CLI](https://docs.microsoft.com/en-us/cli/azure/?view=azure-cli-latest) | N/A
Expand Down Expand Up @@ -60,14 +60,15 @@ Sample ```local.settings.json``` file
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
"moongladestorage": "<storage account connection string>",
"EnableSsl": true,
"SmtpServerPort": 587,
"AdminEmail": "edi.wang@outlook.com",
"EmailDisplayName": "Moonglade Notification Azure Function (local)",
"SmtpServer": "smtp-mail.outlook.com",
"SmtpUserName": "edi.wang@outlook.com",
"EmailAccountPassword": "**********"
"EmailAccountPassword": "<smtp password>"
}
}
```
Expand Down
8 changes: 4 additions & 4 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<PropertyGroup>
<Authors>Edi Wang</Authors>
<Company>edi.wang</Company>
<Copyright>(C) 2022 edi.wang@outlook.com</Copyright>
<AssemblyVersion>12.5.0.0</AssemblyVersion>
<FileVersion>12.5.0.0</FileVersion>
<Version>12.5.0.0</Version>
<Copyright>(C) 2024 edi.wang@outlook.com</Copyright>
<AssemblyVersion>14.4.0.0</AssemblyVersion>
<FileVersion>14.4.0.0</FileVersion>
<Version>14.4.0.0</Version>
</PropertyGroup>
</Project>
35 changes: 17 additions & 18 deletions src/Moonglade.Function.Email/Enqueue.cs
Original file line number Diff line number Diff line change
@@ -1,47 +1,46 @@
using Azure.Storage.Queues;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using Azure.Storage.Queues;
using System.Text;
using Newtonsoft.Json;
using System.Text.Json;
using FromBodyAttribute = Microsoft.Azure.Functions.Worker.Http.FromBodyAttribute;

namespace Moonglade.Function.Email;

public static class Enqueue
public class Enqueue(ILogger<Enqueue> logger)
{
[FunctionName("Enqueue")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] EnqueueRequest req,
ILogger log)
[Function("Enqueue")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
[FromBody] EnqueueRequest payload)
{
log.LogInformation($"C# HTTP trigger function `Enqueue` processed a request. OriginAspNetRequestId={req.OriginAspNetRequestId}");
logger.LogInformation($"C# HTTP trigger function `Enqueue` processed a request. OriginAspNetRequestId={payload.OriginAspNetRequestId}");

var conn = Environment.GetEnvironmentVariable("moongladestorage");
var queue = new QueueClient(conn, "moongladeemailqueue");

var en = new EmailNotification
{
DistributionList = string.Join(';', req.Receipts),
MessageType = req.Type,

// System.Text.Json doesn't work here because Azure Function model binding is already using Newtonsoft.Json, wtf..
MessageBody = JsonConvert.SerializeObject(req.Payload)
DistributionList = string.Join(';', payload.Receipts),
MessageType = payload.Type,
MessageBody = JsonSerializer.Serialize(payload.Payload, MoongladeJsonSerializerOptions.Default)
};

await InsertMessageAsync(queue, en, log);
await InsertMessageAsync(queue, en);

return new AcceptedResult();
}

private static async Task InsertMessageAsync(QueueClient queue, EmailNotification emailNotification, ILogger logger)
private async Task InsertMessageAsync(QueueClient queue, EmailNotification emailNotification)
{
if (null != await queue.CreateIfNotExistsAsync())
{
logger.LogInformation($"Azure Storage Queue '{queue.Name}' was created.");
}

var json = JsonConvert.SerializeObject(emailNotification);
var json = JsonSerializer.Serialize(emailNotification, MoongladeJsonSerializerOptions.Default);
var bytes = Encoding.UTF8.GetBytes(json);
var base64Json = Convert.ToBase64String(bytes);

Expand Down
90 changes: 52 additions & 38 deletions src/Moonglade.Function.Email/Moonglade.Function.Email.csproj
Original file line number Diff line number Diff line change
@@ -1,39 +1,53 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>14.3.1</Version>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://edi.wang</PackageProjectUrl>
<RepositoryUrl>https://github.com/EdiWang/Moonglade.Notification</RepositoryUrl>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<None Remove="mailConfiguration.xml" />
</ItemGroup>
<ItemGroup>
<Content Include="mailConfiguration.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="5.2.2" />
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ApplicationInsights" Version="1.0.0-preview4" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.3.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
<PackageReference Include="Edi.TemplateEmail" Version="2023.4.2" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
<None Update="mailConfiguration.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://edi.wang</PackageProjectUrl>
<RepositoryUrl>https://github.com/EdiWang/Moonglade.Email</RepositoryUrl>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
<OutputType>Exe</OutputType>
</PropertyGroup>
<ItemGroup>
<None Remove="mailConfiguration.xml" />
</ItemGroup>
<ItemGroup>
<Content Include="mailConfiguration.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />

<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.21.0" />

<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Storage.Queues" Version="5.3.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Timer" Version="4.3.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.2" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.2.1" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.2.0" />

<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="5.2.2" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ApplicationInsights" Version="1.0.0-preview4" />

<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />

<PackageReference Include="Edi.TemplateEmail" Version="2023.4.2" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
<None Update="mailConfiguration.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" />
</ItemGroup>
</Project>
14 changes: 14 additions & 0 deletions src/Moonglade.Function.Email/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

var host = new HostBuilder()
.ConfigureFunctionsWebApplication()
.ConfigureServices(services =>
{
services.AddApplicationInsightsTelemetryWorkerService();
services.ConfigureFunctionsApplicationInsights();
})
.Build();

host.Run();
Loading

0 comments on commit 0514e36

Please sign in to comment.