azure-cosmos-dotnet-repository
azure-cosmos-dotnet-repository copied to clipboard
Object reference not set to an instance of an object - Microsoft.Azure.Cosmos.Linq.SubtreeEvaluator.EvaluateMemberAccess
I am getting below exception when using predicate(as shown below), but same code working fine when I am running app on localhost.
private async Task<EmailTemplate> GetEmailTemplateAsync(UserSubscriptions userSubscriptions, InvoiceDownloadEvent @event, ILogger logger)
{
EmailTemplate emailTemplate = null;
Expression<Func<EmailTemplate, bool>> predicate = (x) => x.NotificationType == (int)@event.EventType.ToEnum<EventType>()
&& x.NotificationSubType == String.Empty;
if (!string.IsNullOrEmpty(userSubscriptions.PreferredLanguage))
{
logger.LogInformation($"UUID:{userSubscriptions.UUID} preferred language: {userSubscriptions.PreferredLanguage}");
predicate = predicate.And(x => x.Language == userSubscriptions.PreferredLanguage);
}
else
{
logger.LogInformation($"UUID's preferred language is null," +
$" retrieving email template with default language: {_emailConfigOptions.Value.DefaultPreferredLanguage}");
predicate = predicate.And(x => x.Language == _emailConfigOptions.Value.DefaultPreferredLanguage);
}
var emailTemplateList = await _emailTemplateRepository.GetAsync(predicate);
if (emailTemplateList.Any())
{
emailTemplate = emailTemplateList.FirstOrDefault();
logger.LogInformation($"Template ID:{emailTemplate?.TemplateId} found for," +
$" NotificationType:{(int)EventType.INVOICE_DOWNLOAD}," +
$" PreferredLanguage: {userSubscriptions.PreferredLanguage}");
}
else
{
logger.LogWarning($"No email template found for {@event.EventType}");
}
return emailTemplate;
}
Below is the stack trace details:
Exception Message:Object reference not set to an instance of an object., StackTrace: at Microsoft.Azure.Cosmos.Linq.SubtreeEvaluator.EvaluateMemberAccess(Expression expression)at Microsoft.Azure.Cosmos.Linq.SubtreeEvaluator.EvaluateMemberAccess(Expression expression)at Microsoft.Azure.Cosmos.Linq.SubtreeEvaluator.EvaluateConstant(Expression expression)at Microsoft.Azure.Cosmos.Linq.SubtreeEvaluator.Visit(Expression expression)at System.Linq.Expressions.ExpressionVisitor.VisitBinary(BinaryExpression node)at System.Linq.Expressions.BinaryExpression.Accept(ExpressionVisitor visitor)at Microsoft.Azure.Cosmos.Linq.SubtreeEvaluator.Visit(Expression expression)at System.Linq.Expressions.ExpressionVisitor.VisitBinary(BinaryExpression node)at System.Linq.Expressions.BinaryExpression.Accept(ExpressionVisitor visitor)at Microsoft.Azure.Cosmos.Linq.SubtreeEvaluator.Visit(Expression expression)at System.Linq.Expressions.ExpressionVisitor.VisitBinary(BinaryExpression node)at System.Linq.Expressions.BinaryExpression.Accept(ExpressionVisitor visitor)at Microsoft.Azure.Cosmos.Linq.SubtreeEvaluator.Visit(Expression expression)at System.Linq.Expressions.ExpressionVisitor.VisitBinary(BinaryExpression node)at System.Linq.Expressions.BinaryExpression.Accept(ExpressionVisitor visitor)at Microsoft.Azure.Cosmos.Linq.SubtreeEvaluator.Visit(Expression expression)at System.Linq.Expressions.ExpressionVisitor.VisitLambda[T](Expression1 node)at System.Linq.Expressions.Expression
1.Accept(ExpressionVisitor visitor)at Microsoft.Azure.Cosmos.Linq.SubtreeEvaluator.Visit(Expression expression)at System.Linq.Expressions.ExpressionVisitor.VisitUnary(UnaryExpression node)at System.Linq.Expressions.UnaryExpression.Accept(ExpressionVisitor visitor)at Microsoft.Azure.Cosmos.Linq.SubtreeEvaluator.Visit(Expression expression)at System.Dynamic.Utils.ExpressionVisitorUtils.VisitArguments(ExpressionVisitor visitor, IArgumentProvider nodes)at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node)at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)at Microsoft.Azure.Cosmos.Linq.SubtreeEvaluator.Visit(Expression expression)at Microsoft.Azure.Cosmos.Linq.ConstantEvaluator.PartialEval(Expression expression, Func2 fnCanBeEvaluated)at Microsoft.Azure.Cosmos.Linq.ConstantEvaluator.PartialEval(Expression expression)at Microsoft.Azure.Cosmos.Linq.SqlTranslator.TranslateQuery(Expression inputExpression, CosmosLinqSerializerOptions linqSerializerOptions, IDictionary
2 parameters)at Microsoft.Azure.Cosmos.Linq.DocumentQueryEvaluator.HandleMethodCallExpression(MethodCallExpression expression, IDictionary2 parameters, CosmosLinqSerializerOptions linqSerializerOptions)at Microsoft.Azure.Cosmos.Linq.DocumentQueryEvaluator.Evaluate(Expression expression, CosmosLinqSerializerOptions linqSerializerOptions, IDictionary
2 parameters)at Microsoft.Azure.Cosmos.Linq.CosmosLinqQuery1.ToString()at Microsoft.Azure.CosmosRepository.Logging.LoggerExtensions.LogQueryConstructed[TItem](ILogger logger, IQueryable
1 queryable)at Microsoft.Azure.CosmosRepository.DefaultRepository1.GetAsync(Expression
1 predicate, CancellationToken cancellationToken)at SFH.N3.InvoiceEventSubscriber.Processors.InvoiceDownload.InvoiceDownloadEmailNotificationPublisher.GetEmailTemplateAsync(UserSubscriptions userSubscriptions, InvoiceDownloadEvent event, ILogger logger) in C:\notification-subscribers\dev\Notification-Microservices\SFH.N3.InvoiceEventSubscriber\SFH.N3.InvoiceEventSubscriber\Processors\InvoiceDownload\InvoiceDownloadEmailNotificationPublisher.cs:line 214at SFH.N3.InvoiceEventSubscriber.Processors.InvoiceDownload.InvoiceDownloadEmailNotificationPublisher.PublishEmailAsync(IEnumerable`1 userSubscriptions, InvoiceDownloadEvent event) in C:\notification-subscribers\dev\Notification-Microservices\SFH.N3.InvoiceEventSubscriber\SFH.N3.InvoiceEventSubscriber\Processors\InvoiceDownload\InvoiceDownloadEmailNotificationPublisher.cs:line 79at SFH.N3.InvoiceEventSubscriber.Processors.InvoiceDownload.InvoiceDownloadEventProcessor.ProcessAsync(InvoiceDownloadEvent event) in C:\notification-subscribers\dev\Notification-Microservices\SFH.N3.InvoiceEventSubscriber\SFH.N3.InvoiceEventSubscriber\Processors\InvoiceDownload\InvoiceDownloadEventProcessor.cs:line 85at SFH.N3.InvoiceEventSubscriber.Functions.InvoiceDownloadEventSubscriber.RunAsync(String sbMsg) in C:\notification-subscribers\dev\Notification-Microservices\SFH.N3.InvoiceEventSubscriber\SFH.N3.InvoiceEventSubscriber\Functions\InvoiceDownloadEventSubscriber.cs:line 66
2022-07-28T13:05:14.711 [Error] Executed 'InvoiceDownloadEventSubscriber' (Failed, Id=2deed929-26ae-48c1-8980-8b88678ee174, Duration=211ms)Object reference not set to an instance of an object.
2022-07-28T13:05:14.744 [Error] Message processing error (Action=UserCallback, ClientId=MessageReceiver2EventTypes/Subscriptions/InvoiceDownloadEventSubscriber, EntityPath=EventTypes/Subscriptions/InvoiceDownloadEventSubscriber, Endpoint=sb-fleethubnotifications-dev.servicebus.windows.net)Object reference not set to an instance of an object
The most likely candidate is that your expression cannot be evaluated on the server. Instead of your expression calling .ToEnum<TEnum>
you might consider simply getting that value ahead of time, and passing the int
value instead. Your localhost
knows what that extension is, but the Cosmos DB server doesn't. For example:
int eventType = (int)@event.EventType.ToEnum<EventType>();
Expression<Func<EmailTemplate, bool>> predicate =
x => x.NotificationType == eventType
&& x.NotificationSubType == string.Empty;
Try that instead.
Thanks! @IEvangelist , I have changed my code as you suggested and now it started working.