views:

55

answers:

0

Hi all, i'm developing an application that uses ASP.NET MVC 2 and autofac latest release. I've developed a custom mvc helper method that allow me to easily display a custom text input in the form like this:

<%: Html.MyMethod("blabla") %>

The method generate a string, put it as the input value and save it also in a session. The problem is that it seems that with autofac enabled the controller constructor that uses dependency injetion is reloaed two times, so the helper is called two times and the session is overwritten with a new generated string while the form still has the old original value. Why?

UPDATE 1 This is the mvc helper:

public static MvcHtmlString MyMethod(this HtmlHelper htmlHelper)
{
    string myguid = Guid.NewGuid();

    TagBuilder tagBuilder = new TagBuilder("input");
    tagBuilder.Attributes.Add("type", "hidden");
    tagBuilder.Attributes.Add("name", "myinputname");
    tagBuilder.Attributes.Add("id", "myinputid");
    tagBuilder.Attributes.Add("value", myguid);

    htmlHelper.ViewContext.HttpContext.Session.Add("mysession", myguyid);

    return MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.SelfClosing));
}

This is the controller:

public class MyController : Controller
{
    private readonly MyInterface interface1;
    private readonly MyInterface2 interface2;

    public MyController(MyInterface service, MyInterface2 service2)
    {
        interface1 = service;
        interface2 = service2;
    }
}

With the Visual Studio debugger activated, if i put a breakpoint at the Session.Add line of the helper method and at the start of the MyController constructor i can see that it's called two times, so the first time it's all ok but the second one it doesn't generate a new form input but overwrite the session with a new guid. This problem doesn't happen in another application where i don't use autofac... Also, i don't have a config of the controllers, this is how i initialize autofac in global.asax:

ControllerBuilder.Current.SetControllerFactory(new AutofacControllerFactory(MyContainerProvider));