我有一个控制台应用程序作为我的网络作业来处理我的应用程序内的通知。使用队列触发进程。应用程序使用实体框架 6 与 SQL Azure 数据库交互。被调用的 Process() 方法读取/写入数据库中的数据。
处理队列消息时出现几个错误。它们永远不会进入毒物队列,因为它们在 2-3 次后被成功重新处理。主要错误如下:
mscorlib.dll 中出现“System.StackOverflowException”类型的未处理异常
错误:System.OutOfMemoryException:引发了“System.OutOfMemoryException”类型的异常。
默认批量大小为 16,因此消息是并行处理的。
我的猜测是用于并行处理消息的 Ninject 设置是错误的。因此,当它们同时处理时,会出现一些错误,最终处理成功。
我的问题是:这个设置看起来好吗?我应该使用 InThreadScope() 可能是因为我不知道并行处理也是多线程的。
这是我的应用程序的代码。
程序.cs
namespace NotificationsProcessor
{
public class Program
{
private static StandardKernel _kernel;
private static void Main(string[] args)
{
var module = new CustomModule();
var kernel = new StandardKernel(module);
_kernel = kernel;
var config =
new JobHostConfiguration(AzureStorageAccount.ConnectionString)
{
NameResolver = new QueueNameResolver()
};
var host = new JobHost(config);
//config.Queues.BatchSize = 1; //Process messages in parallel
host.RunAndBlock();
}
public static void ProcessNotification([QueueTrigger("%notificationsQueueKey%")] string item)
{
var n = _kernel.Get<INotifications>();
n.Process(item);
}
public static void ProcessPoison([QueueTrigger("%notificationsQueueKeyPoison%")] string item)
{
//Process poison message.
}
}
}
这是 Ninject 的 CustomModule 的代码
namespace NotificationsProcessor.NinjectFiles
{
public class CustomModule : NinjectModule
{
public override void Load()
{
Bind<IDbContext>().To<DataContext>(); //EF datacontext
Bind<INotifications>().To<NotificationsService>();
Bind<IEmails>().To<EmailsService>();
Bind<ISms>().ToSmsService>();
}
}
}
处理方法的代码。
public void ProcessMessage(string message)
{
try
{
var notificationQueueMessage = JsonConvert.DeserializeObject<NotificationQueueMessage>(message);
//Grab message and check if it has to be processed
var notification = _context.Set().Find(notificationQueueMessage.NotificationId);
if (notification != null)
{
if (notification.NotificationType == NotificationType.AppointmentReminder.ToString())
{
notificationSuccess = SendAppointmentReminderEmail(notification); //Code that sends email using the SendGrid Api
}
}
}
catch (Exception ex)
{
_logger.LogError(ex + Environment.NewLine + message, LogSources.EmailsService);
throw;
}
}
更新 - 添加了异常
Json 序列化程序抛出异常。这是堆栈跟踪:
错误:System.OutOfMemoryException:引发了“System.OutOfMemoryException”类型的异常。在 System.String.CtorCharCount(Char c, Int32 count) 在 Newtonsoft.Json.JsonWriter.AutoCompleteClose(JsonContainerType type) 在 Newtonsoft.Json.JsonWriter.WriteEndObject() 在 Newtonsoft.Json 的 Newtonsoft.Json.JsonTextWriter.WriteIndent()。 JsonWriter.WriteEnd(JsonContainerType type) 在 Newtonsoft.Json.JsonWriter.WriteEnd() 在 Newtonsoft.Json.JsonWriter.AutoCompleteAll() 在 Newtonsoft.Json.JsonTextWriter.Close() 在 Newtonsoft.Json.JsonWriter.System.IDisposable.Dispose( )在 Newtonsoft.Json.JsonConvert.SerializeObjectInternal(对象值,类型类型,JsonSerializer jsonSerializer)在 Newtonsoft.Json.JsonConvert.SerializeObject(对象值,类型类型,格式化格式,JsonSerializerSettings 设置)在 Core.Services.Communications.EmailsService。