MSDN > 論壇首頁 > SharePoint - Development and Programming > Why the event handler is not trigged from console app ?
發問發問
 

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

  • Sunday, 31 August, 2008 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

解答

  • Friday, 5 September, 2008 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 ;-)

所有回覆

  • Sunday, 31 August, 2008 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.
  • Monday, 1 September, 2008 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.
  • Monday, 1 September, 2008 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......
  • Monday, 1 September, 2008 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
  • Tuesday, 2 September, 2008 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?
  • Friday, 5 September, 2008 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 ;-)
  • Thursday, 16 July, 2009 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.

     


    • 已編輯TomKrebs Thursday, 16 July, 2009 13:32
    •