在Azure Webjobs中使用MobileServiceContext时抛出System.NullReferenceException

时间:2017-06-04 11:30:51

标签: c# asp.net .net azure azure-mobile-services

我在Azure App Service中使用移动应用程序并且正在运行webjob。问题是webjob中发生错误。

Microsoft.Azure.WebJobs.Host.FunctionInvocationException: Microsoft.Azure.WebJobs.Host.FunctionInvocationException: Exception while executing function: Functions.ProcessQueueMessage ---> System.NullReferenceException: Object reference not set to an instance of an object. at WebJob_CreateThumbnail.Functions.ProcessQueueMessage(Thumbnail thumbnail, TextWriter log) in d:\a\1\s\testdev\WebJob-CreateThumbnail\Functions.cs:line 35 at lambda_method(Closure , Functions , Object[] ) at Microsoft.Azure.WebJobs.Host.Executors.VoidMethodInvoker`1.InvokeAsync(TReflected instance, Object[] arguments) at Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker`1.<InvokeAsync>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.<ExecuteWithWatchersAsync>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.<ExecuteWithLoggingAsync>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.<ExecuteWithLoggingAsync>d__13.MoveNext() --- End of inner exception stack trace --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.<ExecuteWithLoggingAsync>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.<TryExecuteAsync>d__1.MoveNext()

几乎所有时候都会发生错误,但有时webjob可以正常运行而无需更改任何代码。 此外,当错误发生时,&#34; Functions.cs:第35行&#34;第35行是使用(MobileServiceContext context = new MobileServiceContext())&#34;在以下代码中。

public static void ProcessQueueMessage([QueueTrigger("createthumbnailqueue")]  Thumbnail thumbnail, TextWriter log)
{
    log.WriteLine("userId :{0} + thumbnailURL : {1}", thumbnail.Id, thumbnail.ThumbnailUrl);
    Uri thumbnailUri = new Uri(thumbnail.ThumbnailUrl);
    if (thumbnailUri.Host == _storageURLHost)
    {
            using (MobileServiceContext context = new MobileServiceContext())
                {
…

MobileServiceContext是移动应用模板项目中的默认上下文,并从webjob引用。 MobileServiceContext如下。

namespace test.Models
{
    public class MobileServiceContext : DbContext, IMobileServiceContext
    {
        private const string connectionStringName = "Name=MS_TableConnectionString";

        public MobileServiceContext() : base(connectionStringName)
        {
        }

        public DbSet<Comment> Comments { get; set; }
        public DbSet<Movie> Movies { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Add(
                new AttributeToColumnAnnotationConvention<TableColumnAttribute, string>(
                    "ServiceTableColumn", (property, attributes) => attributes.Single().ColumnType.ToString()));
        }
    }
}

有没有人告诉我为什么有时会出现这种错误以及如何修复它?

0 个答案:

没有答案