请不要将此标记为重复,因为这似乎与其他堆栈溢出问题不同。
我的代码看起来很完美,也可以按预期工作,但有时会引发异常。这是代码
try
{
using (var db = new ClubrContext())
{
var notificationService = new PushNotificationService(_logger);
var sendMailService = new SendMailService(_logger);
var smsService = new SMSService(_logger, sendMailService);
var reminderService = new GuestQuestionReminderService(notificationService, _logger, db, smsService);
await reminderService.SendReminders();
}
}
catch (Exception ex)
{
}
SendReminder 函数的代码如下所示 -
public class GuestQuestionReminderService : BaseReminderService
{
public GuestQuestionReminderService(IPushNotificationService notificationService, ILoggerService loggerService, ClubrContext dbContext, ISMSService smsService) :
base(notificationService, loggerService, dbContext, smsService)
{
}
public async Task SendReminders()
{
try
{
var groups = DbContext.Groups
.Include("Messages")
.Include("Admin")
.Include("Admin.Profile")
.Include("Promoter")
.Include("Promoter.Profile")
.Where(x => !x.Deleted && x.PartyDate >= DateTime.Today && x.Offers.Any() && x.Offers.Any(offer => offer.IsAccepted && !offer.Deleted))
.ToList();
foreach (var group in groups)
{
var offer = group.Offers.FirstOrDefault(x => x.IsAccepted && !x.Deleted);
var lastPromoterMessageDate = offer.Messages.OrderByDescending(x => x.MessageDate).FirstOrDefault(x => x.FromUserId == group.PromoterId)?.MessageDate;
lastPromoterMessageDate = lastPromoterMessageDate ?? DateTime.MinValue;
var recentAdminMessages = offer.Messages.Where(x => x.FromUserId == group.AdminId && x.MessageDate > lastPromoterMessageDate).ToList();
}
}
catch (Exception ex)
{
LoggerService.Error(ex, ex.StackTrace);
}
}
}
乍一看,一切似乎都是正确的。所有对数据库的调用都在 using 语句中,但抛出异常!
我在一个论坛上读到这可能是因为连接字符串不正确,但我不确定这是否正确。仍然在这里提供连接字符串 -
Data Source=[Data Source];Initial Catalog=[Database];User ID=[User Id];Password=[Password];MultipleActiveResultSets=true;
这是异常堆栈跟踪 -
System.InvalidOperationException: The operation cannot be completed because the DbContext has been disposed.
System.InvalidOperationException: The operation cannot be completed because the DbContext has been disposed.
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()at System.Data.Entity.Internal.LazyInternalContext.GetObjectContextWithoutDatabaseInitialization()
at System.Data.Entity.Internal.InternalContext.UpdateEntitySetMappingsForType(Type entityType)
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.Include(String path)
at System.Data.Entity.Infrastructure.DbQuery`1.Include(String path)
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.LazyInternalContext.GetObjectContextWithoutDatabaseInitialization()
at System.Data.Entity.Internal.InternalContext.UpdateEntitySetMappingsForType(Type entityType)
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.Include(String path)
at System.Data.Entity.Infrastructure.DbQuery`1.Include(String path)
at Clubr.WebJobs.ClubListingSchedule.Services.GuestQuestionReminderService.<SendReminders>d__1.MoveNext() in C:\sourcecode\clbr\Source\Clubr.WebJobs.ClubListingSchedule\Services\GuestQuestionReminderService.cs:line 24at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.LazyInternalContext.GetObjectContextWithoutDatabaseInitialization()
at System.Data.Entity.Internal.InternalContext.UpdateEntitySetMappingsForType(Type entityType)
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.Include(String path)
at System.Data.Entity.Infrastructure.DbQuery`1.Include(String path)
at Clubr.WebJobs.ClubListingSchedule.Services.GuestQuestionReminderService.<SendReminders>d__1.MoveNext() in C:\sourcecode\clbr\Source\Clubr.WebJobs.ClubListingSchedule\Services\GuestQuestionReminderService.cs:line 24 at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.LazyInternalContext.GetObjectContextWithoutDatabaseInitialization()
at System.Data.Entity.Internal.InternalContext.UpdateEntitySetMappingsForType(Type entityType)
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.Include(String path)
at System.Data.Entity.Infrastructure.DbQuery`1.Include(String path)
at Clubr.WebJobs.ClubListingSchedule.Services.GuestQuestionReminderService.<SendReminders>d__1.MoveNext() in C:\sourcecode\clbr\Source\Clubr.WebJobs.ClubListingSchedule\Services\GuestQuestionReminderService.cs:line 24
和 BaseService 代码 -
public class BaseReminderService
{
protected static IPushNotificationService NotificationService;
protected static ILoggerService LoggerService;
protected static ISMSService SmsService;
protected static ClubrContext DbContext;
public BaseReminderService(IPushNotificationService notificationService, ILoggerService loggerService, ClubrContext dbContext,
ISMSService smsService)
{
NotificationService = notificationService;
LoggerService = loggerService;
DbContext = dbContext;
SmsService = smsService;
}
}`
现在看BaseService的代码,似乎问题是因为使用了静态变量!
我保持问题开放,所以我知道我的想法是正确的。(另外,如果你能回答,因为它是一个静态变量,它是否在可能并行运行的多个进程/线程之间共享!