我遇到了类似的问题,但它解决了我的目的。我以两种方式使用相同的代码:1.直接通过按钮按下 2.通过计划任务。
通过方法 1 它工作正常,但方法 2 给出了该错误。我的代码:
服务 -
public virtual int SendCampaign(Campaign campaign, EmailAccount emailAccount,
IEnumerable<NewsLetterSubscription> subscriptions)
{
var campaignSubscriberTrack = new CampaignNewsletterSubscriberTrack();
if (campaign == null)
throw new ArgumentNullException("campaign");
if (emailAccount == null)
throw new ArgumentNullException("emailAccount");
int totalEmailsSent = 0;
foreach (var subscription in subscriptions)
{
var tokens = new List<Token>();
_messageTokenProvider.AddStoreTokens(tokens);
_messageTokenProvider.AddNewsLetterSubscriptionTokens(tokens, subscription);
string subject = _tokenizer.Replace(campaign.Subject, tokens, false);
string body = _tokenizer.Replace(campaign.Body, tokens, true);
var email = new QueuedEmail()
{
Priority = 3,
From = emailAccount.Email,
FromName = emailAccount.DisplayName,
To = subscription.Email,
Subject = subject,
Body = body,
CreatedOnUtc = DateTime.UtcNow,
EmailAccountId = emailAccount.Id
};
_queuedEmailService.InsertQueuedEmail(email);
totalEmailsSent++;
campaignSubscriberTrack.CampaignId = campaign.Id;
campaignSubscriberTrack.NewsletterSubscriberId = subscription.Id;
campaignSubscriberTrack.Campaign = campaign;
campaignSubscriberTrack.NewsletterSubscription = subscription;
campaignSubscriberTrack.IsEmailOpened = 0;
campaignSubscriberTrack.OpenedOnUtc = null;
Guid guid;
string id = tokens.Where(t => t.Key.Equals("TrackImage")).Select(t => t.Value).FirstOrDefault();
var lastPart = (id.Split('/').Last()).Split('=').Last();
if (lastPart != null)
{
guid = new Guid(lastPart);
campaignSubscriberTrack.ImageGUID = guid;
}
***InsertCampaignNewsletterSubscriberTrack(campaignSubscriberTrack);***
}
campaign.CampaignSchedulingTime = 0;
UpdateCampaign(campaign);
return totalEmailsSent;
}
班级 -
public partial class CampaignNewsletterSubscriberTrack
{
public virtual Campaign Campaign { get; set; }
public virtual int CampaignId { get; set; }
public virtual NewsLetterSubscription NewsletterSubscription { get; set; }
public virtual int NewsletterSubscriberId { get; set; }
public virtual int IsEmailOpened { get; set; }
public virtual DateTime? OpenedOnUtc { get; set; }
public virtual Guid ImageGUID { get; set; }
}
}
模型 -
public class CampaignNewsletterSubscriberTrackModel
{
public int CampaignId { get; set; }
public int NewsletterSubscriberId { get; set; }
public int IsEmailOpened { get; set; }
public DateTime? OpenedOnUtc { get; set; }
public Guid ImageGUID { get; set; }
}
在这个函数中,记录被插入 -
public virtual void InsertCampaignNewsletterSubscriberTrack(CampaignNewsletterSubscriberTrack track)
{
if (track == null)
throw new ArgumentNullException("track");
_campaignNewsletterSubscriberTrackRepository.Insert(track);
}
错误发生在 --> InsertCampaignNewsletterSubscriberTrack(campaignSubscriberTrack); 我应该怎么办???谁能告诉我哪里出错了?