Implementing scheduled services in Sitefinity 3.x

by
In this post I will show you how to create scheduled services that could be executed on a certain period of time - hourly, daily, weekly, monthly etc. The post will expose web service that runs a Sitefinity search index. The implementation requires

 

1.Creating a web service in Sitefinity

2. Creating a console application that will be used to call the service

3. Using Windows Task Scheduler.

 

Creating a web service.

 

WebService

  • Leave the generated asmx in projet's root. 
  • Make sure that the generated class is located in App_Code folder

 

In the WebMethod I'm adding getting all services using IndexDataManager object. Then I am looping through all indexes and if the name of the index that I am going to pass exists in the IndexingServiceInfo I run the index programmatically using IndexingService object

 

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Services; 
using Telerik.Search; 
using Telerik.Search.Data; 
using System.Collections; 
using Telerik.Search.Engine; 
 
/// <summary>  
/// Summary description for IndexingService  
/// </summary>  
[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.   
// [System.Web.Script.Services.ScriptService]  
public class IndexingService : System.Web.Services.WebService 
 
    public IndexingService() 
    { 
 
        //Uncomment the following line if using designed components   
        //InitializeComponent();   
    } 
 
    [WebMethod] 
    public void StartIndex(string indexName) 
    { 
        IndexDataManager dataManager = new IndexDataManager(); 
        IList indexes = dataManager.GetServices(); 
        IndexingServiceInfo indexServiceInfo = null
        foreach (IndexingServiceInfo service in indexes) 
        { 
 
            if (service.Name == indexName) 
            { 
                indexServiceInfo = service; 
                break
            } 
        } 
        if (indexServiceInfo != null
        { 
            Telerik.Search.Engine.IndexingService service = 
                new Telerik.Search.Engine.IndexingService(indexServiceInfo); 
            int timeout = this.Context.Server.ScriptTimeout; 
            this.Context.Server.ScriptTimeout = 4800; 
            service.Index(false); 
            this.Context.Server.ScriptTimeout = timeout; 
        } 
        else 
        { 
            throw new ApplicationException("not found name"); 
        } 
    } 

.

 

Creating Console Application

  • Create the console application

 ConsoleApplication

 

  • Add Web reference to the WebService that is located in Sitefinity website

 

  • Implement the logic in the console application to call the WebService method, pass the name of the index and finally run it.

 

namespace IndexConsoleApplication 
    class Program 
    { 
        static void Main(string[] args) 
        { 
            StartIndex("RunFromConsole"); 
        } 
        public static void StartIndex(string indexName) 
        { 
            IndexingService client = new IndexingService(); 
            client.StartIndex(indexName); 
            Console.Write("the website was indexed at" + DateTime.Now.ToUniversalTime().ToString()); 
            Console.Write("next index is scheduled for" + DateTime.Now.ToUniversalTime().AddDays(1).ToString()); 
            Console.Read(); 
        } 
    } 
}  

 

Using Windows Task Scheduler

  • Open My Computer >> Management >> System Tools >> Task Schedler

 

  •  Click Create New Task

- Triggers tab - set the time you want to run the index

- Actions tab - set the path to the exe generated by the console application.

4 Comments

  1. 1 Tony Montata 16 Jun
    Very helpful post! Thank you!
  2. 2 dissanpointed 01 Dec
    Dont like it. I'm little bit dissapointed, that there's no automatic indexing option. As a CMS we expect to have this fixed or supported out of the box. CommunityServer has that from the start. It's annoyning to write everything (not just indexing) custom. Why use sitefinity then?
  3. 3 sathi 08 Dec
    In this post I will show you how to create scheduled services that could be executed on a certain period of time - hourly, daily, weekly, monthly etc. The post will expose web service that runs a Sitefinity search index. The implementation requires
    Dog Boots
  4. 4 Pak 13 Dec
    40. hurry up 匆忙41. keep on doing sth. 继续做某时,保持。。。状态42. all kinds of 各种各样43. a kind of 一种44. laugh at 嘲笑45. listen to sb. 听某人说46. no lenogr 不再47. look after / at / for/ up 照顾 看 注视 寻找查询 查找48. make a mistake 犯错误49. make a noise 发出。。。声音 噪音50. in the middle of 在中间51. neither…nor 既不 也不52. from now on 从现在开始53. a number of 大量的54. at once 立即,马上55. once upon a time 很久以前56. put on / sth down / up 穿上 放下 搭起 张贴57. get / be ready 准备做某事58. take / have a rest 休息59. ring up 打电话60. send for 派人去请

Comment

  1.    
     
     
      
       

Categories

+ View all

Blogs

+ View all