Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AddTagHelperActivation incorrectly uses DefaultScopedLifestyle for tag helpers instead of Transient #761

Closed
ofirgeller opened this issue Oct 12, 2019 · 3 comments
Labels
Milestone

Comments

@ofirgeller
Copy link

@ofirgeller ofirgeller commented Oct 12, 2019

I have a tag helper that inherits from a Microsoft tag helper. before using:
options.AddTagHelperActivation inside of the AddSimpleInjector configuration method i would get an instance of the helper per appearance of the tag in the cshtml document. now it seems I am getting the same instance.

Expected behavior

Would expect simple injector to behave like the dotnet core framework DI and create an instance per appearance of a tag. or give an option of specifying that it should.

Actual behavior

The tag helper is registered as a singleton/per request or something similar.

To Reproduce

[HtmlTargetElement(tag: "simple-tag")]
public class SimpleTag : AnchorTagHelper
{
    public override int Order => int.MaxValue;

    public SimpleTag(IHtmlGenerator generator) : base(generator) { }

    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
        base.Process(context, output);
        output.TagName = "a";
        output.Attributes.SetAttribute("href", $"/something" + output.Attributes["href"].Value);
    }
}

If you use the tag helper multiply times, sometimes with an href attribute and sometimes with an action attribute you will get an exception since you can't have both attributes on the same tag, and since the tag helper will be the same instance if the rendering is in this order:

  1. tag with action attribute
  2. tag with href attribute

you will get an exception when you get to the second tag.

Context:
Container has these settings:

container.Options.DefaultLifestyle = Lifestyle.Scoped;
container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();

Which is the wrong option for the tag helpers, but there is no way to override the lifestyle of the tag helpers that I could find.

@ofirgeller ofirgeller added the question label Oct 12, 2019
@dotnetjunkie dotnetjunkie added bug task and removed question labels Oct 12, 2019
@dotnetjunkie dotnetjunkie added this to the v4.8 milestone Oct 12, 2019
@dotnetjunkie

This comment has been minimized.

Copy link
Collaborator

@dotnetjunkie dotnetjunkie commented Oct 12, 2019

I can confirm this is a bug. I added it to the v4.8 milestone. In the meantime, as a workaround, you can override the lifetime selection behavior to ensure that tag helpers always get a transient lifestyle.

@dotnetjunkie dotnetjunkie changed the title AddTagHelperActivation treats tag helpers as singletons AddTagHelperActivation incorrectly uses DefaultScopedLifestyle for tag helpers instead of Transient Oct 20, 2019
@dotnetjunkie

This comment has been minimized.

Copy link
Collaborator

@dotnetjunkie dotnetjunkie commented Nov 17, 2019

bug-761 branch created.

dotnetjunkie added a commit that referenced this issue Nov 17, 2019
TagHelpers will now always get the Transient lifestyle. Fixes #761.
@dotnetjunkie

This comment has been minimized.

Copy link
Collaborator

@dotnetjunkie dotnetjunkie commented Nov 24, 2019

We just release Simple Injector v4.8, which fixed this bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.