Microsoft 开发人员网络 > 论坛主页 > SharePoint - Development and Programming > Why the event handler is not trigged from console app ?
提出问题提出问题
 

已答复Why the event handler is not trigged from console app ?

  • 2008年8月31日 18:26ovulex 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     包含代码
     A few days ago when try to test an event handler, I've build a simple console app. and add from code an item to a list. Surprise ! ItemAdded() was not called. Here is the Main() method:

    static void Main(string[] args)
           {
               using (SPSite site = new SPSite("http://ovidiubeches/"))
               {
                   using (SPWeb web = site.OpenWeb("dt"))
                   {
                       web.AllowUnsafeUpdates = true;

                       SPList list = web.Lists["Tasks"];
                       SPListItem item = list.Items.Add();
                       item["Title"] = "new item " + DateTime.Now.ToString();
                       item.Update();

                       web.AllowUnsafeUpdates = false;
                   }
               }
           }

    So the even hanlder  is working perfect when add item from browser or from a webpart or from a webservice. What is the answer for this ? Maybe you ask why do you want add an item from console app?! Why not ? What if I want to have a unit testing ?

    many thanx,

    Ovidiu

答案

  • 2008年9月5日 22:38JakeJBlues 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复
    Hello Togehter,

    it seems very funny, we had the same problem.

    The ItemAdded-Event seems not to be fired, when the application, which requests a add or update item is ended.

    So the ItemAdding and ItemUpdating works always, because it is not a asynchronous request.

    When we test in our console application to add or updating list items everything works fine, when we want to go to production it doesn't work.

    The difference was, in the tests the last statement was a System.Console.ReadLine, which seems to give the enviroment enough time to call the ItemAdded-Event.

    In production we removed that line and the ItemAdded was not called.

    Greetings JJR
    welcome to the blues ;-)

全部回复

  • 2008年8月31日 22:55Gary LapointeMVP用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    I've done lots of stuff with adding items via the command line (via custom stsadm extensions) and I've never had an issue with event handlers not firing.  Maybe it has to do with AllowUnsafeUpdates being set to true - you don't need to set this for command line operations.
  • 2008年9月1日 0:52Moonis TahirMVP用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    did you check event log to see if there was any issue. I think event handler was triggered but some code may have issue in your event handler. if you can check event log and sharepoint Logs directory log files also that should give you the idea of possible fix.
    If possible if you can paste some ItemAdded event code that will be great.
  • 2008年9月1日 3:27ErucyMVP用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    It seems like the Event Handler of SharePoint only trigged in a Context environment ( in the w3wp.exe process, that means the WebPart, the WebApplication, the Web Services, or the Event Handler it self).
    It cannot trigged in a console application, because it's not running under the "web". I think it's by design......
  • 2008年9月1日 20:14ovulex 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     包含代码
    Yes Erucy, I think same right now .... but do have somebody have a explanation for this ? 
    Here is the event handler code : ...as I said before it is working perfect in other situations :)

     public class ListSyncEventHandler : SPItemEventReceiver
        {        public override void ItemAdded(SPItemEventProperties properties)
            {
                DisableEventFiring();

                try
                {
                    Logging.LogMessage("info", "start ListSyncEventHandler.ItemAdded");            }
                catch (Exception ex)
                {
                    Logging.LogMessage("Exception", "Exception in ItemAdded");
                    Logging.LogException(ex);
                    throw ex;
                }

                EnableEventFiring();
            }

     .....
    }

    So I do invite other sharepoint programmer to try add items from a custom app console (c#/vb)... not stsadm

    many thanx,

    Ovidiu
  • 2008年9月2日 1:09Gary LapointeMVP用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    STSADM is just a console app - there's nothing special about it - there is no web context.  Did you check your event log to see if there's any errors?
  • 2008年9月5日 22:38JakeJBlues 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复
    Hello Togehter,

    it seems very funny, we had the same problem.

    The ItemAdded-Event seems not to be fired, when the application, which requests a add or update item is ended.

    So the ItemAdding and ItemUpdating works always, because it is not a asynchronous request.

    When we test in our console application to add or updating list items everything works fine, when we want to go to production it doesn't work.

    The difference was, in the tests the last statement was a System.Console.ReadLine, which seems to give the enviroment enough time to call the ItemAdded-Event.

    In production we removed that line and the ItemAdded was not called.

    Greetings JJR
    welcome to the blues ;-)
  • 2009年7月16日 13:28TomKrebs 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    In Production (or in Dev) I used the below code in the console app which resolved the issue, now the changes made to the SharePoint List from the C# Console App are causing the Event Handler to get kicked:

    using System.Threading;
    Thread.Sleep(xyz);  

    //xyz milli seconds - setting this to 1 sec (1000) didn't help me, setting to 30 secs (30000) did help, guess this time should be more than the time it takes for the event handler to 'complete'

     

     

    The issue is that the console app is immediately getting closed/completed and not giving "enough time" for the event handler to get kicked (and complete), and I find the Sleep statement coming to the rescue.

    Thanks JakeJBlues for the hint.