asp.net mvc5 my websites 如何让代码每分钟自动执行
How to make code execute automatically every minute in asp.net mvc5 my websites
我有使用应用程序控制台完成的代码,我想使用 asp.net mvc5 在我的网站中使用它,以便存储它从 RSS 提要带来的数据并将其存储在数据库中
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using CodeHollow.FeedReader;
using CodeHollow.FeedReader.Feeds;
using System.Data;
using System.Data.SqlClient;
using System.Data.Entity.Core;
using System.Data.Entity.Infrastructure;
namespace RssFeedBackEnd
{
public class Program
{
public static void Main(string[] args)
{
RssFeedDB db = new RssFeedDB();
var qry = (from c in db.Links select c);
foreach (var li in qry)
{
try
{
var feed = FeedReader.Read(li.adressLink);
var item = feed.Items;
if (feed.Type.ToString() == "Rss_2_0")
{
//New n = new New();
foreach (var items in item)
{
string v = items.Id;
int h = validate(v);
if (h == 0)
{
try
{
New n = new New();
n.TitleNews = items.Title;
n.LinkNews = items.Link;
n.TitlePage = feed.Title;
n.LinkPage = feed.Link;
n.Linkimg = items.SpecificItem.Element.Element("enclosure").Attribute("url").Value;
n.IDurl = items.Id;
n.Pubdate = DateTime.Now;
n.EntryTime = DateTime.Now;
n.Description = items.Description;
n.IdCatogrey = li.CategorayID;
//n.NameCatogrey = li.Category.NameCategory.ToString();
db.News.Add(n);
}
catch { }
}
}
}
}
catch { }
}
db.SaveChanges();
}
static int validate(string n) {
int a = 0;
RssFeedDB db = new RssFeedDB();
var qry = (from m in db.News select m);
foreach (var nws in qry)
{
if (nws.IDurl == n) { a =1; break; }
}
return a;
}
}}
希望能帮我把它放到web应用程序里面,分分钟做他的工作。
我的项目的想法是从RSS feeds链接中获取数据(新闻)并将它们存储在数据库中以显示在我的网站上。
我希望我已经向您展示了我想做的事情。
Web 应用程序根本不适合此类任务。网站的工作是响应来自其他人(通常是人,通过浏览器)的请求。它不会在特定时间自动 运行。
为了实现您的目标,在您的服务器上您需要一个带有计时器的 Windows 服务(然后在每次计时器到期时执行必要的代码),或者一个 Windows 预定定期触发特定应用程序的任务。
既然你已经有了一个控制台应用程序来完成你需要的工作,那么对你来说最简单的解决方案可能是设置一个 Windows 定期执行控制台应用程序的计划任务。
N.B。当然,您仍然可以有一个单独的网站来显示控制台应用程序已保存在数据库中的数据。
P.S。有 ASP.NET 的扩展,例如 Hangifire 和 Quartz.NET,它们可以为 Web 应用程序添加调度功能,但对于您在这种情况下想要执行的操作,它们可能有点矫枉过正。
我有使用应用程序控制台完成的代码,我想使用 asp.net mvc5 在我的网站中使用它,以便存储它从 RSS 提要带来的数据并将其存储在数据库中
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using CodeHollow.FeedReader;
using CodeHollow.FeedReader.Feeds;
using System.Data;
using System.Data.SqlClient;
using System.Data.Entity.Core;
using System.Data.Entity.Infrastructure;
namespace RssFeedBackEnd
{
public class Program
{
public static void Main(string[] args)
{
RssFeedDB db = new RssFeedDB();
var qry = (from c in db.Links select c);
foreach (var li in qry)
{
try
{
var feed = FeedReader.Read(li.adressLink);
var item = feed.Items;
if (feed.Type.ToString() == "Rss_2_0")
{
//New n = new New();
foreach (var items in item)
{
string v = items.Id;
int h = validate(v);
if (h == 0)
{
try
{
New n = new New();
n.TitleNews = items.Title;
n.LinkNews = items.Link;
n.TitlePage = feed.Title;
n.LinkPage = feed.Link;
n.Linkimg = items.SpecificItem.Element.Element("enclosure").Attribute("url").Value;
n.IDurl = items.Id;
n.Pubdate = DateTime.Now;
n.EntryTime = DateTime.Now;
n.Description = items.Description;
n.IdCatogrey = li.CategorayID;
//n.NameCatogrey = li.Category.NameCategory.ToString();
db.News.Add(n);
}
catch { }
}
}
}
}
catch { }
}
db.SaveChanges();
}
static int validate(string n) {
int a = 0;
RssFeedDB db = new RssFeedDB();
var qry = (from m in db.News select m);
foreach (var nws in qry)
{
if (nws.IDurl == n) { a =1; break; }
}
return a;
}
}}
希望能帮我把它放到web应用程序里面,分分钟做他的工作。
我的项目的想法是从RSS feeds链接中获取数据(新闻)并将它们存储在数据库中以显示在我的网站上。
我希望我已经向您展示了我想做的事情。
Web 应用程序根本不适合此类任务。网站的工作是响应来自其他人(通常是人,通过浏览器)的请求。它不会在特定时间自动 运行。
为了实现您的目标,在您的服务器上您需要一个带有计时器的 Windows 服务(然后在每次计时器到期时执行必要的代码),或者一个 Windows 预定定期触发特定应用程序的任务。
既然你已经有了一个控制台应用程序来完成你需要的工作,那么对你来说最简单的解决方案可能是设置一个 Windows 定期执行控制台应用程序的计划任务。
N.B。当然,您仍然可以有一个单独的网站来显示控制台应用程序已保存在数据库中的数据。
P.S。有 ASP.NET 的扩展,例如 Hangifire 和 Quartz.NET,它们可以为 Web 应用程序添加调度功能,但对于您在这种情况下想要执行的操作,它们可能有点矫枉过正。