.net – 如何在F#中声明可以通过WebJob的JobHost.CallAsync调用的函数?

在C#
WebJob中,我能够手动调用这样的公共静态类方法:

using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;

namespace foo
{
    public class Program
    {

        [NoAutomaticTrigger]
        public static void Go(TraceWriter log) { ... }

        static void Main()
        {
           var host = new JobHost();
           var methodInfo = typeof(Program).GetMethod("Go");
           host.Call(methodInfo);
           host.RunAndBlock();
        }

methodInfo是一个System.Reflection.MethodInfo,在调试器中我可以看到它有属性Public |静态| HideBySig和CustomAttributes Microsoft.Azure.WebJobs.NoAutomaticTriggerAttribute.

我想在F#中这样做.这是我到目前为止所拥有的:

type Foo() =

    [<NoAutomaticTrigger>]
    static member Go (log:TraceWriter) =
        log.Info "hello!"

[<EntryPoint>]
let main argv =
        let theType = typedefof<Foo>
        let methodInfo = theType.GetMethods() |> Seq.find(fun t -> t.Name = "Go")
        host.Call(methodInfo)
        host.RunAndBlock()

WebJobs运行时不喜欢它:

System.InvalidOperationException
  HResult=0x80131509
  Message='Void Go(Microsoft.Azure.WebJobs.Host.TraceWriter)' can't be invoked from Azure WebJobs SDK. Is it missing Azure WebJobs SDK attributes?
  Source=Microsoft.Azure.WebJobs.Host
  StackTrace:
   at Microsoft.Azure.WebJobs.JobHost.Validate(IFunctionDefinition function, Object key)
   at Microsoft.Azure.WebJobs.JobHost.<CallAsyncCore>d__37.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.WebJobs.JobHost.Call(MethodInfo method)
   at Program.main(String[] argv) in C:\path\to\project\Program.fs:line 110

我的F#methodInfo确实有NoAutomaticTrigger属性.它还有Public和Static,但它缺少HideBySig.那可能很重要吗?我应该比较MethodInfos的其他部分吗?

这是webjobs sdk:https://github.com/Azure/azure-webjobs-sdk/blob/v2.2.0/src/Microsoft.Azure.WebJobs.Host/JobHost.cs#L306的相关来源

为了它的价值,我已经能够成功使用F#中的TimerTrigger和ServiceBusTrigger;这只是我正在努力的手动调用模式.

接下来我打算筛选webjobs源代码并尝试弄清楚它正在寻找什么,但我希望有一些显而易见的人对F#和/或webjobs更有经验可以告诉我.

最佳答案 通过WebJobs源进行调试,我最终结束了

DefaultTypeLocator,它选择标记为
IsPublic的类.我尝试了我的F#声明,但似乎无法实现;我只设法生产
IsNestedPublic.

所以我尝试了另一种方法:我没有尝试编写现有WebJobs运行时可发现的F#函数,而是覆盖了发现逻辑:

type myTypeLocator() =
    interface ITypeLocator with
        member this.GetTypes () =
            new System.Collections.Generic.List<Type>([ typedefof<Foo> ]) :> IReadOnlyList<Type>

let config = new JobHostConfiguration (
                    DashboardConnectionString = dashboardConnectionString,
                    StorageConnectionString = storageConnectionString,
                    TypeLocator = new myTypeLocator()
                )

let host = new JobHost(config)

这很有用:我的功能被发现,我可以JobHost.Call他们.

点赞