Azure上的WebJob失败,但本地运行正常
我正在本地运行一个WebJob--仍然连接到Azure上的同一存储服务--但是当我在Azure上发布它时,它失败了 它在本地也出现了同样的错误,但我能够修复它,并在我的本地开发机器上完美地运行它。知道为什么它会在Azure上失败吗 重申这一点,即使它在本地运行,它仍然连接到Azure上相同的队列和存储帐户。所以在本地运行的唯一东西就是代码 以下是我在Azure Portal的WebJob日志下发现的错误: Microsoft.Azure.WebJobs.Host.FunctionInvocationException: Microsoft.Azure.WebJobs.Host.FunctionInvocationException:异常 执行函数时:Functions.ProcessQueueMessage--> System.MissingMethodException:未定义无参数构造函数 对于这个对象。在 System.RuntimeTypeHandle.CreateInstance(RuntimeType类型,布尔值 publicOnly、Boolean noCheck、Boolean&canBeCached、, RuntimeMethodHandleInternal&ctor、Boolean&bNeedSecurityCheck)位于 System.RuntimeType.CreateInstanceSlow(布尔publicOnly,布尔型 skipCheckThis、布尔填充缓存、堆栈爬网标记和堆栈标记)位于 System.RuntimeType.CreateInstanceDefaultCtor(仅限布尔值), 布尔skipCheckThis、布尔填充缓存、堆栈爬网标记和堆栈标记) 在System.Activator.CreateInstanceT处 Microsoft.Azure.WebJobs.Host.Executors.DefaultJobActivator.CreateInstanceT 在 Microsoft.Azure.WebJobs.Host.Executors.ActivatorInstanceFactoryAzure上的WebJob失败,但本地运行正常,azure,azure-webjobs,azure-webjobssdk,Azure,Azure Webjobs,Azure Webjobssdk,我正在本地运行一个WebJob--仍然连接到Azure上的同一存储服务--但是当我在Azure上发布它时,它失败了 它在本地也出现了同样的错误,但我能够修复它,并在我的本地开发机器上完美地运行它。知道为什么它会在Azure上失败吗 重申这一点,即使它在本地运行,它仍然连接到Azure上相同的队列和存储帐户。所以在本地运行的唯一东西就是代码 以下是我在Azure Portal的WebJob日志下发现的错误: Microsoft.Azure.WebJobs.Host.FunctionInvocati
1.Create()
在
Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker
1.d\u 0.MoveNext()
---来自引发异常的上一个位置的堆栈结束跟踪---在
System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccess(任务
任务)在
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务
任务)在
Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.d_u31.MoveNext()
---来自引发异常的上一个位置的堆栈结束跟踪---在
System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccess(任务
任务)在
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务
任务)在
Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.d__2c.MoveNext()
---来自引发异常的上一个位置的堆栈结束跟踪---在
System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccess(任务
任务)在
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务
任务)在System.Runtime.CompilerServices.TaskWaiter.ValidateEnd(任务
任务)在
Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.d_u13.MoveNext()
---内部异常堆栈跟踪的结束---在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()处
Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.d_u13.MoveNext()
---来自引发异常的上一个位置的堆栈结束跟踪---在
System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccess(任务
任务)在
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务
任务)在
Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.d_u1.MoveNext()
另外,我在VS 2015 Cloud->Azure WebJob中创建了WebJob。此外,它的目标是.NETFramework 4.6.2
还有一点很重要,我有一个appsettings.json文件,其中包含一些设置信息。这在bin目录中。我确实阅读了这个文件,并获得了一些代码中使用的设置。当我通过FTP进入WebJobs目录时,我在那里看到appsettings.json文件。不确定这个问题是否与此有关
更新:
以下是主要内容:
class Program
{
static readonly IKernel Kernel = new StandardKernel();
static JobHostConfiguration config;
static void Main()
{
BootStrapIoc();
var host = new JobHost(config);
host.RunAndBlock();
}
private static void BootStrapIoc()
{
Kernel.Load(Assembly.GetExecutingAssembly());
config = new JobHostConfiguration
{
JobActivator = new MyJobActivator(Kernel)
};
}
}
以下是MyJobActivator:
public class MyJobActivator : IJobActivator
{
private readonly IKernel _container;
public MyJobActivator(IKernel container)
{
_container = container;
}
public T CreateInstance<T>()
{
return _container.Get<T>();
}
}
你得到的例外是 没有为此对象定义无参数构造函数 实现队列处理方法的对象必须具有无参数构造函数,以便JobHost能够创建它(对不起,这是我在没有看到代码示例的情况下可以给出的最佳指针) Microsoft.Azure.WebJobs.Host.FunctionInvocationException:执行函数时异常:Functions.ProcessQueueMessage-->System.MissingMethodException:未为此对象定义无参数构造函数 根据您的描述,我在我这边测试了这个问题,根据您的代码,我可能会遇到相同的错误。经过一些试验,我可以让它在我这边和Azure上按预期工作。以下是一些可能的原因,您可以参考: Program.cs>Main
IKernel Kernel = new StandardKernel();
Kernel.Load(Assembly.GetExecutingAssembly());
var config = new JobHostConfiguration
{
JobActivator = new MyJobActivator(Kernel)
};
//Initializes a new instance of the Microsoft.Azure.WebJobs.JobHost class using the configuration provided.
var host = new JobHost(config);
host.RunAndBlock();
要加载appsettings.json
文件,可以按如下方式配置映射器:
Bind<IConfiguration>().ToMethod(ctx =>
{
var builder = new ConfigurationBuilder();
//set base path to the current working directory of your application
builder.SetBasePath(Directory.GetCurrentDirectory());
builder.AddJsonFile("appsettings.json");
IConfigurationRoot Configuration = builder.Build();
return Configuration;
});
Bind().ToMethod(ctx=>
{
var builder=new ConfigurationBuilder();
//将基本路径设置为应用程序的当前工作目录
SetBasePath(Directory.GetCurrentDirectory());
AddJsonFile(“appsettings.json”);
IConfigurationRoot配置=builder.Build();
返回配置;
});
注意:确保appsettings.json
位于项目的根目录下,并将“复制到输出目录”设置为Copy always
我的测试结果:
刚刚在原始帖子中添加了一个更新,显示了我的函数类的外观。我只是想弄清楚。我想你是说函数类必须有无参数构造函数,对吗?第二,为何它在本地运作良好?如果我正在传递给构造函数的服务导致了问题,那么它是否也应该在本地失败?我非常感谢您的回复。我刚刚更新了原始帖子,向您展示了我在这个WebJob中的几乎所有代码。我已经有了你建议的大部分内容,但添加了缺少的部分。同样,该应用程序在本地运行良好,但在Azure上出现相同的错误消息。这可能是.NET framework b的问题吗
IKernel Kernel = new StandardKernel();
Kernel.Load(Assembly.GetExecutingAssembly());
var config = new JobHostConfiguration
{
JobActivator = new MyJobActivator(Kernel)
};
//Initializes a new instance of the Microsoft.Azure.WebJobs.JobHost class using the configuration provided.
var host = new JobHost(config);
host.RunAndBlock();
Bind<IConfiguration>().ToMethod(ctx =>
{
var builder = new ConfigurationBuilder();
//set base path to the current working directory of your application
builder.SetBasePath(Directory.GetCurrentDirectory());
builder.AddJsonFile("appsettings.json");
IConfigurationRoot Configuration = builder.Build();
return Configuration;
});