gpt4 book ai didi

azure - Microsoft.Azure.WebJobs.Host.FunctionInitationException

转载 作者:行者123 更新时间:2023-12-03 04:27:04 30 4
gpt4 key购买 nike

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

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

我已经为我的所有服务设置了 Bind 语句。服务调用存储库。我还需要绑定(bind)存储库吗?

当我运行 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.FunctionInvoker
1.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 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 绑定(bind):

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: Noparameterless constructor Defined for this object”,看来您在主函数中缺少初始化JobHost的参数配置。

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

JobHost  host = new JobHost(config)

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

enter image description here

以下是我的详细步骤:

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

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

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

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

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

enter image description here

关于azure - Microsoft.Azure.WebJobs.Host.FunctionInitationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41990085/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com