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 externally supplied worker extensions project #2763

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion samples/FunctionApp/FunctionApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.18.1" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="$(SdkVersion)" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.Net.NameResolution" Version="4.3.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Abstractions" Version="1.3.0" />
Expand Down
14 changes: 5 additions & 9 deletions sdk/Sdk/ExtensionsCsprojGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ namespace Microsoft.Azure.Functions.Worker.Sdk
{
internal class ExtensionsCsprojGenerator
{
internal const string ExtensionsProjectName = "WorkerExtensions.csproj";

private readonly IDictionary<string, string> _extensions;
private readonly string _outputPath;
private readonly string _targetFrameworkIdentifier;
Expand All @@ -29,22 +27,20 @@ public ExtensionsCsprojGenerator(IDictionary<string, string> extensions, string

public void Generate()
{
var extensionsCsprojFilePath = Path.Combine(_outputPath, ExtensionsProjectName);

string csproj = GetCsProjContent();
if (File.Exists(extensionsCsprojFilePath))
if (File.Exists(_outputPath))
{
string existing = File.ReadAllText(extensionsCsprojFilePath);
string existing = File.ReadAllText(_outputPath);
if (string.Equals(csproj, existing, StringComparison.Ordinal))
{
// If contents are the same, only touch the file to update timestamp.
File.SetLastWriteTimeUtc(extensionsCsprojFilePath, DateTime.UtcNow);
File.SetLastWriteTimeUtc(_outputPath, DateTime.UtcNow);
return;
}
}

RecreateDirectory(_outputPath);
File.WriteAllText(extensionsCsprojFilePath, csproj);
RecreateDirectory(Path.GetDirectoryName(_outputPath));
File.WriteAllText(_outputPath, csproj);
}

private void RecreateDirectory(string directoryPath)
Expand Down
4 changes: 2 additions & 2 deletions sdk/Sdk/ExtensionsMetadata.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System.Collections.Generic;
Expand All @@ -9,6 +9,6 @@ namespace Microsoft.Azure.Functions.Worker.Sdk
public class ExtensionsMetadata
{
[JsonPropertyName("extensions")]
public IEnumerable<ExtensionReference>? Extensions { get; set; }
public List<ExtensionReference> Extensions { get; set; } = new List<ExtensionReference>();
}
}
60 changes: 60 additions & 0 deletions sdk/Sdk/ExtensionsMetadataEnhancer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
// Licensed under the MIT License. See License.txt in the project root for license information.

using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using Mono.Cecil;

namespace Microsoft.Azure.Functions.Worker.Sdk
{
Expand All @@ -25,6 +29,34 @@ public static void AddHintPath(IEnumerable<ExtensionReference> extensions)
}
}

public static IEnumerable<ExtensionReference> GetWebJobsExtensions(string fileName)
{
// NOTE: this is an incomplete approach to getting extensions and is intended only for our usages.
// Running this with arbitrary assemblies (especially user supplied) can lead to exceptions.
AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(fileName);
IEnumerable<CustomAttribute> attributes = assembly.Modules.SelectMany(p => p.GetCustomAttributes())
.Where(a => a.AttributeType.FullName == "Microsoft.Azure.WebJobs.Hosting.WebJobsStartupAttribute");

foreach (CustomAttribute attribute in attributes)
{
CustomAttributeArgument typeProperty = attribute.ConstructorArguments.ElementAtOrDefault(0);
CustomAttributeArgument nameProperty = attribute.ConstructorArguments.ElementAtOrDefault(1);

TypeDefinition typeDef = (TypeDefinition)typeProperty.Value;
string assemblyQualifiedName = Assembly.CreateQualifiedName(
typeDef.Module.Assembly.FullName, GetReflectionFullName(typeDef));

string name = GetName((string)nameProperty.Value, typeDef);

yield return new ExtensionReference
{
Name = name,
TypeName = assemblyQualifiedName,
HintPath = $@"{ExtensionsBinaryDirectoryPath}/{Path.GetFileName(fileName)}",
};
}
}

private static string? GetAssemblyNameOrNull(string? typeName)
{
if (typeName == null)
Expand All @@ -41,5 +73,33 @@ public static void AddHintPath(IEnumerable<ExtensionReference> extensions)

return null;
}

// Copying the WebJobsStartup constructor logic from:
// https://github.com/Azure/azure-webjobs-sdk/blob/e5417775bcb8c8d3d53698932ca8e4e265eac66d/src/Microsoft.Azure.WebJobs.Host/Hosting/WebJobsStartupAttribute.cs#L33-L47.
private static string GetName(string name, TypeDefinition startupTypeDef)
{
if (string.IsNullOrEmpty(name))
{
// for a startup class named 'CustomConfigWebJobsStartup' or 'CustomConfigStartup',
// default to a name 'CustomConfig'
name = startupTypeDef.Name;
int idx = name.IndexOf("WebJobsStartup");
if (idx < 0)
{
idx = name.IndexOf("Startup");
}
if (idx > 0)
{
name = name.Substring(0, idx);
}
}

return name;
}

private static string GetReflectionFullName(TypeReference typeRef)
{
return typeRef.FullName.Replace("/", "+");
}
}
}
Loading
Loading