当前位置: 移动技术网 > IT编程>开发语言>c# > c#使用windows服务更新站点地图的详细示例

c#使用windows服务更新站点地图的详细示例

2019年07月18日  | 移动技术网IT编程  | 我要评论

由于公司平台访问人数逐渐增多,公司项目的数据库已经几次出现宕机现象。为减轻数据库压力,我上个月对公司项目做了下调整。把新闻板块提取出来单独一个站点,单独一个数据库。减少了主站点和数据库的负担和压力。

但放在线上一个月,新闻新的发布数量最少已经有500篇左右。百度只收录了70个左右,于是想到可能是没有站点地图造成的。但怎么定时更新站点地图呢?

我尝试使用windows服务来定时更新站点地图。

首先需要了解下几个问题。

1、百度收录的站点地图(sitemap)的格式。详情请查看该链接:查看

目前我只用到了xml格式的站点地图,就讲下xml格式的站点地图。

xml格式:

各个标签的解释:

<loc>www.example1.com</loc>该页的网址。该值必须少于256个字节(必填项)。格式为<loc>您的url地址</loc>

<lastmod>2010-01-01</lastmod>该文件上次修改的日期(选填项)。格式为<lastmod>年-月-日</lastmod>

<changefreq> always </ changefreq >页面可能发生更改的频率(选填项) 。

有效值为:always、hourly、daily、weekly、monthly、yearly、never

<priority>1.0</priority >此网页的优先级。有效值范围从 0.0 到 1.0 (选填项) 。0.0优先级最低、1.0最高。

2、站点地图更新的频率问题

由于公司新闻更新一般时间固定在9:30-18:00。每天大概发布10——50篇新闻,所以我设想更新站点地图的时间在:9:00——19:00,每3个小时更新一次,其他时间不做任何操作,降低数据库查询的负担。

开始写windows 服务。

1、新建windows服务,给windows起个名字

2、添加安装程序

双击service1.cs进入视图界面,右键,选择“添加安装程序”

添加以后的效果。

serviceinstaller1:服务安装组件。(主要用于设置 服务名称、服务描述、服务是否开机启动或延迟启动、服务启动方式等)

serviceprocessinstaller1:服务进程安装组件(主要用户设置运行服务的账户类型,如:user、localservice、networkservice、localsystem等)

serviceinstaller1的属性设置(快捷键f4):

属性说明:

delayedautostart:设置是否自动启动

description:服务描述

servicename:服务名称

starttype:启动类型。有三种:manual(启动)、automatic(自动)、disabled(不启动)

serviceprocessinstaller1属性设置(快捷键f4):

属性说明:

account:运行服务的账户类型。有四种:user、localservice、networkservice、localsystem。

我选择的是localsystem,意思是:当前系统都能运行。

3、定时触发问题的一些需要注意的误区。

定时触发,大家肯定想到的是timer组件,但一定要记住。windows服务下是没有timer控件的。。。。这里看到的timer组件是不会定时触发的,需要自己实现timer定时触发的事件,本人曾在此坑深陷多时。。。。

由于本人为了实现定时触发的事件可控性和可修改性。增加了一个配置文件。配置文件添加方式如图:

windows服务项目——右键——添加——新建项

配置文件设置:

4、定时windows服务代码:

复制代码 代码如下:

public partial class npcgonewsservice : servicebase
    {
        timer atimer = new timer();       //system.timers,不是form的 
        public npcgonewsservice()
        {
            initializecomponent();
        }

        protected override void onstart(string[] args)
        {
            //一个一直重写sitemap的服务
            atimer.elapsed += new elapsedeventhandler(atimer_tick);
            atimer.enabled = true; 
            atimer.interval = int.parse(publicmethods.getappsettings("newssiteupdatetime")) * 1000;    //配置文件中配置的秒数
            atimer.start();
        }


        protected override void onstop()
        {
            this.atimer.enabled = false;
            this.atimer.stop();
        }

        /// <summary>
        /// timer定时触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void atimer_tick(object sender, elapsedeventargs e)
        {
            //调用该服务的时间为上午9点——下午18点,其他时间不调用
            if (datetime.now.hour >= 9 && datetime.now.hour <= 19)
            {
                makesitemap make = new makesitemap();
                make.getbaidusitemap();
            }
        }
    }

生成百度地图的方法(getbaidusitemap):

复制代码 代码如下:

        /// <summary>
        /// 生成百度站点地图
        /// </summary>
        /// <returns></returns>
        public void getbaidusitemap()
        {
            xmldocument xmldoc = new xmldocument();
            //获取新闻站点地图的本地保存路径
            string path = path.combine(publicmethods.getappsettings("newssitemappath"));
            xmldoc.load(path);

            //查找根节点
            xmlnode root = xmldoc.selectsinglenode("urlset");
            //删除以前的所有老节点
            root.removeall();

            #region 平台公告
            //查询最新100条平台公告
            list<it_news> platformnotice = getplatformnotice();
            if (platformnotice != null && platformnotice.count > 0 && platformnotice[0].newsid > 0)
            {
                foreach (it_news platformnoticenews in platformnotice)
                {
                    xmlelement xesub1 = xmldoc.createelement("url");

                    xmlelement xe1 = xmldoc.createelement("loc");
                    xe1.innertext = "http://news.npcgo.com" + "/notice-" + platformnoticenews.newstype + "-" + platformnoticenews.newsid + ".html";
                    xmlelement xe2 = xmldoc.createelement("lastmod");
                    xe2.innertext = convert.todatetime(platformnoticenews.newscreatetime).tostring("yyyy-mm-dd");
                    xmlelement xe3 = xmldoc.createelement("changefreq");
                    xe3.innertext = "daily";
                    xmlelement xe4 = xmldoc.createelement("priority");
                    xe4.innertext = "0.2";

                    xesub1.appendchild(xe1);
                    xesub1.appendchild(xe2);
                    xesub1.appendchild(xe3);
                    xesub1.appendchild(xe4);

                    root.appendchild(xesub1);
                }
            }
            #endregion

            xmldoc.appendchild(root);
            xmldoc.save(path);
        }

安装windows服务:

需要写一个windows服务安装的批处理和卸载的批处理,都放在windows服务项目的bin/debug文件夹下。

安装服务,install.bat代码:

复制代码 代码如下:

%systemroot%\microsoft.net\framework\v4.0.30319\installutil.exe gametradingnewsservice.exe
net start npcgonewsservices
sc config npcgonewsservices start= auto

解释:

gametradingnewsservice.exe 是windows服务项目编译以后bin文件中的exe程序的名称。
net start xxx 是开启xxx服务
npcgonewsservices 是windows服务名称(即:serviceinstaller1属性面板设置的服务名称(servicename属性))
sc config npcgonewsservices start= auto 是设置windows服务启动方式为自动

卸载服务 unstall.bat代码:

复制代码 代码如下:

%systemroot%\microsoft.net\framework\v4.0.30319\installutil.exe /u gametradingnewsservice.exe
gametradingnewsservice.exe 是windows服务项目编译以后bin文件中的exe程序的名称

至此,一个服务是彻底完成。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网