views:

1174

answers:

7

Hey all!

So I've been following Steven Sanderson's book called Pro ASP.NET MVC Framework, and I'm running into an exception:

No parameterless constructor defined for this object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.MissingMethodException: No parameterless constructor defined for this object.

Source Error:

Line 16:             HttpContext.Current.RewritePath(Request.ApplicationPath, false);
Line 17:             IHttpHandler httpHandler = new MvcHttpHandler();
Line 18:             httpHandler.ProcessRequest(HttpContext.Current);
Line 19:             HttpContext.Current.RewritePath(originalPath, false);
Line 20:         }


Source File: C:\Users\Stephen\Documents\Visual Studio 2008\Projects\SportsStore\WebUI\Default.aspx.cs    Line: 18

Here's my WindsorControllerFactory code:

public class WindsorControllerFactory : DefaultControllerFactory
{
    WindsorContainer container;

    // The constructor
    // 1. Sets up a new IoC container
    // 2. Registers all components specified in web.config
    // 3. Registers all controller types as components
    public WindsorControllerFactory()
    { 
        // Instantiate a container, taking configuration from web.config
        container = new WindsorContainer(
                        new XmlInterpreter(new ConfigResource("castle"))
                    );

        // Also register all the controller types as transient
        var controllerTypes = from t in Assembly.GetExecutingAssembly().GetTypes()
                              where typeof(IController).IsAssignableFrom(t)
                              select t;

        foreach (Type t in controllerTypes)
            container.AddComponentWithLifestyle(t.FullName, t, Castle.Core.LifestyleType.Transient);
    }

    // Constructs the controller instance needed to service each request
    protected override IController GetControllerInstance(Type controllerType)
    {
        return (IController)container.Resolve(controllerType);
    }
}

My Global.asax.cs code:

protected void Application_Start()
    {
        RegisterRoutes(RouteTable.Routes);
        ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory());
    }

And the web.config values:

<configSections>
    <section name="castle"
             type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler,
                   Castle.Windsor"/>
</configSections>
<castle>
    <properties>
      <myConnStr>Server=.\SQLEXPRESS;Database=SportsStore;Trusted_Connection=yes;</myConnStr>
    </properties>
    <components>
      <component id="ProdsRepository"
                 service="DomainModel.Abstract.IProductsRepository, DomainModel"
                 type="DomainModel.Concrete.SqlProductsRepository, DomainModel">
        <parameters>
          <connectionString>#{myConnStr}</connectionString>
        </parameters>
      </component>
    </components>
</castle>

Thanks all! -Steve

A: 

I would say the problem is that you are trying to instantiate a Linq to SQL DataContext without passing the connection string.

The solution would be to declare something like:

<components>
    <component id="..." service="..." type="...">
        <parameters>
            <connectionString>blah blah blah</connectionString>
        </parameters>
    </component>
</components>

Another option, the one I'm using in my current project, is to simply create a parameterless constructor in your DataContext that will simply call the constructor that takes the connection string with a default value, declared as a static string on the class itself:

public partial class MyDataContext
{
    private static string standardConnectionString = "blah blah blah";

    public MyDataContext()
             : this(standardConnectionString) {}
}

Does it solve your problem?

Bruno Reis
Hey, Debugged through and saw this exception is firing in the constructor for WindsorControllerFactory:Exception Details: System.Configuration.ConfigurationErrorsException: Could not find section 'castle' in the configuration file associated with this domain.It's clearly in the web.config, but for some reason it's not finding it. I think this is the issue at hand. Since Application_Start fires once, that exception is thrown, then refreshing it can't load the Controller due to that, and since that WindsorControllerFactory is where the logic should be, that's the problem.
LookitsPuck
Annnnd, I'm a newb!Did not realize that there was a web.config for both the Views folder as well as for the application itself. All of the appropriate config data for Castle was in the Views web.config and NOT the web app's web.config. Egg on my face. I swapped and everything is working!
LookitsPuck
A: 

Mate, you're a genius! I did the exact same thing. I didnt realise I was entering the webconfig settings into the view's folder webconfig rather than the actual webconfig for the application.

Vishal
A: 

I wonder how many more would do the same... Ditto here.

Jano
A: 

AHAHAHAHA, I did the same thing. And it's funny how we all did it at around the same time. Thanks LookitsPuck. I've been n00bing around for the past 6 hours trying to figure out what was wrong.

Ciceronius
A: 

Use: protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)

Insted of: protected override IController GetControllerInstance(Type controllerType)

Khawar
A: 

I had this issue, it happens when you have an Entity class without default constructor . Always create one default constructor that dont accept paremeters.

Thiganofx
+1  A: 

I encountered this problem:「Could not load type 'DomainModel.Abstract.IProductsRepository' from assembly 'DomainModel'.」

I search this problem in Google then found here. I can't realize LookitsPuck how correct this problem. So how can I correct this bug?

Huang