1

Closed

MVP Pages binds presenter twice (causing all events to be fired twice)

description

Reproducing this bug is simple enough:

1) Create a new Master Page (does not have to be linked to a presenter, just a plain old master page)
2) Create a standard ASP.NET page under this Master Page. (Not sure if the Master Page is required to reproduce this bug)
3) Associate the ASP.NET page to a presenter using the PresenterBinding attribute (i.e. don't find the presenter via the convention based discover strategy). An easy way to do this is to put the Presenter under some obscure namespace.
4) Set a breakpoint in the constructor of the Presenter.
5) Run the application and you'll find that the Presenter is initialized twice which naturally will cause all the bound view events in the presenter to be executed twice.

This is a silent bug that could cause a lot of issues and a serious performance hit. I found out about it when Sql Server complained about inserting an entity with the same primary key.

I temporarily fixed it by adding a small if clause in the AttributeBasedPresenterDiscoveryStrategy.cs file >> GetBindings >> foreach (var hostAttribute) in relevantHostDefinedAttributes) ....

if (bindings.Any(x => x.PresenterType == hostAttribute.Attribute.PresenterType))
{
messages.Add(string.Format(
    CultureInfo.InvariantCulture,
    "found, but ignored, a [PresenterBinding] attribute on host instance {0} (presenter type: {1}, view type: {2}, binding mode: {3}) because the presenter has been previously bound",
    hostAttribute.Host.GetType().FullName,
    hostAttribute.Attribute.PresenterType.FullName,
    hostAttribute.Attribute.ViewType.FullName,
    hostAttribute.Attribute.BindingMode
));
continue;
}

It basically checks for duplicate Presenter Types. Obviously, this is a hack than a real fix. There's something wrong with the attribute based discovery strategy. I'll attempt to come up with a real fix once I get some time to investigate the issue deeper.

If you have any trouble reproducing this issue, please let me know.

file attachments

Closed Mar 26, 2011 at 12:03 AM by tathamoddie

comments

tathamoddie wrote Nov 3, 2010 at 10:44 PM

Hi Jet,

Which build are you using? It sounds like you're running off a source release.

Also, can you please give us the output from Trace.axd for one of your pages that's binding twice?

-- Tatham

rkuss wrote Nov 4, 2010 at 7:16 AM

Hi,

i can't reproduce the error using the latest official release 0.9.7.4. I used a page as view and a control as view. The page inherits from a masterpage. I've tried regular presenter binding and structuremap factory binding. All same result. No duplicate presenter calls.

Jet_Blazer wrote Nov 6, 2010 at 5:50 AM

@rkuss: I think you're right about the official release. No issues with the build.
@tathamoddie: Yes, I am running off a source release.

I've attached a sample to reproduce the bug with the latest changeset ((a6df5c98f42d)) as of this post.

Sorry for the delay. Crazy weekdays.

tathamoddie wrote Nov 7, 2010 at 3:30 AM

Thanks Jet - that clears that up.

The only thing that's in source but not release is the convention based discovery support. Thus, if you're using attributes anyway, you'll be better off on a compiled release.

ppattisapu wrote Feb 2, 2011 at 11:35 PM

Tatham,
I ma having the same issue. Using the attribute based presenter wiring is firing the presenter constructor twice. I downloaded the latest and greatest version [.0.9.7.4]. Infact,, I was able to reproduce this bug even from the "FeatureDemos.Web" downloaded from "webformsmvp_a6df5c98f42d" branch [Run the sample and but a breakpoint in HelloWorldPresenter.cs constructor and run the PageView.aspx]. I tried to modify the source by applying the above fix as mentioned by "Jet Blazer" but it didn't work. Help please?

-Prak

ppattisapu wrote Feb 2, 2011 at 11:35 PM

Tatham,
I ma having the same issue. Using the attribute based presenter wiring is firing the presenter constructor twice. I downloaded the latest and greatest version [.0.9.7.4]. Infact,, I was able to reproduce this bug even from the "FeatureDemos.Web" downloaded from "webformsmvp_a6df5c98f42d" branch [Run the sample and but a breakpoint in HelloWorldPresenter.cs constructor and run the PageView.aspx]. I tried to modify the source by applying the above fix as mentioned by "Jet Blazer" but it didn't work. Help please?

-Prak

ppattisapu wrote Feb 3, 2011 at 1:52 PM

Tatham,
I am having the same issue. Using the attribute based presenter wiring is firing the presenter constructor twice. I downloaded the latest and greatest version [.0.9.7.4]. Infact,, I was able to reproduce this bug even from the "FeatureDemos.Web" downloaded from "webformsmvp_a6df5c98f42d" branch [Run the sample and but a breakpoint in HelloWorldPresenter.cs constructor and run the PageView.aspx]. I tried to modify the source by applying the above fix as mentioned by "Jet Blazer" but it didn't work. Help please?

-Prak

tathamoddie wrote Mar 14, 2011 at 12:22 PM

This should all be fine now in the source as of 023ed098abac.

ldiego08 wrote Mar 23, 2011 at 5:42 AM

I'm facing this same issue.

I'm using MvpPages decorated with the PresenterBinding attribute and the presenter constructor is called twice for an unknown reason causing the binded Load event (that performs heavy database interaction) be called twice as well.

Is there anything I should disable when using attribute-based binding or something?

tathamoddie wrote Mar 23, 2011 at 7:13 AM

Diego,

Attribute based binding should work how it always has. We did a lot of work in this are for v1 though, so there's an inherent risk of bugs there.

Let's keep debugging this on the mailing list thread.


-- Tatham

tathamoddie wrote Mar 25, 2011 at 6:54 AM

Diego,

Your scenario (self hosted views) is now fixed in fa48bb933000.

-- Tatham

ldiego08 wrote Mar 25, 2011 at 2:52 PM

Way to go, Tatham!

I really appreciate the quick response.

Thanks for the fix.