Skip to content

Commit 57a3ae0

Browse files
committed
Enabling instance function methods
1 parent 00e62f4 commit 57a3ae0

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

src/WebJobs.Script/Description/DotNet/Compilation/Raw/RawAssemblyCompilation.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ public FunctionSignature GetEntryPointSignature(IFunctionEntryPointResolver entr
5151
throw new InvalidOperationException($"The function type name '{typeName}' is invalid.");
5252
}
5353

54-
MethodInfo method = functionType.GetMethod(methodName, BindingFlags.Static | BindingFlags.Public);
54+
// To preserve legacy behavior, we attempt to bind to public static methods first, then fallback to instance
55+
// methods if a match is not found.
56+
MethodInfo method = functionType.GetMethod(methodName, BindingFlags.Static | BindingFlags.Public)
57+
?? functionType.GetMethod(methodName, BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public);
5558
if (method == null)
5659
{
5760
throw new InvalidOperationException($"The method '{methodName}' cannot be found.");
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Text;
7+
using Microsoft.Azure.WebJobs.Script.Description;
8+
using Xunit;
9+
10+
namespace Microsoft.Azure.WebJobs.Script.Tests.Description.DotNet
11+
{
12+
public class RawAssemblyCompilationTests
13+
{
14+
[Theory]
15+
[MemberData(nameof(GetTargetNames))]
16+
public void GetEntryPointSignature_BindsToExpectedMethod(string entryPointName, string methodName)
17+
{
18+
var testAssembly = typeof(TestFunction1).Assembly;
19+
string assemblyPath = new Uri(testAssembly.CodeBase, UriKind.Absolute).LocalPath;
20+
var compilation = new RawAssemblyCompilation(assemblyPath, entryPointName);
21+
22+
FunctionSignature signature = compilation.GetEntryPointSignature(new FunctionEntryPointResolver(), testAssembly);
23+
24+
Assert.NotNull(signature);
25+
Assert.Equal(methodName, signature.MethodName);
26+
}
27+
28+
[Fact]
29+
public void GetEntryPointSignature_PrefersStaticMethod()
30+
{
31+
var testAssembly = typeof(TestFunction1).Assembly;
32+
string assemblyPath = new Uri(testAssembly.CodeBase, UriKind.Absolute).LocalPath;
33+
var compilation = new RawAssemblyCompilation(assemblyPath, $"{typeof(TestFunction3).FullName}.{nameof(TestFunction3.Run)}");
34+
35+
FunctionSignature signature = compilation.GetEntryPointSignature(new FunctionEntryPointResolver(), testAssembly);
36+
37+
Assert.NotNull(signature);
38+
Assert.Equal(nameof(TestFunction3.Run), signature.MethodName);
39+
}
40+
41+
public static IEnumerable<object[]> GetTargetNames()
42+
{
43+
return new[]
44+
{
45+
new[] { $"{typeof(TestFunction1).FullName}.{nameof(TestFunction1.Run)}", nameof(TestFunction1.Run) },
46+
new[] { $"{typeof(TestFunction2).FullName}.{nameof(TestFunction2.Run)}", nameof(TestFunction2.Run) }
47+
};
48+
}
49+
}
50+
51+
public class TestFunction1
52+
{
53+
public void Run() { }
54+
}
55+
56+
public class TestFunction2
57+
{
58+
public static void Run() { }
59+
}
60+
61+
public class TestFunction3
62+
{
63+
public void Run() { }
64+
65+
public static void Run(string test) { }
66+
}
67+
}

0 commit comments

Comments
 (0)