Microsoft.Azure.WebJobs.Host.FunctionInvocationException

Microsoft.Azure.WebJobs.Host.FunctionInvocationException

我在 Azure WebJob 中收到此错误。知道是什么原因造成的吗?

我正在使用 Ninject 在我的 Azure WebJobs 控制台应用程序中处理 DI。

我已经为我的所有服务设置了绑定语句。服务调用存储库。我是否也需要绑定存储库?

当我 运行 WebJob 时,它从队列中提取消息但失败并显示以下消息。我认为这与 Ninject.

有关

Microsoft.Azure.WebJobs.Host.FunctionInvocationException: Microsoft.Azure.WebJobs.Host.FunctionInvocationException: Exception while executing function: Functions.ProcessQueueMessage ---> System.MissingMethodException: No parameterless constructor defined for this object. at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) at System.Activator.CreateInstanceT at Microsoft.Azure.WebJobs.Host.Executors.DefaultJobActivator.CreateInstanceT at Microsoft.Azure.WebJobs.Host.Executors.ActivatorInstanceFactory1.Create() at Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker1.d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.d__31.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.d__2c.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task) at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.d__13.MoveNext() --- End of inner exception stack trace --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.d__13.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.d__1.MoveNext()

这是程序 class:

class Program
{
   static readonly IKernel Kernel = new StandardKernel();
   static JobHostConfiguration config;

   static void Main()
   {
      BootStrapIoc();
      var host = new JobHost();

      // The following code ensures that the WebJob will be running continuously
      host.RunAndBlock();
    }

    private static void BootStrapIoc()
    {
       Kernel.Load(Assembly.GetExecutingAssembly());
       config = new JobHostConfiguration
       {
          JobActivator = new MyJobActivator(Kernel)
       };
     }
}

这是 MyJobActivator

public class BrmJobActivator : IJobActivator
{
   private readonly IKernel _container;

   public MyJobActivator(IKernel container)
   {
      _container = container;
   }

   public T CreateInstance<T>()
   {
      return _container.Get<T>();
   }
}

这是 Ninject 绑定:

public class NinjectBindings : Ninject.Modules.NinjectModule
    {
        public override void Load()
        {
            Bind<IConfiguration>().ToMethod(ctx => {
                var builder = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json");
                IConfigurationRoot Configuration = builder.Build();
                return Configuration;
            });

            Bind<IAccountsServices>().To<AccountsServices>();
            Bind<IBlogServices>().To<BlogServices>();

            // Bind<IAccountsRepository>().To<AccountsRepository>();
            // Bind<IBlogsRepository>().To<BlogsRepository>();
        }
    }

根据错误信息“System.MissingMethodException: No parameterless constructor defined for this object”,您似乎错过了主函数中用于初始化 JobHost 的参数配置。

如果是这种情况,请在main函数中使用如下代码。它工作正常。

JobHost  host = new JobHost(config)

虽然没有 config 参数来初始化 JobHost,但我收到了相同的错误消息。

以下是我的详细步骤:

在main函数中,修改代码如下:

IKernel Kernel = new StandardKernel();
Kernel.Load(Assembly.GetExecutingAssembly());
var config = new JobHostConfiguration
{
     JobActivator = new MyJobActivator(Kernel)
};
var host = new JobHost(config);
host.RunAndBlock();

我使用您提供的代码创建了演示,并将 class 名称 BrmJobActivator 更改为 MyJobActivator。在 NinJect Bindings.cs 加载函数中,只需保持以下代码:

  Bind<IAccountsServices>().To<AccountsServices>();
  Bind<IBlogServices>().To<BlogServices>();

然后它对我来说工作正常。