Startup.cs: 调用控制器上下文
Startup.cs: calling a context of controller
我担心的是:有一个每天发送电子邮件的 RecurringJon。在静态电子邮件内容中,它工作正常。但我的最终用户希望每天将他们的关键物品数量发送给他们。我有一个用于他们库存的控制器,用于比较项目以确定哪些项目处于临界水平。但我无法调用库存的_context。
我已经尝试在服务中添加上下文,但仍然无法正常工作。
ItemRegContext
namespace Intranet.Controllers
{
public class ItemRegController : Controller
{
private readonly ItemRegContext _context;
public ItemRegController(ItemRegContext context)
{
_context = context;
}
}
Startup.cs
public void Configure(IApplicationBuilder app, IRecurringJobManager recurringJob)
{
app.UseHangfireDashboard();
recurringJob.AddOrUpdate("", () => SendEmail(), Cron.Minutely());
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
public void SendEmail()
{
var message = new MimeMessage();
var builder = new BodyBuilder();
string msgFromDB = string.Empty;
var items = _context.ItemRegs;
foreach (ItemReg item in items)
{
if (item.CritLevel >= item.Qty)
{
msgFromDB += "<tr>" +
"<td>" + item.ItemName + "</td>" +
"<td>" + item.ItemDesc + "</td>" +
"<td>" + item.ManufName + "</td>" +
"<td>" + item.AsstSerial + "</td>" +
"<td>" + item.PartNum + "</td>" +
"<td>" + item.TypeName + "</td>" +
"<td>" + item.CalDate + "</td>" +
"<td>" + item.Qty + "</td>" +
"<td>" + item.UnitName + "</td>" +
"<td>" + item.Remarks + "</td>" +
"<td>" + item.LocName + "</td>" +
"</tr>";
}
//else {
// msgFromDB += "<tr><<td colspan='11'>No critical record on the list</td>></tr>";
//}
}
#region email
// reference value from appsettings.json
string host = "smtp.some.com";
int port = 333;
bool boole = false;
string authEmail = "somemail@mail.com";
string authPass = "somepassword";
message.From.Add(new MailboxAddress("somename", "jose.baleros@pttphils.com"));
message.To.Add(new MailboxAddress("somemail@mail.com"));
message.Subject = "some subjects";
builder.HtmlBody =
string.Format(@"
<p> Critical Stocks for today " +
DateTime.Now.ToString() + " </p>" +
"<table border='1'> " +
"<thead>" +
" <tr> " +
" <th> Name </th>" +
" <th> Item Description </th>" +
" <th> MFR </th>" +
" <th> Asset/SN </th>" +
" <th> P/N </th>" +
" <th> Type </th>" +
" <th> CAL Date </th>" +
" <th> QTY </th>" +
" <th> Unit </th>" +
" <th> Remarks </th>" +
" <th> Location </th>" +
" </tr>" +
" </thead>" +
" <tbody>" + msgFromDB + "</tbody> " +
"</table>" +
"<br />" +
"<br />");
message.Body = builder.ToMessageBody();
using (var client = new SmtpClient())
{
client.Connect(host, port, boole);
client.Authenticate(authEmail, authPass);
client.Send(message);
client.Disconnect(true);
}
}
在 public void SendEmail()
, var items = _context.ItemRegs;
_context 应该得到 ItemRegContext
的列表
我担心的是:有一个每天发送电子邮件的 RecurringJon。在静态电子邮件内容中,它工作正常。但我的最终用户希望每天将他们的关键物品数量发送给他们。我有一个用于他们库存的控制器,用于比较项目以确定哪些项目处于临界水平。但我无法调用库存的_context。
我已经尝试在服务中添加上下文,但仍然无法正常工作。
ItemRegContext
namespace Intranet.Controllers
{
public class ItemRegController : Controller
{
private readonly ItemRegContext _context;
public ItemRegController(ItemRegContext context)
{
_context = context;
}
}
Startup.cs
public void Configure(IApplicationBuilder app, IRecurringJobManager recurringJob)
{
app.UseHangfireDashboard();
recurringJob.AddOrUpdate("", () => SendEmail(), Cron.Minutely());
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
public void SendEmail()
{
var message = new MimeMessage();
var builder = new BodyBuilder();
string msgFromDB = string.Empty;
var items = _context.ItemRegs;
foreach (ItemReg item in items)
{
if (item.CritLevel >= item.Qty)
{
msgFromDB += "<tr>" +
"<td>" + item.ItemName + "</td>" +
"<td>" + item.ItemDesc + "</td>" +
"<td>" + item.ManufName + "</td>" +
"<td>" + item.AsstSerial + "</td>" +
"<td>" + item.PartNum + "</td>" +
"<td>" + item.TypeName + "</td>" +
"<td>" + item.CalDate + "</td>" +
"<td>" + item.Qty + "</td>" +
"<td>" + item.UnitName + "</td>" +
"<td>" + item.Remarks + "</td>" +
"<td>" + item.LocName + "</td>" +
"</tr>";
}
//else {
// msgFromDB += "<tr><<td colspan='11'>No critical record on the list</td>></tr>";
//}
}
#region email
// reference value from appsettings.json
string host = "smtp.some.com";
int port = 333;
bool boole = false;
string authEmail = "somemail@mail.com";
string authPass = "somepassword";
message.From.Add(new MailboxAddress("somename", "jose.baleros@pttphils.com"));
message.To.Add(new MailboxAddress("somemail@mail.com"));
message.Subject = "some subjects";
builder.HtmlBody =
string.Format(@"
<p> Critical Stocks for today " +
DateTime.Now.ToString() + " </p>" +
"<table border='1'> " +
"<thead>" +
" <tr> " +
" <th> Name </th>" +
" <th> Item Description </th>" +
" <th> MFR </th>" +
" <th> Asset/SN </th>" +
" <th> P/N </th>" +
" <th> Type </th>" +
" <th> CAL Date </th>" +
" <th> QTY </th>" +
" <th> Unit </th>" +
" <th> Remarks </th>" +
" <th> Location </th>" +
" </tr>" +
" </thead>" +
" <tbody>" + msgFromDB + "</tbody> " +
"</table>" +
"<br />" +
"<br />");
message.Body = builder.ToMessageBody();
using (var client = new SmtpClient())
{
client.Connect(host, port, boole);
client.Authenticate(authEmail, authPass);
client.Send(message);
client.Disconnect(true);
}
}
在 public void SendEmail()
, var items = _context.ItemRegs;
_context 应该得到 ItemRegContext