Domanda

I am having a nightmare trying to debug the event receiver of a Sharepoint list. This is the code of what I am doing:

//Add an event receiver to the list
list.EventReceivers.Add(SPEventReceiverType.ItemAdded, "DatasEvent, Version = 1.0.0.0, Culture = neutral, PublicKeyToken =  6f4db1e1fedbed57", "DatasEvent.DatasEventReceiver");

public override void ItemAdded(SPItemEventProperties properties)
{
    try
    {
        int itemIdSql;
        SPListItem item = properties.ListItem;
        ...
    }
    catch (SqlException ex)
    {
        Debug.WriteLine(ex.Message);
    }
}

If I place a breakpoint in the event receiver it will not stop.

È stato utile?

Soluzione

First of all ensure that the latest assembly version gets to the GAC. The easiest way for it is to use the "Deploy" option from the context menu of the project or simply hit F5.

Then please let us know where this code is allocated

//Add an event receiver to the list list.EventReceivers.Add(SPEventReceiverType.ItemAdded, "DatasEvent, Version = 1.0.0.0, Culture = neutral, PublicKeyToken = 6f4db1e1fedbed57", "DatasEvent.DatasEventReceiver");

if it is placed in a feature receiver then make sure that it is activated before you try to attach to w3wp.exe that corresponds to the app pool your target web application corresponds to.

Altri suggerimenti

go to your visual studio Debug -> Attach process -> attach all available w3wp.exe in the list. Now try activating the feature in the web browser. Your breakpoint should be loaded and hit.

I personally prefer adding System.diagnostic.debug.WriteLn() messages to the event receiver code and view them using DebugView on the server. Attaching to worker processes every time is annoying.

I would try:

Debugger.Launch();

Only in a development environment, otherwise it will try to debug for every request.

I would put it before adding the event receiver.

ItemAdded handles the asynchronous event that occurs after an item is added. The execution is carried out via timer job (and not in current worker process w3wp). So you should attach to OWSTIMER process to debug it.

You should enable debugging of sharepoint in two web.config files. You should use Debugger.Launch() to stop the runtime. E.g. After deployment and Activation event fires (in EventReceiver's code there is a Debugger.Launch() command) and VS asks you to debug the code.

There is no need to use "attach to process", but it's another possible way to debug.

this links helped me a lot

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top